-1

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?](http://superuser.com/questions/529854/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

3 Answers3

1

This:

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.

Vojtech
  • 1,212
  • 9
  • 6
0

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.

Then:

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

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

SΛLVΘ
  • 1,389
  • 1
  • 11
  • 13
0

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

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

Alex
  • 166
  • 13