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 without receiving the warning/error about .
and ..
?
find
alternative returns "success" even if some file is not successfully deleted; not good for script. – Franklin Yu Jul 29 '16 at 07:22find
command, the manpage for find states "Because -delete implies -depth, you cannot usefully use -prune and -delete together." -- yet you use-prune -delete
? – Doktor J Aug 18 '16 at 21:29-prune
doesn't do anything here. And on reading back I see that I didn't answer the question correctly: I took care not to recurse, but the question explicitly asks for recursive deletion. I've corrected my answer. – Gilles 'SO- stop being evil' Aug 18 '16 at 21:53find . ! -name . -prune -exec rm -rf {} +
less confusing (even if strictly equivalent) – Stéphane Chazelas Jun 13 '17 at 12:12.[^.]*
instead of.[!.]*
when history substitution is enabled (which by default is the case interactively but not in scripts), because zsh parses!
as a history reference. But in zsh you wouldn't need that in the first place, you can just use*(D)
to include dot files (without.
or..
) in the wildcard match. – Gilles 'SO- stop being evil' Aug 01 '17 at 14:29find
approach:find . -name . -o -prune -exec rm -rf -- {} +
. Is it trying to delete everything in my current directory?!!rm: cannot remove './hobby/outdoors/RACCC WW Beginners Handbook 2016 Final.docx': Permission denied
– sakovias Dec 28 '17 at 15:02rm -rf /path/to/subdirectory/{..?*,.[!.]*,*} 2>/dev/null
– bjd2385 Jun 06 '18 at 07:00rm -rf /home/user/somedir/{,.[^.],..?}*
and i gotzsh: no matches found: /home/user/somedir/..?*
. Then @Gillies i triedrm -rf /home/user/somedir/*(D)
and it worked*(D) to include dot files (without . or ..) in the wildcard match
– Santhosh Sep 07 '19 at 17:34find /repo -mindepth 1 -maxdepth 1 -exec rm -r -- {} +
.-mindepth
to prevent deleting the given directory,-maxdepth
to avoid stuff that's already going to be deleted byrm -r
– mpen Feb 07 '21 at 02:09rm -rf .*
does parent traversal? In any flavour of UNIX? – Torsten Bronger Aug 31 '21 at 08:58..
so they'll only delete children of the current directory. But I wouldn't put it past some older implementations ofrm
to omit this check and therefore delete the current directory and its siblings before they try and fail tormdir("..")
. – Gilles 'SO- stop being evil' Sep 01 '21 at 06:41./*
or-- *
would be safer in case a filename starts with a dash (-
) so that it doesn't get interpreted as an option. – Paul P Mar 30 '23 at 09:42