Created
August 21, 2011 14:59
-
-
Save phunehehe/1160707 to your computer and use it in GitHub Desktop.
Organize files by file extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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=' | |
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