Register
It is currently Fri Apr 18, 2014 12:10 am

Directory Cleanup Script (clean up the mess!)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Mon Sep 18, 2006 1:23 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
** NOTE: Version 2 of the Cleanup Script is now below **
Greetings,

This is my first finished bash script I've made. It works for me but I know for a fact it can be improved. Since I'm still learning bash scripting, I'm posting my script here for ideas, help and tips and such. I created this script for my own use but others may can use it too.

This script basically cleans out the clutter in a folder (/home/username by default). It will move files based on extension to certain folders.


Code:
#! /bin/bash
#
# CleanHouse Script V.2 By Mark Reaves (C)2006
# You may freely distribute this script as long as all comments
# remain in this file. Feel the love of Linux ;-)
#
# This script simply moves files in the users home folder (/home/user)
# to other more organised folders based on extension.
# In the variables section, $HOME will become /home/yourusername

# Please change the variables to suit your needs.
CLUTTERDIR=$HOME          # Default is /home/username. This is the cluttered directory.
MP3DIR=$HOME/Music/mp3          # Default is /home/username/Music/mp3
OGGDIR=$HOME/Music/ogg          # Default is /home/username/Music/ogg
TEXTDIR=$HOME/Texts          # Default is /home/username/Texts
VIDDIR=$HOME/Videos          # Default is /home/username/Videos
SCRIPTDIR=$HOME/Texts/Scripts  # Default is /home/username/Texts/Scripts

cd $CLUTTERDIR
mkdir -p $MP3DIR $OGGDIR $TEXTDIR $VIDDIR $SCRIPTDIR
clear

cat<<_EOF_
Script Starting.

Greetings $USER! Welcome to my cleanup script.
Depending on the speed of your computer
and how many files you have to move
and their size, this could take awhile.
If this messes up your computer,
IT'S NOT MY FAULT!

_EOF_
read -p "Are you ready (yes or no)? " ANSWER
if [[ $ANSWER = yes ]]
   then
      echo "Ok, Lets proceed.";
   else
      echo "Too bad, exiting.";
      exit
   fi
echo "Cleaning up! Please wait..."

ls | while read; do
   case $REPLY in
   (*.mp3)
       mv $REPLY $MP3DIR;;
   (*.ogg)
       mv $REPLY $OGGDIR;;
   (*.txt)
       mv $REPLY $TEXTDIR;;
   (*.doc)
       mv $REPLY $TEXTDIR;;
   (*.rtf)
       mv $REPLY $TEXTDIR;;
   (*.avi)
       mv $REPLY $VIDDIR;;
   (*.mpg)
       mv $REPLY $VIDDIR;;
   (*.mpeg)
       mv $REPLY $VIDDIR;;
   (*.asf)
       mv $REPLY $VIDDIR;;
   (*.sh)
       mv $REPLY $SCRIPTDIR;;
   
   # for any types that aren't in a case statement, they will be left alone
   esac
done

cat<<_EOF_
---------------------------------------------------------
Cleaning complete!
Thank you for using my script.
Please let me know of suggestions
ideas or bug fixes by emailing me at
motstudios@gmail.com
---------------------------------------------------------
_EOF_


NOTE: This script can also be found at the URL below:
Please right click and "Save As" to save it.
http://mark.22kb.com/dl/cleanhouse.sh


Last edited by katasuka on Fri Oct 06, 2006 11:18 am, edited 5 times in total.

Top
 Profile  
 PostPosted: Mon Sep 18, 2006 9:39 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
an interesting concept ;)


Top
 Profile WWW  
 PostPosted: Tue Sep 19, 2006 4:28 am   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
btw, is there a way to hide the errors that show when the directory already exists? or, is there a way to check for the folders and if they exist then continue and if they dont exist then make them?

also is there a way to hide the errors if there are no files to move?


Top
 Profile  
 PostPosted: Tue Sep 19, 2006 12:25 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
katasuka wrote:
btw, is there a way to hide the errors that show when the directory already exists? or, is there a way to check for the folders and if they exist then continue and if they dont exist then make them?

also is there a way to hide the errors if there are no files to move?


Code:
mkdir -p


should "check" then create if it doesn't exist

Quote:
-p, --parents
no error if existing, make parent directories as needed


Top
 Profile WWW  
 PostPosted: Tue Sep 19, 2006 3:45 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
it appears now that when i run the script i get 2 errors that im not sure how to fix.

Code:
Script Starting.

Greetings mark! Welcome to my cleanup script.
Depending on the speed of your computer
and how many files you have to move
and their size, this could take awhile.

Are you ready (yes or no)? yes
./cleanhouse.sh: line 49: unexpected EOF while looking for matching `"'
./cleanhouse.sh: line 50: syntax error: unexpected end of file


and now the script doesn't work. any ideas?


Top
 Profile  
 PostPosted: Wed Sep 20, 2006 12:10 pm   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
katasuka wrote:
it appears now that when i run the script i get 2 errors that im not sure how to fix.

Code:
Script Starting.

Greetings mark! Welcome to my cleanup script.
Depending on the speed of your computer
and how many files you have to move
and their size, this could take awhile.

Are you ready (yes or no)? yes
./cleanhouse.sh: line 49: unexpected EOF while looking for matching `"'
./cleanhouse.sh: line 50: syntax error: unexpected end of file


and now the script doesn't work. any ideas?


It might appear to be two errors, but it's all part of the same error........What it means is there is a missing quote (") someplace in the script......There's no closing quote someplace, so when it reaches the EOF, it tells you that as well as missing a quote........

Try running the script with 'bash -x' in front of the command, like so:
Code:
bash -x ./cleanhouse.sh

This should help you pinpoint where in the script it's choking at......The '-x' option tells bash to print out each command as it's being executed, so it should stop where the error is....... :-)

NOTE: Each command output on the screen will be prepended with a plus sign (+blah-blah) to show the difference between the scripts commands and normal output.....


Top
 Profile  
 PostPosted: Wed Sep 20, 2006 10:37 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
thank you :D the script is fixed now (also edited the post that had the script to reflect the fixes).

it was a missing " and ; now fixed and working.

still wondering if theres a way to hide the errors that show when theres no files to move.


Top
 Profile  
 PostPosted: Wed Sep 20, 2006 11:31 pm   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
katasuka wrote:
thank you :D the script is fixed now (also edited the post that had the script to reflect the fixes).

it was a missing " and ; now fixed and working.

still wondering if theres a way to hide the errors that show when theres no files to move.

N/P :) .........glad you found the problem....

As for hiding errors, the way to do that is to generate a list of all the files being looked for on-the-fly, and then move any files found.......This is easily done by piping an 'ls' command to a 'while' loop, like so:
Code:
ls *.<ext> | while read, do mv $REPLY <target dir>; done

That way, if there are no files found with the matching extension, the while loop has nothing to do and the script simply moves on to the next command........ :)

And, if you don't mind, here's a coding tip I like to do..........Replace all those consecutive 'echo' commands with a single "here-document".........This will also reduce the chance of forgetting a quote, since there will be less quotes in the script ;-)....

So, recoding the "Script Starting" intro would look like this:
Code:
cat<<_EOF_
Script Starting.

Greetings $USER! Welcome to my cleanup script.
Depending on the speed of your computer
and how many files you have to move
and their size, this could take awhile.

_EOF_
read -p "Are you ready (yes or no)? " ANSWER

Notice I also replaced the 'echo -n' command followed by 'reply' using only a 'reply -p' command....'reply -p' does the same thing as 'echo -n' followed by 'reply'.....Read up on the bash manpage about here-documents and the 'read' builtin command

This is mainly a personal choice, and not to be taken as the "correct" way........To me it's a bit easier to read and less prone to missing a quote (believe me, this is a not uncommon error when coding)........

HTH :)
---thegeekster


PS: You can also replace all those 'mkdir' commands with a single one:
Code:
mkdir -p $MP3DIR $OGGDIR $TEXTDIR $VIDDIR

And, redirecting ouput to the 'bit bucket' (/dev/null) should not be necessary, as 'mkdir -p' shouldn't output anything, whether the directory exists or not.... The -p switch will suppress errors if the directory does exist, as documentated in the manpage.......


Last edited by thegeekster on Mon Sep 25, 2006 2:55 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Mon Sep 25, 2006 4:22 am   
User avatar

Joined: Mon Jul 03, 2006 8:58 pm
Posts: 52
Location: Rochester, NY
You can also take the loop one step further and simply list all the files in the directory and only act on the ones you're interested in:

Code:
ls | while read; do
   case $REPLY in
   (*.mp3)
       mv $REPLY <mp3 dir>;;
   (*.ogg)
       mv $REPLY <ogg dir>;;
   # ... and so on
   # for any types that aren't in a case statement, they will be left alone
   esac
done


Top
 Profile WWW  
 PostPosted: Mon Sep 25, 2006 2:42 pm   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
BrionS wrote:
You can also take the loop one step further and simply list all the files in the directory and only act on the ones you're interested in...

Good call, Brion....much cleaner and easier to sort using 'case'.... :)


Top
 Profile  
 PostPosted: Sun Dec 24, 2006 4:30 pm   

Joined: Mon Sep 18, 2006 1:16 pm
Posts: 27
well last night when updating and testing this script i noticed one problem that i hope someone knows how to fix.

i was updating the script to include more filetypes. when i went to test it with a lot of files i found out that files with spaces in their names dont get moved. is there a way around this?


Top
 Profile  
 PostPosted: Mon Dec 25, 2006 2:04 pm   
User avatar

Joined: Mon Jul 03, 2006 8:58 pm
Posts: 52
Location: Rochester, NY
Ah, you've run into the old "bad" filename problem. Filesnames with spaces, quotes, apostrophes, parentheses and other "dirty" characters make life a hassle for scripts.

I suggest reading over this thread that discusses this problem.


Top
 Profile WWW  
 PostPosted: Mon Dec 25, 2006 5:25 pm   

Joined: Mon Jun 19, 2006 10:26 pm
Posts: 3
and if you get this error "argument list to long"

run
Code:
$ find /tmp -maxdepth 1 -name "*" -print0 |xargs -0 rm -f


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP