Use find command but exclude files in two directories

前端 未结 6 1073
刺人心
刺人心 2021-01-29 22:16

I want to find files that end with _peaks.bed, but exclude files in the tmp and scripts folders.

My command is like this:

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 22:55

    Here's how you can specify that with find:

    find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*"
    

    Explanation:

    • find . - Start find from current working directory (recursively by default)
    • -type f - Specify to find that you only want files in the results
    • -name "*_peaks.bed" - Look for files with the name ending in _peaks.bed
    • ! -path "./tmp/*" - Exclude all results whose path starts with ./tmp/
    • ! -path "./scripts/*" - Also exclude all results whose path starts with ./scripts/

    Testing the Solution:

    $ mkdir a b c d e
    $ touch a/1 b/2 c/3 d/4 e/5 e/a e/b
    $ find . -type f ! -path "./a/*" ! -path "./b/*"
    
    ./d/4
    ./c/3
    ./e/a
    ./e/b
    ./e/5
    

    You were pretty close, the -name option only considers the basename, where as -path considers the entire path =)

提交回复
热议问题