It appears you have not registered with our community. To register please click here ...

Author Topic: [HOW TO] Use find to clean tmp directories  (Read 1819 times)

dynaweb

  • <b>Canine Deamon</b>
  • Administrator
  • Sr. Member
  • *****
  • Posts: 493
  • Karma: +10/-0
  • Generic personal text here ...
    • MSN Messenger - danno_d_manno@yahoo.com
    • View Profile
    • DynaWeb Designs
    • Email
[HOW TO] Use find to clean tmp directories
« on: October 07, 2005, 11:29:24 PM »
The Linux operating system designates a /tmp directory that is reserved for all users to write to. It may contain information that is important for a short while such as recent uploads, session data, fonts, etc.. If you are administering a system that does not get rebooted often, there is a good chance your /tmp partition could fill up with useless junk. Once at capacity, a full /tmp dir could cause performance problems.
 
If you are having problems with the /tmp directory filling up, then it is probable that your system needs a command to help periodically clean it out. The challenge is that wiping out the whole dir might cause some problems since those files are there for a reason. What is needed is to find the files that are older than X number of days and delete them safely. After all, those older files were only needed on a temporary basis; hence being in the /tmp dir.
 
As a solution, I propose the following "find" commands for keeping your /tmp files clean. Log in to shell terminal and su to root user.
 
See how it looks:
This first command is for testing. It will pick out the files to delete and display them on the screen, but it will not actually clean anything:
[indent]# find /tmp /var/tmp -not -type d -mtime +3 -print0 | xargs --null --no-run-if-empty ls[/indent]Command to clean files:
If you are cool with that stuff being cleaned out, then you can run the real command:
[indent]# find /tmp /var/tmp -not -type d -mtime +3 -print0 | xargs --null --no-run-if-empty rm -f
[/indent]Now you deserve to know what exactly this command does. The preceding command does the following:
[indent]1) find /tmp /var/tmp = Executes the "find" command in the directories /tmp and /var/tmp
2) -not -type d = Excludes directories
3) -mtime +3 = Stuff that is at least 3 days old
4) -print0 = The execution command, prints the full file name to output followed by a null character
5) xargs --null = Executes the output line that ends in a null character
6) --no-run-if-empty = Self explanatory
7) rm -f = Forcefully remove each file that is printed
[/indent]

Command to clean empty directories:
Now that the files are cleaned out, we can delete the empty directories (a.k.a. folders) with the following command:
[indent]# find /tmp /var/tmp -depth -mindepth 1 -type d -print0 | xargs --null --no-run-if-empty rmdir

[/indent]You can add these two commands to your root crontab to run daily. Run the second command a minute later than the first.

Now your dusting chore is taken care of automatically :D


Useful Reference:
 
find
Purpose: search for files in a directory hierarchy
Manual: Online here or run # "man find" from shell
 
xargs
Purpose: build and execute command lines from standard input
Manual: Online here or run # "man xargs" from shell
 
rm
Purpose: removes files or directories
Manual: Online here or run # "man rm" from shell
« Last Edit: January 24, 2007, 08:44:27 PM by dynaweb »
Those who cannot learn from history are doomed to repeat it. -- Linux learns.

zelo

  • Super Moderator
  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 264
  • Karma: +10/-0
    • ICQ Messenger - 233717
    • MSN Messenger - webmaster@zelo.com
    • Yahoo Instant Messenger - zelo@yahoo.com
    • View Profile
    • http://www.zelo.com
    • Email
[HOW TO] Use find to clean tmp directories
« Reply #1 on: January 24, 2007, 07:41:55 PM »
As a side-note, it is VERY important to clean your tmp files as they do not clean themselves and they can become very large and cumersome.

ctwjr

  • Super Moderator
  • Jr. Member
  • **
  • Posts: 72
  • Karma: +10/-0
    • View Profile
    • http://www.ftplive.com
    • Email
[HOW TO] Use find to clean tmp directories
« Reply #2 on: January 24, 2007, 08:43:01 PM »
Quote from: zelo
As a side-note, it is VERY important to clean your tmp files as they do not clean themselves and they can become very large and cumersome.

You\'re not kidding.  I ran a cron to clean the temp files (and error_log files) on a server and saved about 30 Gigs of space!

zelo

  • Super Moderator
  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 264
  • Karma: +10/-0
    • ICQ Messenger - 233717
    • MSN Messenger - webmaster@zelo.com
    • Yahoo Instant Messenger - zelo@yahoo.com
    • View Profile
    • http://www.zelo.com
    • Email
[HOW TO] Use find to clean tmp directories
« Reply #3 on: January 24, 2007, 09:27:28 PM »
One of the best ways to not forget to clean out your temp file is to run a CRON.

 

Related Topics

  Subject / Started by Replies Last post
5 Replies
1331 Views
Last post September 30, 2005, 12:32:11 PM
by dynaweb
0 Replies
180 Views
Last post December 05, 2005, 03:30:27 PM
by Linux News
3 Replies
2674 Views
Last post January 27, 2007, 02:24:46 AM
by ctwjr