I need to make a script to delete files not in use in /tmp

I need to make a script to delete files not in use in /tmp
Thanks!

Were you expecting the solution in your post ? what have you done ?

Does this fit your bill?

 rm -rf /tmp/*

If you want to delete files accessed by yesterday,

A=$(date | awk '{print substr($0,5,7)}')
ls -alu /tmp | egrep -v "${A}|^total" | awk '{print "rm -rf /tmp/"$9}' | sh -

(be careful (rm -rf)...:eek:)
thank you...:slight_smile:

It is not advisable to remove files that have only recently been accessed as it is no guarantee that they are not actually still being used by a process. A 100% safe way is to clean /tmp (and /var/tmp) at boot time. One could use a compromise and determine an age at which a tmp file is deemed no longer in use. Best is to use the standard cleanup scripts that come with most OS'es as an init.d script these days....

Have a good look at what you might be deleting in the context of your own system and its software. There is no general answer to this except as Scrutinizer suggests doing it during a reboot. Certainly confine any deletions to files (i.e. not pipes, directories etc.) and perhaps look at files which are older than your most recent boot to start with. If you have a database engine running be aware of how long it has been up an what files it creates.