Linux- 使用 grep 查找文件

Linux- finding files with grep

我是 Linux 的新手,我真的不知道自己做错了什么。 我需要在目录 /usr/sbin 中找到名称中包含 'fs' 且不以 'x' 开头的文件。 我需要在 111.txt 文件中写入结果,但不能使用 find 来执行此操作。 我试过这个命令,但它不起作用。

grep -r -v '^x' -w 'fs' /usr/sbin/ > 111.txt 

您可以通过管道将 find 放入两个链接的 grep 命令中:

find /usr/sbin/ | grep 'fs' | grep -v '^/usr/sbin/x' > 111.txt

grep 不是那个意思,你应该使用 find 代替:

find /usr/sbin -type f -name '*fs*' -not -name 'x*' > 111.txt

尝试

shopt -s extglob
printf '%s\n' /usr/sbin/@(fs*|[^x]*fs*) >111.txt
  • shopt -s extglob 启用“扩展的 globbing”,它支持像 @(pattern1|pattern2) 这样的模式。见 extglob section in glob - Greg's Wiki.

  • 这个答案最初是建议的

      # BAD CODE.  DON'T USE.
      printf '%s\n' /usr/sbin/[^x]*fs* >111.txt
    

    这被破坏了,因为它排除了名称以 fs.

    开头的文件