public
Last active

Organize files by file extension

  • Download Gist
organize.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/bin/sh
# Organize files by file extension
# Written in answer to http://unix.stackexchange.com/q/19110/250
 
 
# Configuration (feel free to add your types and change the path)
DOCUMENTS='
pdf
doc
'
DOCUMENT_PATH="$HOME/docs"
 
MUSIC='
mp3
ogg
'
MUSIC_PATH="$HOME/music"
 
 
# Take a list of file extensions and make a regex to use in `find'
function make_regex {
 
REGEX=''
 
for EXT in $(echo $1)
do
if [ -z $REGEX ]
then REGEX=".*\.($EXT"
else REGEX="$REGEX|$EXT"
fi
done
 
REGEX="$REGEX)"
echo $REGEX
}
 
 
# Find and move the files
# Delete "echo" below to let the script do things
find $HOME -type f \
-regextype posix-egrep \
-iregex $(make_regex $DOCUMENTS) \
-exec echo mv -i '{}' $DOCUMENT_PATH \;
 
find $HOME -type f \
-regextype posix-egrep \
-iregex $(make_regex $MUSIC) \
-exec echo mv -i '{}' $MUSIC_PATH \;
 
 
# Desktop notification when done
notify-send "Clean Up" "Documents and music files moved to place."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.