Shell Scripting


  I`ve kinda lost touch with my shell scriprting and might have to write a shell script in the near future to go through the files in a directory and delete any file older than 45 days old. This is actually on a solaris5.6 machine. If anyone can help with pointers, tips, links to good sites and if possible the code itself , that would be great. I will be trying to write it myself of course but if all else fails(as i said i`m on a tight schedule), i want something to fall back on.

Help will be appreciated

Who is Participating?
qiang8Connect With a Mentor Commented:

echo "Removing old files in /home/user/data ... "
cd /home/user/data

for file in `find . -mtime +15 -print`
   if [ -f $file ]
     echo "Removing $file that existed more than 15 days."
     rm $file
     #use mv command to some temp dir for testing  

You can cron this script (Bash shell) and perform a periodical clean up on your unwanted data. Cheers and good luck.
You could probably use the find command to do a search and destroy mission.
something like:  #find /directorytolookin  -mtime +45 -exec rm {} \;
I would test it using "ls -la" instead of "rm" first to make sure it does what you want it to do.

Look in your man page for the exact details of your implimenation: #man find
You may have to use something other than -mtime.

For tutorials go to and to a search for something like: unix shell tutorial

infy_geekAuthor Commented:
Thanks giang. Your help was really useful. My boss suddenly made up his mind that he wnted the script today itself. Youre a life saver.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.