beg*_*nsh 2 linux bash shell awk
我在目录中有一组文件.我想从ls输出中提取没有绝对路径和修改时间戳的文件名.
/apps/dir/file1.txt
/apps/dir/file2.txt
Run Code Online (Sandbox Code Playgroud)
现在从ls输出我提取出文件名和时间戳的字段
ls -ltr /apps/dir | awk '{print $6 $7 $8 $9}'
Sep 25 2013 /apps/dir/file1.txt
Dec 20 2013 /apps/dir/file2.txt
Dec 20 2013 /apps/dir/file3.txt
Run Code Online (Sandbox Code Playgroud)
而我希望它像
Sep 25 2013 file1
Dec 20 2013 file2
Dec 20 2013 file3
Run Code Online (Sandbox Code Playgroud)
一个解决方案可以是cd到那个目录并从那里运行命令,但有没有CD的解决方案?我也使用substr(),但由于文件名的长度不相等,所以将常量值传递给substr()函数并没有成功.
使用GNUfind,您可以执行以下操作来获取没有路径的文件名:
find /apps/dir -type f -printf "%f\n"
Run Code Online (Sandbox Code Playgroud)
正如Kojiro在评论中提到的,您可以使用%t或%T(format)获得修改时间.
或像BroSlow建议的那样做
find /apps/dir -printf "%Ab %Ad %AY %f\n"
Run Code Online (Sandbox Code Playgroud)
不要尝试执行以下操作(它将在具有空格的文件名中断,甚至跨越不同的操作系统,其中ls -l表示具有更少/更多列:
ls -ltr /apps/dir | awk '{n=split($9,f,/\//);print $6,$7,$8,f[n]}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4974 次 |
| 最近记录: |