Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing and deleting files 1

Status
Not open for further replies.

taneujin

MIS
Aug 14, 2001
61
SG
Hi, I have a set of log files which are in different directories with different dates. I would like to list all the files created 1 month ago and then delete only those files which are in several different directories every day. Can someone help me with the command as I have just started with Linux recently. Thank you.
 
Have all the files got a common extension (eg .html). If they have, and they reside on the same filesystem, you could use find as follows, from the top level directory above your log directories:

find . -name '*.html' -mtime +31 -print |pg

This will list the files. If you're really sure you want to delete them, replace the -print command with -exec rm {} \; and take out the |pg at the end. Be careful with rm, however, it's a very dangerous command in this and any other context. You could add an -i option to the rm so that you are prompted for each file to be deleted, but this may not be practical if there are hundreds.

If this does not suit your requirements (eg because the files aren't on the same filesystem), you could write a script to cd to the appropriate directories and do individual finds and deletes on them as above. Hope this makes sense - post again if you require further info. Cheers.

 
Just wondering, the months are not all 31 days. How can I set it to all files that are 30 or 60 days old from the time I start the find command? Thanks
 
Glad to help. Just change the 31 to 30 or 60 as required. Apologies if this caused any confusion, I just used 31 for illustrative purposes. You can have as many or as few days specified as you wish. Incidentally, the -mtime option finds the files based on their modification time - in the case of log files this is usually whenh they are created. HTH.
 
Hi,
Well, you can make a simple script and the n run that script at anytime which will automatically delete the log files.You can even run this script automatically if you want.Go to Super user scheduled tasks in Linuxconf and add a rule with the timing.
Helpful commands:-
ls -tru #This gives a list of old and unused files.
Note:After deleting the log files you need to activate that file again.Do this
touch filename

regards
Krischrist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top