【问题标题】:List files recursively with folder and file size使用文件夹和文件大小递归列出文件
【发布时间】:2020-06-01 19:02:19
【问题描述】:

我想以纯文本形式分享包含文件名和大小的目录摘要。

This thread 显示具有人类可读文件大小的文件列表,例如在 macOS 上:

$ 杜 -h -d 10 14G ./190803 啊啊啊 13G ./190804(无声音)

This post 列出了一个带有漂亮树的文件结构:

$查找。 -打印 | sed -e 's;[^/]*/;|____;g;s;____|; |;g' . |____.DS_Store |____190803 啊啊啊 | |____190803 啊啊.mp4 | |____DSC_0011.MOV | |____DSC_0012.MOV | |____DSC_0013.MOV | |____DSC_0014.MOV | |____DSC_0015.MOV | |____DSC_0016.MOV |____190804(没有声音) | |_____DSC0018.MOV | |_____DSC0019.MOV | |_____DSC0020.MOV | |_____DSC0021.MOV | |_____DSC0022.MOV | |_____DSC0023.MOV | |____DSC_0017.MOV

如何在后一个树形显示中将两者结合起来并在每个项目、文件或文件夹旁边显示人类可读的文件大小?

【问题讨论】:

    标签: shell directory filesize ls


    【解决方案1】:

    试试这个:

    find . -print0 | 
      xargs -0 -I% du -h "%" | 
      awk ' { 
          size = $1 
          $1 = "" 
          print $0, "(" size ")" }
      ' | 
      sed -e 's;[^/]*/;|____;g;s;____|; |;g'
    

    如果您希望文件大小在该行的前面,您可以试试这个:

    find . -print0 | xargs -0 -I% du -h "%" | awk ' { size = $1 ; $1 = ""; print $0, size }' | sed -e 's;[^/]*/;|____;g;s;____|; |;g' | awk ' {size=$NF ; $NF=""; printf("(%5s) %s\n", size, $0) }'
    

    print0-0 处理带引号的案例文件路径,如 Getting error "xargs unterminated quote" when tried to print the number of lines in terminal

    【讨论】:

    • @miguelmorin - 看起来 xargs 不喜欢文件名中的空格和括号。我应该知道的更好。我对帖子进行了编辑以使其更具可读性,并在 xargs 传递给 du -h 的参数周围添加了一些引号。
    • @miguelmorin - 让我们尝试使用这个解决方案stackoverflow.com/questions/11649872/… - 我将更新上面的两个命令来处理引号
    • @miguelmorin - 在测试中,我在这样的目录中创建了一个文件:> can't。这破坏了原始脚本 - 我收到了 unterminated quote 消息。但是,添加 -print0 和 -0 消除了错误。你应该很高兴。
    【解决方案2】:

    你可以试试这个:

    for f in $(ls -R); do wc -c $f;done
    

    【讨论】:

    • 我在第一个目录中收到此错误:wc: 190803: open: No such file or directorywc: Aaah: open: No such file or directory。在该目录上仅运行 wc -c 会出现此错误:wc: 190803 Aaah/: read: Is a directory
    猜你喜欢
    • 2012-05-29
    • 2018-06-27
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 2018-12-17
    • 2020-06-23
    • 2012-06-16
    相关资源
    最近更新 更多