Am Mittwoch, 29. September 2004 23:36 schrieb ody:
I've trouble making bash script to move files from a directory to a new directory--(named as the files' extension name). For example: moving files *.pdf on /foo/ to /foo/pdf/, but there are so many extension. But how ? Thx in advance.
untested: # read all entries in current directory (including .dot files) ls -a | while read f; do # skip if it's not a regular file test -f "$f" || continue # get extension (part after last dot) - translate to lowercase to # avoid pdf, PDF, Pdf ... directories ext="`echo \"$f\" | sed 's/.*\.//' | tr '[:upper:]' '[:lower:]'`" # no extension: skip test "$f" = "$ext" && continue # create directory if it does not exist test -d "$ext" || mkdir "$ext" # move file into directory mv "$f" "$ext" done Jan -- Linux-Quickies: http://www.jan-trippler.de PingoS: http://www.pingos.org