Skip to content

Instantly share code, notes, and snippets.

@hemalchevli
Forked from phunehehe/organize.sh
Created October 5, 2013 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hemalchevli/6839521 to your computer and use it in GitHub Desktop.
Save hemalchevli/6839521 to your computer and use it in GitHub Desktop.
#!/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