jlp jlp - 9 months ago 30
Linux Question

How to delete files under subdirectories but not deleting subdirectories themselves in linux

I have the following directory structure:


I want to delete all files under /archive but not its subfolders, so after deletion, the directory structure should look like:


I have tried the following two commands, but the files under the subfolders are not deleted (ie. my.txt and your.txt), anyone know why ?

find -L /archive ! -type d -exec rm -rfv {} +
find -L /archive -type f -exec rm -rfv {} +


use find

$ find . ! -type d -delete

make sure you're in the right path.