linux排除文件名是,linux – 从bash shell中的find中排除文件扩展名列表

我想为我的make文件编写一个清理例程,除了我文件夹中的必要源文件之外,它会删除所有内容.例如,我的文件夹包含具有以下扩展名的文件:.f .f90 .F90 .F03 .o .h .out .dat .txt .hdf .gif.

我知道我可以用以下方法完成此任务

find . -name \( '*.o' '*.out' '*.dat' '*.txt' '*.hdf' '*.gif' \) -delete

使用否定,我可以这样做:

find . -not -name '*.f*' -not -name '*.F*' -not -name '*.h' -delete

但是,当我尝试这样做时:

find . -not -name \( '*.f*' '*.F*' '*.h' \)

我收到一个错误:

find: paths must exceed expression: [first expression in the above list]

(在这种情况下,我会得到:

find:路径必须超出表达式:* .f *

)

你能解释为什么会这样,以及如何做我想做的事情?每次我想在列表中添加文件扩展名时,我都讨厌编写-not -name.另外,我想找出为什么这会给我一个错误,这样我就可以更好地学习Linux.

谢谢!

解决方法:

find . -not -name \( '*.f' '*.F' '*.h' \)

被解释为

find

. # path to search

-not # negate next expression

-name \( # expression for files named "("

'*.f' '*.F' .'*.h' \) # more paths to search?

导致错误.

由于这些是单字母扩展,您可以将它们折叠为单个glob:

find . -not -name '*.[fFh]'

但如果它们更长,你必须写出全球

find . -not -name '*.f' -not -name '*.F' -not -name '*.h'

要么

find . -not \( -name '*.f' -o -name '*.F' -o -name '*.h' \)

或切换到使用正则表达式.

find . -not -regex '.*\.(f|F|h)$'

请注意,find中的正则表达式不是POSIX标准的一部分,可能并非在所有实现中都可用.

标签:linux,makefile,find

来源: https://codeday.me/bug/20190823/1700303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值