没有路径无法保存文件名,结合 grep 和 awk?

can't save file name without path , combination with grep and awk?

find /user/stc/tmp -mmin -200 -ls | grep '.txt' | awk '{print , , , }' >> tempresult.txt

首先我是新手, 我试图找出上周(home/user/temp)修改过的 .txt 文件并将结果保存到文件中。 所以我是怎么想出来的,但我需要帮助来只保存没有路径的文件名。 我可以用

打印它
awk '{print $NF}'

但是当我结合上面的命令尝试它时它没有按预期工作。

任何 help/suggestion/Improvement 非常感谢。 提前谢谢你。

该管道对于任务而言过于复杂。

find 支持所有过滤和格式化支持,您无需 grep/awk 管道即可完成所需的操作。 (更不用说管道 grepawk 几乎从一开始就没有必要。)

尝试:

find /usr/stc/tmp -mmin -200 -a -name '*.txt' -printf '%P\n'
  • -a 组合 find 说明符(尽管这也是默认操作)
  • -name '*.txt' 至 select 仅匹配所需名称格式的文件
  • -printf 到 select 输出格式
  • '%P\n' 输出找到的文件的名称,其中发现文件的命令行参数被删除(后跟一个换行符)。

如果您实际上只是指文件 name(根本没有路径组件),那么您需要 %f 格式字符串而不是 %P(谢谢Xorg).

find /user/stc/tmp -mmin -200 -printf "%f\n" > log_file

  find /user/stc/tmp -mtime -7 -printf "%f\n" > log_file

..

-mtime -7 # 最近 7 天

%f 将忽略文件所在的任何子目录。

根据用户请求进行编辑

 find /user/stc/tmp -mtime -7 -printf "%Td-%Tm-%TY %Tr %TY %f\n" > log_file


%Td-%Tm-%TY        #modification time: day-month-year
%Tr                #modification date time
%TY                #modification date year

另请注意 linux 跟踪修改日期而不是创建日期...