在 find 命令中查找忽略目录的文件数和文件名
Find the file count and file name ignoring directories in the find command
我正在寻找这样的东西
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;
我应该改变什么?
部分解决方案在这里找到:https://superuser.com/questions/620570/how-do-i-return-only-file-names-from-the-find-command
尝试将 find . -type f -exec basename {} \;
命令添加到您的第一个查找查询中。
这应该有效:
$ find . -type d -print0 | xargs -0 -I {} sh -c ' echo "{}: \c" ; find {} -maxdepth 1 -type f | wc -l ; find {} -maxdepth 1 -type f -print'
更新
这个删除不需要的路径...
$ find . -type d -print0 | xargs -0 -I {} sh -c ' echo "{}: \c" ; find {} -maxdepth 1 -type f | wc -l ; find {} -maxdepth 1 -type f -print | sed "s#.*/##" '
并不是所有的输出行都会被变量捕获,所以使用 ${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
我正在寻找这样的东西
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;
我应该改变什么?
部分解决方案在这里找到:https://superuser.com/questions/620570/how-do-i-return-only-file-names-from-the-find-command
尝试将 find . -type f -exec basename {} \;
命令添加到您的第一个查找查询中。
这应该有效:
$ find . -type d -print0 | xargs -0 -I {} sh -c ' echo "{}: \c" ; find {} -maxdepth 1 -type f | wc -l ; find {} -maxdepth 1 -type f -print'
更新
这个删除不需要的路径...
$ find . -type d -print0 | xargs -0 -I {} sh -c ' echo "{}: \c" ; find {} -maxdepth 1 -type f | wc -l ; find {} -maxdepth 1 -type f -print | sed "s#.*/##" '
并不是所有的输出行都会被变量捕获,所以使用 ${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