Skip to content

Instantly share code, notes, and snippets.

@phunehehe
Created August 21, 2011 14:59
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save phunehehe/1160707 to your computer and use it in GitHub Desktop.
Organize files by file extension
#!/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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment