Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在/etc中显示名称中包含数字的配置文件(使用grep)_Bash_Grep - Fatal编程技术网

Bash 在/etc中显示名称中包含数字的配置文件(使用grep)

Bash 在/etc中显示名称中包含数字的配置文件(使用grep),bash,grep,Bash,Grep,任务(12)来自 使用grep我可以按如下方式解决此任务: grep --no-message -l [[:alnum:]] /etc/* | grep [[:digit:]] 获得了类似的结果: ls -o /etc/ | grep ^- | awk '{print $8}' | grep [[:digit:]] 但是我想递归地读取每个目录下的所有文件。这就是我能做的: grep --no-message -lR [[:alnum:]] /etc/* | grep [[:digit:]]

任务(12)来自

使用grep我可以按如下方式解决此任务:

grep --no-message -l [[:alnum:]] /etc/* | grep [[:digit:]]
获得了类似的结果:

ls -o /etc/ | grep ^- | awk '{print $8}' | grep [[:digit:]]
但是我想递归地读取每个目录下的所有文件。这就是我能做的:

grep --no-message -lR [[:alnum:]] /etc/* | grep [[:digit:]]

今天如何解决这个问题?这是正确的方法吗?您可以提供什么样的额外解决方案来解决此任务?

格里沙和安德烈给出了完美的答案。但是,如果您正在寻找一种使用
grep
的解决方案,请点击这里

while read -r filepath; do
  # grep for digits in the basename so that we choose only those
  # files that have a digit in them, not in any parent directory name
  if grep -q '[[:digit:]]' <<< "$(basename $filepath)"; then
    echo "$filepath"
  fi
done < <(find /etc -type f -print)
读取文件路径时;做
#grep用于basename中的数字,以便我们只选择那些
#包含数字的文件,而不是任何父目录名中的文件

如果grep-q'[:digit:]'您可以使用
find

find /etc -type f -name \*[[:digit:]]\*
find /etc -name '*[[:digit:]]*'
请注意,此命令仅列出名称中带有数字的文件,而不列出完整路径。对于后者,请使用不同的过滤器:

find /etc -type f -path \*[[:digit:]]\*

你不应该仅仅为了获取文件名而使用grep。i、 而不是我认为你正在试图做的事情

grep --no-message -l [[:alnum:]] /etc/*
你可以这么做

echo /etc/*
由于目标是列出带有数字的文件,因此可以使用类似于以下的glob:

echo /etc/*[[:digit:]]*
如果要递归地执行此操作,可以在bash中执行,如:

shopt -s globstar
echo /etc/**/*[[:digit:]]*
或者您可以使用
查找

find /etc -type f -name \*[[:digit:]]\*
find /etc -name '*[[:digit:]]*'

如果任务是使用纯
grep
解决方案,那么它就是:

grep -laR . /etc | grep '^.*/[^/]*[[:digit:]][^/]*$'
正则表达式:

  • ^…$
    涵盖整行内容
  • */[^/]*…[^/]*
    只查找文件名

  • 是否要搜索文件内容或文件名?我要搜索文件名。请注意,
    echo
    在一行上列出所有文件,而不是在
    find
    之间换行。管道
    echo
    输出到
    xargs-nX
    以查看X列中的输出。@kurgulus请检查我的第二个答案