Skip to content

Instantly share code, notes, and snippets.

@umayr
Created July 9, 2019 14:38
Show Gist options
  • Save umayr/a66d57194a04fe9bacd7d06fcd7323dc to your computer and use it in GitHub Desktop.
Save umayr/a66d57194a04fe9bacd7d06fcd7323dc to your computer and use it in GitHub Desktop.
Ignore a directory and a file in Find command

Consider the following scenario:

λ tree .                                                                                                                                                         (folder) 10:34:52
.
├── delete-file1
├── delete-file2
├── delete-file3
├── keep-file1
└── keep-folder
    └── keep-file2

1 directory, 5 files

Where you have to delete everything besides this one file and one folder then run the following command:

find . -type f ! -name "keep-file*" -exec rm -f {} \;

# in case your files don't have common pattern
find . -type f ! -name "keep-file1" ! -path "*/keep-folder/*" -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment