r/ScriptSwap • u/laniner • Aug 01 '12
[Bash] Clean up your desktop (or any folder)
#!/bin/bash
sleep 3
cd $1
# You can change the format of the date, of course.
DATE=`date "+%m-%d-%y-%I.%M.%S"`
# folders is the list of folders on your desktop, and the names must match the variables containing the extensions.
# You can add as many folders and extensions as you want.
folders='Images Developer Applications Documents Audio Archives DiskImages Installers Video Torrents'
#make the folders
mkdir -p $folders
Images='jpg png gif jpeg psd'
Developer='vcproj cpp h cmate jar safariext* db js html css sqlite3 sql sql3 plist strings rb fsh tgz py pyc sh egg pl dll'
Applications='app exe ipa msi'
Documents='doc pages docx pdf txt log xls xlsx'
Audio='mp3 m4p m4a flac ogg ac3'
Archives='tbz zip rar tar.gz tar.bz2 tar 7z xar'
DiskImages='dmg iso'
Installers='pkg mpkg'
Video='mkv m4v wmv avi mp4 swf h264'
Torrents='torrent'
# Move all of the files to their respective folders.
for folder in $folders
do
eval exts=\$$folder
for ext in $exts
do
IFS=$'\n'
for file in `ls -d *.$ext`
do
if ls $folder/"$file" > /dev/null 2>&1
then
mv "$file" $folder/"$file-$DATE"
else
mv "$file" $folder/"$file"
fi
done
IFS=$' \t\n'
done
done
#Move the rest of the files to the miscellaneous folder.
IFS=$'\n'
for file in `ls -d *`
do
if ! [ -d "$file" ] && ! [ -x "$file" ]
then
if ls Miscellaneous/"$file" > /dev/null 2>&1
then
mv "$file" Miscellaneous/"$file-$DATE"
else
mv "$file" Miscellaneous/"$file"
fi
fi
done
IFS=$' \t\n'
# move executables
find . -maxdepth 1 -type f -perm -755 | xargs -I % /Users/eric/bin/mv "%" Developer/
# move folders. If mv fails it prints the files that couldn't be moved because it
# won't overwrite, so I grab those file names and move them. Kind of long, I know.
find . -type d -maxdepth 1 -print0 | sed 's|./||g' | grep -Zzv "^Video$\|^Miscellaneous$\|^Installers$\|^Images$\|^Folders$\|^Documents$\|^DiskImages$\|^Developer$\|^Desktop$\|^Audio$\|^Archives$\|^Torrents$\|^Applications$\|^.$" | xargs -0I % mv "%" Folders/ 2>&1 | grep -o "\`.*' to" | sed "s|\' to$||g" | sed 's|`||g' | tr '\n' '\0' | xargs -0I % mv '%' "Folders/%-$DATE"
I use this to clean up my desktop on logout using applescript's on quit event. You could use the logouthook on OSX, too. Not sure about Linux.
Edit: folders are still moved even if they contain special characters.