I want to use rm -rf to remove files/folders inside a folder, but skip or ignore a specific folder.

It has been suggested as duplicated to another quesiton, but that is not the case. The other quesiton ask about skipping files. I want to skip a folder with all the content of that folder.

  • 1
    Possible duplicate of How to delete all files in a directory except some? – Steven Nov 11 '15 at 17:59
  • not a duplicate. The other quesitons asks about skipping files. I need to skip a folder with the content of that folder. – Daniel Benedykt Nov 11 '15 at 18:05
  • 1
    What did you already try? Did you know that find accepts both -type f for files but also -type d for directories? – Hennes Nov 11 '15 at 18:12
  • so I tried the following: find . ! -name .svn The problem is that it returns the files inside the .svn folder anyway. If I add -type d , then nothing is returned – Daniel Benedykt Nov 11 '15 at 18:36

It is kind of a duplicate of link in comments... In any case, for a dir:

find . ! \( -type d -and -iname "test" \)

To see whether test dir is skipped or not.


find . ! \( -type d -and -iname "test" \) -delete

Simply find . ! -iname "test" for both file and dir.

  • Not working for me. If I have a folder "test" and inside a "file.test", it still returns the "file.test" in the list. – Daniel Benedykt Nov 11 '15 at 18:54
  • Please re-read and retry. – SΛLVΘ Nov 11 '15 at 19:22
  • Ok, back to this. If you want to skip "file.test" you can't use "test". Try find . ! -iname "*test", wildcard * means "0 or more chars". – SΛLVΘ Nov 12 '15 at 14:58

find . -not -path ./.svn/* ! -name .svn

Essentially, the first part excludes all files within that directory, and the second part excludes the directory itself.



find . -mindepth 1 -maxdepth 1 -not -name '.svn' -print0 | xargs -0 -r rm -rf

does what you want it to do, I believe. It skips the .svn directory and its contents but deletes everything else, files and directories, including those starting with a '.' in the current directory and any subdirectories.

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.