bash - 如何在使用查找时列出没有绝对路径的目录中的所有文件及其文件大小
问题描述
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 很酷,我很高兴。
解决方案
请试试:
find dir -maxdepth 1 -type f -printf "%s %f\n"
推荐阅读
- php - 如何建立非安全连接
- python - 在 Flask SQLAlchemy 中修改 Sqlite 数据库结构
- jenkins - 停止后如何在特定端口上启动 Jenkins?
- spring - 如果我尝试使用 lombok 获取布尔 @Transient 变量的值,为什么会出错?
- c# - 将视图拖到其他视图下时出现 WPF 性能问题
- c - 为什么“指向不同数组的指针数组”的符号与传统二维数组的符号相匹配?
- angular - 为什么 ng 命令仅在 Vs 代码和命令提示符下以“管理员身份”工作
- c++ - 如何知道在 C++ 中何时调用 delete 以及何时调用 delete[]?
- reactjs - react redux - 如何重新加载页面而不是“推送”
- amazon-web-services - Amazon DynamoDB LastEvaluatedKey 模式?