Find path in bash on insensitive manner

前端 未结 2 1174
天涯浪人
天涯浪人 2021-01-26 09:52

Suppose a path like

/home/albfan/Projects/InSaNEWEBproJECT

Despite of the fact to not use such that names. Is there a way to check for a path

相关标签:
2条回答
  • 2021-01-26 10:23

    The simplest solution:

    $ find . | grep -qi /path/to/something[^/]*$ 
    

    But if you have some additional conditions that must be checked for matched file, you can run grep inside find:

    $ find . -exec sh -c 'echo {} | grep -qi /path/to/something' \; -print
    

    Here you will get all files that are in the directory. If you want to get only the directory's name:

    $ find . -exec sh -c 'echo {} | grep -qi /path/to/something[^/]*$' \; -print
    

    Example of usage:

    $ mkdir -p Projects/InSaNEWEBproJECT/src/main/resources/
    $ find . -exec sh -c 'echo {} | grep -qi /projects/insanewebproject[^/]*$' \; -print
    ./Projects/InSaNEWEBproJECT
    
    0 讨论(0)
  • 2021-01-26 10:25

    My fault! I guess I tried

    find -ipath 'projects/insanewebproject' 
    

    but the trick here is that I must use

    find -ipath './projects/insanewebproject'
    

    That ./ does the change. Thanks!.

    man says -path is more portable than -wholename

    if you expect only one result, you can add | head -n1, cause that way head kill pipe when it fills its buffer, which is only one line length

    find -ipath './projects/insanewebproject'| head -n1
    
    0 讨论(0)
提交回复
热议问题