The below should not fail when directories/files contains spaces, it ignores non-regular files and non-standard filenames (like .hello). As input arguments, it takes any number of files and directories (directories are expanded to its containing files). Run the script without arguments to view a description of what it does. I hope the format of the below bash script does not get messed up on the way to your inboxes, as usually happens with the mailinglist's emails to my inbox. ------start #! /bin/bash # Exit on unexpected error set -e # Set how '*' should expand shopt -s dotglob nullglob if [ "$1" == "-v" ] then VERBOSE=1 shift fi if (($#<1)) then cat <<EOF >&2 Usage: $0 [-v] FILE... Moves each FILE to a subdirectory whose name is FILE's extension -v Does not change any files/directories, but shows the commands that would have been executed. $0 operates on each input argument independently. If an argument is a directory, it is expanded to the files of that directory. Only regular file are operated on. A file is generally of the form DIRECTORY/PREFIX.EXTENSION, where EXTENSION does not contain any '.'. First the directory DIRECTORY/EXTENSION is made, and then the file is moved to DIRECTORY/EXTENSION/PREFIX.EXTENSION. EOF #' This comment is for the font-lock-mode in xemacs exit 1 fi # Precondition: Receives one argument that must be a path to an # existing regular file transform() { path="$1" filename="${path##*/}" directory="${path%$filename}" prefix="${filename%.*}" ext="${filename#$prefix}" if [ -z "$prefix" ] then echo "$path: Filename prefix is empty" >&2 return elif [ -z "$ext" -o "$ext" == "." ] then echo "$path: Extension is empty" >&2 return fi ext="${ext#.}" newdir="$directory$ext" if [ -z "$VERBOSE" ] then mkdir -p "$newdir" mv "$path" "$newdir/$filename" else echo "mkdir -p \"$newdir\"" echo "mv \"$path\" \"$newdir/$filename\"" fi return } for FILE in "$@" do if [ -f "$FILE" ] then transform "$FILE" elif [ -d "$FILE" ] then for file in "$FILE"/* do if [ -f "$file" ] then transform "$file" fi done fi Done ------end Håkon Hallingstad -----Original Message----- From: hart [mailto:ody@inf.its-sby.edu] Sent: 30. september 2004 00:43 To: suse-programming-e@suse.com Subject: [suse-programming-e] separating files based on its extension I am a bash newbie, having difficulties making some script. I have some directories containing filez with many extension. I want to separate those filez into new directory on the filez' directory and name the new dir as the filez' extesion. I wonder to know how the bash script look like? For example, /foo/a.pdf moved to /foo/pdf/a.pdf /foo/a.s.zip moved to /foo/zip/a.s.zip /foo/a.b.123 moved to /foo/123/a.b.123 ad so on Thx in advance. -- --- hart -- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e