Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to remove all content of the current working directory. How to do it in efficient and elegant way?

I have several files, directories and some of them starts with dot, like:

touch .abc; touch abcd ; mkdir foobar; touch "file name" "#"

I am looking for posix compatible, elegant solution, to make this directory empty. I will post some of my ideas, but none of them are perfect.

share|improve this question
    
d=$(basename `pwd`); cd .. && rm -rf ./${d} && mkdir ${d} && cd - –  devnull Apr 11 '13 at 12:46
    
@devnull: fails if you don't have perms to remove CWD. –  Michał Šrajer Apr 11 '13 at 13:13
    
It was assumed that you have perms on the directory. Nevertheless, this should work regardless d=$(basename `pwd`); cd .. && mktemp -p ./${d} && rm -rf ./${d} && mkdir ${d} && cd - –  devnull Apr 11 '13 at 13:36
add comment

8 Answers

rm -rf -- * .*

This is portable and works, but it's ugly, because except with zsh, it issues warnings:

rm: cannot remove directory: `.'
rm: cannot remove directory: `..'
share|improve this answer
    
Yeah, you can adapt with stuff like [^.] but then you'll have other problems (like folders named ..abc); so, I doubt you can get rid of these nasty warnings. It's not a "clean" option, but it works. –  Tom Wijsman Apr 11 '13 at 10:07
    
@TomWijsman, [^.] is not POSIX, [!.] is. –  Stéphane Chazelas Apr 11 '13 at 10:46
    
@StephaneChazelas: If you need POSIX, then convert the syntax, I don't care... –  Tom Wijsman Apr 11 '13 at 11:04
add comment

Try this:

find . -maxdepth 1 -not -name \. -exec rm -rf \{\} ;
share|improve this answer
1  
-maxdepth and -not are not posix. ! is. –  Stéphane Chazelas Apr 11 '13 at 10:38
add comment

POSIXly:

rm -rf -- * .[!.]* ..?*

That one could fail if the list of files it too big, this one wouldn't:

find . ! -name . -prune -exec rm -rf {} +

With zsh:

rm -rf -- *(D)
share|improve this answer
    
Stéphane, you rock! –  Michał Šrajer Apr 11 '13 at 10:57
add comment

Best solution I found is to:

find . -delete

however, -delete predicate is risky (see man) and I don't like to use it.

Update:

I don't like -delete predicate because it can cause a disaster when used by someone not familiar with the details of find's logic. For example:

find . -delete -name '*.bak'  # DO NOT DO IT!!!

Someone may expect such call to remove only '*.bak' files, but it is exact equivalent to find . -delete.

share|improve this answer
    
This should be the best option; I don't see why it would be risky, it's logic to put it at the end and you can first run it without that to see what it would delete. –  Tom Wijsman Apr 11 '13 at 10:05
    
@TomWijsman in this case -delete is fine, but in general -delete is risky, this is why I don't like it. –  Michał Šrajer Apr 11 '13 at 10:22
1  
-delete is not POSIX. –  Stéphane Chazelas Apr 11 '13 at 10:37
1  
What about -exec rm -rf {} \;? –  Tom Wijsman Apr 11 '13 at 11:01
add comment

how about

 rm -rf * .[a-z]* .[A-Z]* .[0-9]*

will that work ? or you have other file name patterns to match as well ?

share|improve this answer
    
in posix filesystem, almost any character can be in the filename. Try touch '#'. –  Michał Šrajer Apr 11 '13 at 10:03
1  
Restricting to certain characters isn't really reliable; you're missing the often used . and - for instance, but even including these you may come across a _ or other less frequent characters. –  Tom Wijsman Apr 11 '13 at 10:08
add comment

another option is to remember value of pwd, cd .., rm -rf the directory and recreate the directory. This is also ugly for many reasons.

BTW, please do not down-vote. Comment if needed.

share|improve this answer
add comment

First delete files, then empty directories:

find . -type f -exec rm \{\} \;
find . -depth -type d -empty -exec rmdir \{\} \;
share|improve this answer
    
this "trivial task" is getting crazy... ;-) –  Michał Šrajer Apr 11 '13 at 10:28
    
-empty is not POSIX. -type f is for regular files only. You would miss other types of files like symlinks, sockets, pipes, doors, devices... Use ! -type d instead. –  Stéphane Chazelas Apr 11 '13 at 10:52
add comment
rm -rf .

This of course does not work at all, however this:

rm -rf -- "$PWD"

removes CWD, and we end up within nonexisting directory. This is very ugly.

BTW, please do not down-vote. Comment if needed.

share|improve this answer
    
Well, this does not remove the "content" of a directory. –  Tom Wijsman Apr 11 '13 at 10:06
    
@TomWijsman. Agree. This is why it would need recreation of the directory. Yet another ugly solution... –  Michał Šrajer Apr 11 '13 at 10:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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