【问题标题】:Using find and xargs to list the timestamp of a file which has found from a containing string使用 find 和 xargs 列出从包含字符串中找到的文件的时间戳
【发布时间】:2019-06-25 16:49:40
【问题描述】:

我必须从内部给定的字符串中找到一个文件,并仅在 linux 命令终端中使用 find 和 xargs 命令列出该文件的时间戳

我已经尝试过这段代码,它成功了,但它在 ls 命令上给出了长行错误,需要更流畅的答案来解决这个特定问题

find /home/stajyer -type f -exec grep -H 'ayberk' {} \; |xargs ls -l

结果:

ls: cannot access '/home/stajyer/myNameChangedFile/aFileInChangedFile4:ayberk': No such file or directory
ls: cannot access 'Binary': No such file or directory
ls: cannot access 'file': No such file or directory
ls: cannot access 'matches': No such file or directory
-rw------- 1 stajyer stajyer 9787 Haz 25 09:45 /home/stajyer/.bash_history

我的代码如何减少与 ls 命令相对应的错误?

【问题讨论】:

  • 看来你只是想从grep输出文件名,为什么不直接使用grep -l呢?有关详细信息,请查看手册页。

标签: linux timestamp xargs


【解决方案1】:

来自man xargs

xargs 读取 标准输入中的项目,由空格分隔(可以是 用双引号或单引号或反斜杠保护)或换行符,

要禁用它,您可以使用零分隔流并将-0--null 选项与xargs-print0find 一起使用。它看起来像这样:

find /home/stajyer -type f -exec grep -qH 'ayberk' {} \; -print0 | xargs -0 ls -l

repl 上测试。

【讨论】:

  • 在你的新代码中,它给出了 ls: cannot access '/home/stajyer/myNameChangedFile/aFileInChangedFile4:ayberk'$'\n''/home/stajyer/myNameChangedFile/aFileInChangedFile4': No这样的文件或目录ls:无法访问'二进制文件/home/stajyer/.bash_history 匹配'$'\n''/home/stajyer/.bash_history':没有这样的文件或目录正在消除最后一个时间戳
  • 我把你的改成了 find /home/stajyer -type f -exec grep -H 'ayberk' {} \; -print0 | xargs ls -l 现在给出:正确的时间,但错误太多
猜你喜欢
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 2021-12-24
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
相关资源
最近更新 更多