In a Linux command line is it possible to - without prompting - recursively remove files (like with rm -r [path]), but preserve directories under [path]?

EDIT: For the interested, I've used Paul's answer below to build this handy script for use in my Makefiles (replace $(DIRS) with the paths to be cleaned - e.g. bin and build directories):

    @if [ $$(find bin -type f | wc -l) -gt 0 ]; \
    then { \
        echo "The following will be deleted:"; \
        echo "------------------------------"; \
        find $(DIRS) -type f; \
        echo "------------------------------"; \
        read -p "Continue (y/n)? " -n 1 -r CONTINUE; \
        echo; \
    }; \
    else echo "No files to delete."; \
    fi; \
    if [[ $$CONTINUE =~ ^[Yy]$$ ]]; \
    then find $(DIRS) -type f -delete; \

Yes, you can use find:

find [path] -type f -delete

Run it without delete first to make sure it is targetting the right files. type f means only look for files, not directories.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.