How to clean up the /tmp folder contents

What is tmp directory in linux ?


Each Linux/Unix system has a directory /tmp which is mounted with separate file system. temporary directory is a directory used to hold temporary files. Many operating systems and some software automatically delete the contents of this directory once their process is got completed. /tmp folders having all the website session files which is hosted on server. so if we deleted those file wrongly the user session got disconnected who is currently accessing databases. Some times the /tmp directory got full, so you can’t write anything to mysql database and you will be get some error messages. Now you should delete something from /tmp directory to get free.

So first, i’m going to check my /tmp directory to identify what kind of files is stored often.

[email protected] [/tmp]# ls -lh
total 11M
drwxrwxrwt 15 root        root        508K May  6 13:26 ./
drwxr-xr-x 29 root        root        4.0K May  3 03:32 ../
-rw-------  1 jobtardi    jobtardi    1.8K May  6 04:48 sess_01677a0944c23187135748f09037f13a
-rw-------  1 jobtarin    jobtarin      31 May  6 13:22 sess_0292d1af394d15f09e5565af76304acd
-rw-------  1 jobtardi    jobtardi       0 May  6 11:20 sess_02e024372f9c054207419d85db55db9d
-rw-------  1 jobtarin    jobtarin       0 May  6 13:15 sess_040410bca2c158b8fe7bead4030a293c
-rw-------  1 jobtardi    jobtardi       0 May  6 11:06 sess_0432ff3d27a403aa9ee48e459dabf77f
-rw-------  1 jobtarin    jobtarin      54 May  6 13:15 sess_0441d357e26d661ca11ab8a8a43ee945
-rw-------  1 jobtardu    jobtardu       0 May  6 08:30 sess_051e1579f9f430999002e3f97d6678c8
-rw-------  1 lungiwor    lungiwor       0 May  6 13:16 sess_056bf5b3e9f890f1bf077f4bc521b107

In my server /tmp folder having only session file, so i’m going to remove only session files.

Simple shell script for session clean ?

For this requirement i’m going to create the simple shell script and assign to cron once in a day.

#!/bin/bash

rm -rf /tmp/sess_*
/sbin/service mysql restart

Just create the shell file called tmpclean.sh at /etc/cron.daily folder and put the above codes. It will clean the session files once in a day and restart the mysql service. Make sure your shell file should be full permission (755) so that it will be executable.

How to clean /tmp folder without affecting anything ?

Many script generates session files after every seconds. You can use mtime argument with the find command to search out little bit old files say 1 hour older. So if you delete them it wont cause much effect for your website.

#!/bin/bash

find /tmp/sess_* -mtime +1h -exec rm {} \;

Just create the shell file called tmpclean.sh at /etc/cron.hourly folder and put the above codes. It will clean the session files every one hour. Make sure your shell file should be full permission (755) so that it will be executable.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

  • JDWired

    This is an awesome tutorial and easy to setup. Thank you for sharing this. It has really helped a lot and saved space from clutter.

Close
Please support the site
By clicking any of these buttons you help our site to get better