Wednesday, November 30, 2005

HOW TO REMOVE OLD FILES FROM A DIRECTORY IN UNIX

This is a typical which may occur.Say you have a lot of old dump files of manyyy months.And you are worried about space.So obviously you will try to remove files from that directory.But its also cumbersome to do a rm -f and keep on ticking the keyboard.We can solve it using find utility in UNIX.The format of the command may be like :
find . -name "*.gz" -print -mtime +30 -exec rm {} \+
or
find . -name "*.gz" -print -mtime +30 -exec rm {} \;
or
find . -name "*.gz" -print -mtime +30 -ok rm {} \;

Lets dissect the above command--
The three commands telling that remove all gunzipped files which has not been modified for last one month.
-name is matching all the files having *.gz.
-print is printing the whole path of the file.
-mtime +30 means modified before 30 days.
-atime +30 means accessed before 30 days.

If we would like to remove all .zip files also alongwith .gz which has not been accessed for last one month then?

find . / \( -name "*.gz" -o -name '*.zip' \) -atime +30 -exec rm {} \;

Whats the difference between + or ; putting after the command?
Actually + collects all the files to be tremoved,create a set of commands, and then execute.So its much faster.

Why you used OK in place of exec ?
Its just to make familiar with the expression.What OK will do is to confirm from the user whether he wants to delete or not.But surely thats a bad idea as this will be against our intention.

Wats that {} after rm?
This is just for passing arguments to rm.

Labels:

0 Comments:

Post a Comment

<< Home