What is the command to remove all files but not directories?
What you're trying to do is recursive deletion. For that you need a recursive tool, such as find
.
find FOLDER -type f -delete
With bash
:
shopt -s globstar ## Enables recursive globbing
for f in FOLDER/**/*; do [[ -f $f ]] && echo rm -- "$f"; done
Here iterating over the glob expanded filenames, and removing only files.
The above is dry-run, if satisfied with the changes to be made, remove echo
for actual removal:
for f in FOLDER/**/*; do [[ -f $f ]] && rm -- "$f"; done
Finally, unset globstar
:
shopt -u globstar
With zsh
, leveraging glob qualifier:
echo -- FOLDER/**/*(.)
(.)
is glob qualifier, that limits the glob expansions to just regular files.
The above will just print the file names, for actual removal:
rm -- FOLDER/**/*(.)
If your version of find
doesn't support -delete
you can use the following to delete every file in the current directory and below.
find . ! -type d -exec rm '{}' \;