2

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?

Alex
  • 8,471
  • 26
  • 75
  • 99
  • 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 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](http://linux.die.net/man/1/lsattr) that are preventing you from removing the files. – user9517 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

6 Answers6

9

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/*
mulaz
  • 10,682
  • 1
  • 31
  • 37
  • 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
1

I ran across this also when running

sudo rm /tmp/*

the issue is how wildcards are handled when using sudo. See this discussion for details: https://stackoverflow.com/questions/31558944/why-wildcard-doesnt-work-in-sudo-rm-statement

The gist is you need a shell with sudo privileges: sudo sh -c 'rm /tmp/*' should do it

MMartinATX
  • 11
  • 1
1

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....

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
mike
  • 11
  • 1
1

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 {} \;
else
 echo The problem, Alex, is that sudo "isn't" actually giving you root.
fi
0

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

rookie coder
  • 121
  • 1
-8

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
    os.system(cmd)
Alex
  • 8,471
  • 26
  • 75
  • 99
  • 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. – phemmer 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