首页 > 解决方案 > 如何在使用查找时列出没有绝对路径的目录中的所有文件及其文件大小

问题描述

GNU bash,版本 4.4.0
Ubuntu 16.04

我想列出目录中的所有文件并将它们打印到第二列,同时在第一列中打印文件的大小。例子

1024 test.jpg
1024 test.js
1024 test.css
1024 test.html  

我已经使用该ls命令完成了此操作,但 shellcheck 不喜欢它。例子:

In run.sh line 47:
ls "$localFiles" | tail -n +3 | awk '{ print $5,$9}' > "${tmp_input3}"
^-- SC2012: Use find instead of ls to better handle non-alphanumeric filenames.  

当我使用该find命令时,它还返回绝对路径。例子:

root@me ~ # mkdir -p /home/remove/test/directory
root@me ~ # cd /home/remove/test/directory && truncate -s 1k test.css test.js test.jpg test.html && cd
root@me ~ # find /home/remove/test/directory -type f -exec ls -ld {} \; | awk '{ print $5, $9 }'
1024 /home/remove/test/directory/test.jpg
1024 /home/remove/test/directory/test.js
1024 /home/remove/test/directory/test.css
1024 /home/remove/test/directory/test.html  

实现我的目标最有效的方法是什么。它可以是任何命令,只要 shellcheck 很酷,我很高兴。

标签: bashfindshellcheck

解决方案


请试试:

find dir -maxdepth 1 -type f -printf "%s %f\n"

推荐阅读