jlp jlp - 1 year ago 87
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 {} +

Answer Source

use find

$ find . ! -type d -delete

make sure you're in the right path.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download