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.

rm -rf /some/path/* deletes all non-hidden files in that dir (and subdirs).

rm -rf /some/path/.* deletes all hidden files in that dir (but not subdirs) and also gives the following error/warning:

rm: cannot remove directory: `/some/dir/.'
rm: cannot remove directory: `/some/dir/..'

What is the proper way to remove all hidden and non-hidden files and folders recursively in a target directory and also not receive the warning/error about . and ..?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

You could always send error messages to /dev/null

rm -rf /some/path/.* 2> /dev/null

You could also just

rm -rf /some/path/
mkdir /some/path/

...then you won't have to bother with hidden files in the first place.

share|improve this answer
    
Deleting and remaking the directory seems to be the most straightforward approach. Thanks! –  Jakobud May 28 '13 at 4:13
    
But what if I only want to delete the hidden files? –  CMCDragonkai Jun 1 at 13:52
    
@CMCDragonkai that should be a different question, but you can find the solution in Gilles' answer (rm ..?* .[!.]* should do it). –  evilsoup Jun 1 at 18:14

Either change the dotglob option of your shell and use *, or use something like find.

find somedir -mindepth 1 -delete
share|improve this answer
2  
Or you could simply rm -rf /some/dir and then create a new empty directory in its place. –  tripleee May 26 '13 at 12:11

* matches all non-dot-files, .[!.]* matches all dot files except . and files whose name begins with .., and ..?* matches all dot-dot files except ... Together they match all files other than . and ... If any of these three patterns matches nothing, it expands to itself; rm -f doesn't care about non-existent arguments, so this doesn't matter.

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

You can also use find. This is more complex but has the advantage of working even if there are so many files that the wildcards above would expand beyond your system's command line length limit.

find . -name . -o -prune -exec rm -- {} +

If your find has -delete or -mindepth and -maxdepth, you can shorten this to

find . -name . -o -prune -delete
find . -mindepth 1 -maxdepth 1 -delete

You may find it clearer to remove and recreate the directory. This has the advantage (or downside, as the case may be) of resulting in an empty directory even if another program is concurrently creating files in the original directory.

share|improve this answer

Find is your friend.

find ! -name '.' ! -name '..' -delete

% find ! -name '.' ! -name '..'
./test
./test4
./test4/.test6
./test3
./.test5
./test2
% find ! -name '.' ! -name '..' -delete    
% find ! -name '.' ! -name '..'     
%             

If you wish to use recursively search something other your current directory ($PWD), then add a path right after the find command; e.g., find /path ! -name '.' ! -name '..' -delete. If you only want to descend n number of directories, then use the -maxdepth n option right after the /path parameter.

The above command was tested on an Ubuntu 13.04 system. Will likely work on other, modern linux systems.

share|improve this answer

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.