12

rm -rf * will remove all files and folders but not hidden ones.

rm -rf .* will remove all hidden files and folders, but not those that are not hidden, correct?

What command should one type, in order to delete all files and folders including the hidden ones? (except . and ..); ?

4 Answers 4

15
rm -rf * .*

will do the trick.

7
  • 1
    NO! .* will expand to all invisible entries in the current directory including .. -- which means that using .* as an argument to rm -rf will attempt to delete the directory above the one you're currently in, and all of its contents. rm will refuse to do it, but I don't consider relying on this safe... May 19, 2012 at 15:54
  • @Gordon Davisson: I've created a directory. I've added files to it, including hidden files. I've make that command. It has deleted all the contents inside that directory recursively, and it haven't deleted any content ABOVE that directory. Can you please clarify your comment ?
    – MEM
    May 19, 2012 at 15:58
  • Thanks for the clarification on your comment above. Now I understand your point. Any alternative suggestion then ?
    – MEM
    May 19, 2012 at 16:01
  • how about using find with the -delete flag... May 19, 2012 at 16:04
  • 1
    @GordonDavisson rm should print a rm: "." and ".." may not be removed message in that case – it'll not automatically remove the parent directory.
    – slhck
    May 20, 2012 at 13:57
1

Try this:

find . -mindepth 1 -delete

I'm not certain that -mindepth is supported by all implementations of find; if yours doesn't have it, -path should give another way to keep it from trying to delete .:

find . -path "*/*" -delete
1
  • BSD find has that option too, yup.
    – slhck
    May 20, 2012 at 13:58
1

Just use this command to purge files/directories according to specific criteria:

# tmpwatch -fauv 0 .
0

shopt -s dotglob also makes * include files that start with a period.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .