【问题标题】:Listing filenames and timestamps in subdirectories recursively in UNIX在 UNIX 中递归地列出子目录中的文件名和时间戳
【发布时间】:2016-10-11 03:48:17
【问题描述】:

我一直使用的命令是“ls -lR”。结果通常如下所示:

.:                                                                              
total 4                                                                         
lrwxrwxrwx    1 root     root             9 Oct 11 03:35 dos -> /root/dos       
drwxr-xr-x    2 root     root            80 Oct 11 03:35 folder1                
drwxr-xr-x    2 root     root           100 Oct 11 03:35 folder2                
-rw-r--r--    1 root     root           242 Oct 11 03:35 hello.c                

./folder1:                                                                      
total 0                                                                         
-rw-r--r--    1 root     root             0 Oct 11 03:25 file1001               
-rw-r--r--    1 root     root             0 Oct 11 03:35 file1002               

./folder2:                                                                      
total 0                                                                         
-rw-r--r--    1 root     root             0 Oct 11 03:39 file2001                   
-rw-r--r--    1 root     root             0 Oct 11 03:45 file2002                   

如何优化命令使其只显示以下内容?

./folder1:                                                                      
  Oct 11 03:25 | file1001               
  Oct 11 03:35 | file1002               

./folder2:                                                                                                                                              
  Oct 11 03:39 | file2001                   
  Oct 11 03:45 | file2002                     

【问题讨论】:

    标签: shell unix command-line


    【解决方案1】:

    以下是可行的方法:

    ~/mydir ls -lR | grep -vi "total" | egrep -o "^\.\/.*|^\..*|([A-Z])\w+\s+[0-9]+\s+[0-9]+\s+.*"
    .:
    Sep  4  2015 es_jira.py
    Sep  4  2015 es_slurp_jira.py
    Aug 21  2015 __init__.py
    ./plugins:
    Sep  4  2015 __init__.py
    Sep  4  2015 lrt_report.py
    Sep 11  2015 mr_fingerprint.py
    Mar  6  2016 mr_tunable.py
    Dec  1  2015 plugin.py
    Dec  1  2015 test
    Dec  1  2015 utils.py
    ./plugins/test:
    Sep  4  2015 _test_ca_space_plugin.py
    Sep  4  2015 _test_lrt_report_plugin.py
    Sep  4  2015 _test_mr_failover_plugin.py
    Sep  4  2015 _test_mr_fingerprint_plugin.py
    Dec  1  2015 _test_mr_tunable_plugin.py
    Sep  4  2015 _test_spacedays_plugin.py
    

    如果您想开始为嵌套行和内容添加选项卡,您正在寻找一个脚本和变量工作,这在单行中是可行的,但比快速而肮脏的 grep 更复杂。

    【讨论】:

    • 是否可以在时间戳和文件名之间添加分隔符(例如 |)?另外,我还想排除根文件夹中的文件(即只显示子文件夹的内容。谢谢!
    • 是的,但您需要研究 sed/awk - 两者都可以做分隔符,并且 sed 只能在根文件夹之后的文件夹中获取内容,但这是一个更长的教程比我可以在评论中解释的要多,而且语法很麻烦。
    猜你喜欢
    • 2016-07-11
    • 2021-01-05
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多