我正在寻找这样的东西
test1 : 2
aaa.txt
bbb.txt
test2 : 3
ababa.txt
cbbab.txt
ddddd.txt
test3 : 1
mmmm.txt
但我当前的代码也列出了该目录。如何删除输出中的目录
这是我的代码
find /tmp/test/ -maxdepth 2 -mindepth 1 -type d | while read dir; do printf "%s : " "$dir"; find "$dir" -maxdepth 1 -type f | wc -l; find "$dir" -maxdepth 1 -type f ; done;
我应该改变什么?
最佳答案
并非所有输出行都会被捕获在变量中,因此使用 ${var##*/}
从 var 中删除路径将不起作用。因此,只需使用 sed 切断完整输出的路径即可:
find /tmp/test/ -maxdepth 2 -mindepth 1 -type d |
while read dir; do
printf "%s : " "$dir"
find "$dir" -maxdepth 1 -type f | wc -l
find "$dir" -maxdepth 1 -type f
done | sed 's#.*/##'
当您想要更好的布局时,可以使用以下结构:
find /tmp -maxdepth 2 -mindepth 1 -type d | while read dir; do
# Delete path from dir with ##*/
printf "%s : " "${dir##*/}"
find "$dir" -maxdepth 1 -type f | wc -l
# Replace path with some spaces
find "$dir" -maxdepth 1 -type f | sed 's#.*/# #'
# Redirect the "permission denied messages" to the end of the Galaxy.
done 2>/dev/null
关于linux - 在 find 命令中查找文件数和文件名,忽略目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349567/