Linux-使用 grep 查找文件

发布于 2025-01-15 11:55:03 字数 204 浏览 3 评论 0原文

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

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

I'm new to Linux and I don't really know what I'm doing wrong.
I need to find files in catalog /usr/sbin that have 'fs' in their name and don't start with 'x'.
I need to write results in 111.txt file but cannot use find to do this.
I tried this command but it doesn't work.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话已封尘 2025-01-22 11:55:03

您可以将 find 通过管道传输到两个链接的 grep 命令中:

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

You could pipe find into two chained grep commands instead:

find /usr/sbin/ | grep 'fs' | grep -v '^/usr/sbin/x' > 111.txt
岁吢 2025-01-22 11:55:03

grep 不是为此目的,您应该使用 find 代替:

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

grep isn't meant for that, you should use find instead:

find /usr/sbin -type f -name '*fs*' -not -name 'x*' > 111.txt
酒中人 2025-01-22 11:55:03

尝试

shopt -s extglob
printf '%s\n' /usr/sbin/@(fs*|[^x]*fs*) >111.txt
  • shopt -s extglob 启用“扩展通配”,它支持 @(pattern1|pattern2) 等模式。请参阅 extglob 部分 (glob - Greg 的维基

  • 这个答案最初是建议的

     # 错误代码。不要使用。
      printf '%s\n' /usr/sbin/[^x]*fs* >111.txt
    

    这已被破坏,因为它排除了名称以 fs 开头的文件。

Try

shopt -s extglob
printf '%s\n' /usr/sbin/@(fs*|[^x]*fs*) >111.txt
  • shopt -s extglob enables "extended globbing", which supports patterns like @(pattern1|pattern2). See the extglob section in glob - Greg's Wiki.

  • This answer originally suggested

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

    That was broken because it excludes files whose names begin with fs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文