I do "sudo rm *" and it does not remove everything. (I probably have lots of objects in there which I don't need.)

I'm not asking whether it's ok to remove everything in tmp - i'm asking how to do it :)

Is there a shell script that can do it?

  • 3
    Is this a serious question ? This is extremely basic stuff that even the most junior sysadmin should be able to work out by reading the man pages. Speak to your manager about some training, you really need it. – user9517 supports GoFundMonica May 27 '12 at 19:45
  • 2
    If it's so easy, please help me answer it. I did "sudo rm * -rf" , and there are still objects in there. Please be kind enough to help me , if it's so easy for you. – Alex May 27 '12 at 20:03
  • You have been provided with the answer of how to do it. Why it doesn't work is a different matter altogether. It's likely that there are permission or attributes that are preventing you from removing the files. – user9517 supports GoFundMonica May 27 '12 at 20:36
  • Were the files that are "still there" actually there before the rm? That is, if you still have processes running they may still be creating files in /tmp/. – mpez0 May 27 '12 at 21:34
  • If there are files still there, there's probably a very good reason for that. Stop and learn why. Post what files are still there so we can help you understand why they're there. – Andy Lester Nov 25 '12 at 19:11

You shouldn't do that, since there may be unix sockets in use (or needed) there, etc.

But still, if you wish:

rm -rf /tmp/*
  • 1
    ...where r stands for recursive, meaning to delete the folders and their content too and f stands for force, never prompt anything. – Silviu May 27 '12 at 19:20
  • I tried this, but it doesn't work. (I even "sudo" before it). After I type "ls" , there is still a lot of stuff there. – Alex May 27 '12 at 20:01
  • 1
    "sudo rm -rf /tmp/*; ls" ...still lists objects in there (pictures, mainly) – Alex May 27 '12 at 20:02
  • @Alex Then pick one of those files (not a directory) that didn't get deleted, sudo rm thatfile without any options, and update the question with whatever error you get. If you don't get an error, did it work? – DerfK May 27 '12 at 20:44

As other posters have mentioned, it's hard to answer an uncooperative asker, but this is the closest I can get to an answer that'll probably work without you actually giving more details as to what specifically is going wrong. It gets around the possibility of an issue with glob expansion (is "*" expanding as root or user? is it expanding to beyond the shell's (rather large) builtin limit?) or with current-directory (are you still in /tmp after the sudo?).

if `sudo id` | grep uid=0; then
  sudo find /tmp/ -maxdepth 1 \! -name . -exec rm -rf {} \;
 echo The problem, Alex, is that sudo "isn't" actually giving you root.

First, I am not savy enough to advise anyone on this matter, However, I can post what I have done in the past to address these issues.

sudo -- rm -rf /tmp/
sudo -- cd /tmp/
sudo -- chmod 777 /tmp

then restart server, all the needed tmp files will be rewritten into /tmp/ by system upon restart...

Including needed /tmp/ files by any website software, etc., on the server.

This will permanently remove any /tmp/session files and free up much space....


Maybe the question was not so stupid, if the problem was related to hidden files. See here for more details: https://askubuntu.com/questions/72446/how-to-remove-all-files-and-subdirectories-in-a-directory-without-deleting-the-d


I solved it myself. Thank you for all your extremely helpful, non-condescending, wonderful help, @lain.

import sys
import os
import fileinput

path = '/tmp/'
listing = os.listdir(path)
for li in listing:
    cmd = 'sudo rm -rf /tmp/' + li
    print cmd
  • 6
    This is insane overkill. You were asked for additional info which you didnt provide, thus nobody was able to explain why you were having issues. – Patrick May 27 '12 at 21:33
  • 7
    This is a bad idea, because it's a security hole. Someone could have created a file with a ; in its name, for example. – James Youngman May 27 '12 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.