Command to remove all files and folders recursively including, hidden ones, on MAC OS Terminal?

10

2

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 ..); ?

MEM

Posted 2012-05-19T15:16:50.560

Reputation: 907

Answers

13

rm -rf * .*

will do the trick.

user1055604

Posted 2012-05-19T15:16:50.560

Reputation: 1 485

1NO! .* 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... – Gordon Davisson – 2012-05-19T15:54:21.183

@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 – 2012-05-19T15:58:53.657

Thanks for the clarification on your comment above. Now I understand your point. Any alternative suggestion then ? – MEM – 2012-05-19T16:01:21.610

how about using find with the -delete flag... – user1055604 – 2012-05-19T16:04:11.497

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 – 2012-05-20T13:57:37.240

@slhck: right, it's the "should" that makes me nervous. It's a bit like saying "the gun's safety should keep it from shooting my foot off when I do this..." – Gordon Davisson – 2012-05-20T17:39:59.120

@GordonDavisson Well, it does print the message on OS X, I don't know about GNU though and of course you're right. Better be safe than sorry. – slhck – 2012-05-20T17:41:00.477

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

Gordon Davisson

Posted 2012-05-19T15:16:50.560

Reputation: 28 538

BSD find has that option too, yup. – slhck – 2012-05-20T13:58:38.890

1

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

# tmpwatch -fauv 0 .

Antoine Nguyen

Posted 2012-05-19T15:16:50.560

Reputation: 11

0

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

Lri

Posted 2012-05-19T15:16:50.560

Reputation: 34 501