34

This is something I used to do a lot on Windows, but after my recent fiasco I want to make sure. Is it safe to do

sudo rm -rf /tmp/*

?

ripper234
  • 31,763
  • Is this is an ever running server, or a desktop machine with daily shutdown? – user unknown Apr 13 '11 at 17:29
  • It can cause various problems on your system, but mainly it will work. – peterh Apr 30 '17 at 18:48
  • Related: https://askubuntu.com/questions/380238/how-to-clean-tmp – Ciro Santilli OurBigBook.com Apr 02 '19 at 11:13
  • I would like to ask why you want to delete files in /tmp? Is it because it is filling up, or is it for privacy reasons? If it's a space issue, it may be due to a badly behaved application, or it may be that it needs to be on a larger partition. If privacy, there are other alternatives, such as using encrypted filesystems that you may want to consider. – Rob Apr 13 '11 at 21:56

6 Answers6

22

In general, no.

If it's filling up with junk, you may want to look at what software isn't cleaning up after itself.

You can also use find to identify files which haven't been modified or accessed in a long time that are probably safe to delete.

Rob
  • 443
  • 10
    Concur with the chorus of 'no' here, and the addendum that most linux distros automatically clean out /tmp/ on startup. While this doesn't necessarily help if we're talking about a no-reboots-EVER server here, one possibility might be running find ./ -type f -atime 14 -exec rm {} \; every week or so to delete any plain file that hasn't been accessed at all in the past two weeks... – Shadur-don't-feed-the-AI Apr 13 '11 at 11:32
  • find ./ -type f -atime 14 -delete with gnu-find – user unknown Apr 13 '11 at 17:30
  • @user this of course assume that /tmp isn't using the current kernel defaults which is relatime only update when the file is modified, or noatime. In which case atime isn't going to be remotely helpful. – xenoterracide Apr 17 '11 at 08:27
7

The real answer is - it depends. /tmp may be used by applications that require lockfiles or temporary logs to be present in order to run, or it may not. There may be symlinks in there...not sure what for, but it's always possible.

You should really look at what is in there before you decide to remove it. doing an rf -rf * on anything is inherently dangerous.

Rory Alsop
  • 2,063
  • 15
  • 30
  • 4
    On my system, logfiles go to /var/log, lockfiles to /var/lock. – user unknown Apr 13 '11 at 17:27
  • 3
    @user sure, for system daemons... this doesn't necessarily mean that there aren't locks or logs for a users application in /tmp because the normal user can't write to /var/log|lock I have a shell script that writes a lock to /tmp – xenoterracide Apr 17 '11 at 08:27
7

No. For example, if you have a MySQL database running on your computer that will kill its socket, or if you are using emacs as a server that will kill the server process. There are many other cases where it is not safe to remove these files. The best thing to do is to write a script which checks the date of the file and only removes it if it is old.

APerson
  • 335
  • 2
  • 9
6

Please use tmpreaper.

2

No.

But you could a ramdisk for the /tmp dir then it would be empty after every reboot of the system. And as a side effect your system may become a little big faster.

Google has a lot on info on tmpfs and/or ramfs.

Johan
  • 4,583
  • Is it not common to clear /tmp automatically on restart? Gentoo does, and I think Ubuntu does, but it's been a while since I used it – Michael Mrozek Apr 13 '11 at 17:11
  • No I don't think the Debian derived distros like Ubuntu does it (right now anyway). But it is quite easy to add this if you like it. – Johan Apr 13 '11 at 18:31
  • Ubuntu Matty (or whatever the current release is called) does. But if you don't reboot your machine regularly, it won't happen. – Rob Apr 13 '11 at 21:53
  • Ok, did not check the current. I'm using the LTS track right now. – Johan Apr 14 '11 at 06:25
  • Ubuntu does, and every Linux I tested before, did: Knoppix, Peanut, Halloween, Red Hat, SuSe. Why would it make the system faster? What exactly would be faster? Calculations, disk access, networking, boot up? – user unknown Apr 17 '11 at 10:23
  • A lot of different sw use temp files in the tmp dir, read/write times to a tmpfs is the same as to ram (since it is ram). Otherwise those access would have the read/write times of a normal harddrive with is way slower. So every time you have a app that use a temp files, you would have faster access for those actions. – Johan Apr 17 '11 at 20:10
-3

My suggestion is to rename your tmp folder first to see what's being affected by this directory. Rename it from "tmp" to "old_tmp". Also, create a new empty folder with name "tmp" because may some setup or system process require this folder to create some files inside this directory like log files. Follow these steps for secure removing of tmp date.

1- Rename existing "tmp" directory as "old_tmp". 2- Create new empty "tmp" directory 3- Assign all permission as "0777" to this newly created directory so the system/setup can have permission to create/place log and other files inside this directory. 4- Run system and applications to make sure everything is running fine and behaving as usual. Keep in observation for 2-3 days. 5- If sure nothing is affected due to renaming your "tmp" directory to "old_tmp" then you can delete "old_tmp" directory.

Note: If anything causing issue's due to renaming "tmp" directory to "old_tmp" then let this directory back to its original "tmp" name.

  • 2
    This is a really bad idea. There are many standard shell utilities that write temporary files into /tmp and that would start failing as soon as you rename the original /tmp. Also note that the /tmp directory should have specific permissions (sticky bit set, for example). – Kusalananda Jul 05 '18 at 09:25
  • Please read carefully what I said: You're right there might be many standard shell utilities that write temporary files into "/tmp" directory that's why I said to create a new "tmp" directory and assigned complete permission to that after renaming old existing "tmp" directory. – A.Aleem11 Jul 06 '18 at 07:24
  • 1
    Yes, and you said the permissions should be 0777 when in fact they should be 1777. – Kusalananda Jul 06 '18 at 07:37