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 directories3)
-mtime +3 =
Stuff that is at least 3 days old4)
-print0 =
The execution command, prints the full file name to output followed by a null character5)
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
Useful Reference: find Purpose: search for files in a directory hierarchy
Manual:
Online here or run # "man find" from shell
xargsPurpose: build and execute command lines from standard input
Manual:
Online here or run # "man xargs" from shell
rmPurpose: removes files or directories
Manual:
Online here or run # "man rm" from shell