2016-02-11 62 views
-1

目录我正在寻找这样的事情查找文件的数量和文件名,忽略了find命令

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; 

我应该怎么改?该解决方案的

回答

0

并非所有outputlines将变量被抓住,所以使用${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 
0

这应该工作:

$ find . -type d -print0 | xargs -0 -I {} sh -c ' echo "{}: \c" ; find {} -maxdepth 1 -type f | wc -l ; find {} -maxdepth 1 -type f -print' 

UPDATE

这一个删除不需要的路径...

$ 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#.*/##" ' 
+0

这一个也增加了文件名中的目录部分,我不想要 – user3897102

+0

@ user3897102:ok了解并更正 – mauro