【问题标题】:Exclude some files from variable in find command在 find 命令中从变量中排除一些文件
【发布时间】:2021-02-16 03:53:35
【问题描述】:

我想在 find 命令中排除一些文件,但效果不是很好!这是我所做的:

arg=$1
exclude="/etc/alternatives/awk 
    /usr/sbin/poweroff 
    /usr/bin/ls"

find $arg -type f,l,c,b -path /etc/alternatives/awk -prune -o -print

在这个示例中,我只使用了一个文件 /etc/alternatives/awk,但我不知道如何告诉脚本在我的变量 exclude 中执行操作。

更新:

我应该只使用这种结构吗?

find $arg -type f,l,c,b -path /usr/bin/pwd -prune -o -path /etc/alternatives/awk -prune -o -path /usr/sbin/poweroff -prune -o -path /usr/bin/ls -prune -o -print

【问题讨论】:

标签: bash for-loop awk sed find


【解决方案1】:

发布的thread 有多种选择,各有优缺点。 之一(最慢的):

#!/bin/bash
arg=$1
exclude='^/etc/alternatives/awk$|^/usr/sbin/poweroff$|^/usr/bin/ls$'

find $arg -type f,l,c,b | grep -Ev "${exclude}"

或者您可以尝试在本地使用 find 而不使用 grep-ing:

#!/bin/bash
arg=$1
exclude='^(/etc/alternatives/awk|/usr/sbin/poweroff|/usr/bin/ls)$'

find $arg -type f,l,c,b -regextype posix-extended -not -regex "${exclude}" -print

【讨论】:

  • 最好使用这种模式:exclude='^/etc/alternatives/awk$|^/usr/sbin/powerof$|^/usr/bin/ls$'
  • egrep 已弃用,取而代之的是 grep -E 但如果任何路径包含 .*?[,该 grep 命令将解释正则表达式元字符,因此 YMMV或( 或类似的 - 你真的应该使用grep -F -f '<list the paths in a file or here doc or here string or similar>'。 @M.J ^ab$|^cd$ = ^(ab|cd)$
  • 谢谢。你能用你提到的更好的方式重写脚本吗?甚至可以作为单独的答案
  • @Vgersh99 是的,就是这样! +1
猜你喜欢
  • 2017-11-15
  • 2019-06-06
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
相关资源
最近更新 更多