2013-03-04 19 views
2

我已经使用bash完成了它,但是如何使用tcsh获取名称或路径到此子目录。后来我需要计算这个子目录中所有文件的总大小,请帮助我。如何查找某些目录的子目录,其中包含大部分文件

例如:

someDirectory
--firstDirectory
---- file11.txt
---- file12.txt
---- file13.txt
--secondDirectory
---- file21.txt
--thirdDirectory
---- file31.txt
---- file32.txt
---- file33.txt
---- file34.txt
---- file35.txt

而作为结果,我想路径thirdDirectory,因为它有最文件。

更新

击的解决方案

#!/bin/bash -f 

declare -i maxcount=0 
declare -i count 
declare maxdirectory 

find someFolder -type d | while read DIR; 
    do let count=`(ls $DIR | wc -w)` 
    if(($count > $maxcount)); then 
     let maxcount=count 
     maxdirectory="$DIR" 
    fi 
done 

更新

TCSH的解决方案

cd $1 
foreach x(*) 
    if(-d $x) then 
    set count = `(ls $x -1 | wc -l)` 
     if($count > $maxcount) then 
      set directory = $x 
      set maxcount = $count 
     endif 
    endif 
end 
+0

你怎么会在bash做呢? 'du'是一个很好的命令... – 2013-03-04 15:55:33

+0

Fredrik,在我计算文件的总大小之前,我需要获取文件夹中的数据,但我不能用'tcsh'来完成,我必须使用所有这些'tcsh' – Procurares 2013-03-04 16:09:36

回答

0

你可以做这样的事情:

foreach f (`find someFolder -type d`) 
    echo `find $f -maxdepth 1 -type f | wc -w` $f >> tmp 
end 
set a = `sort -rn tmp | head -n1` 
set num = $a[1] 
set dir = $a[2] 
2

您可以使用切割:

find . | rev | cut -d "/" -f2- | rev | sort | uniq -c | sort -k1n,1 | tail -n1 

或者AWK:

find . | awk -F "/" '{for(i=1;i<=NF-1;i++) printf $i"/"; printf "\n"}' | sort | uniq -c | sort -k1n,1 | tail -n1 

在标识目录中的文件大小,你可以得到:

du -s 

或:

ls -lh | head -n1 

有人问过类似的问题,但不幸的是它被关闭:In Linux, how do I find find directory with the most subdirectories or files?

添加型F查找,如果你只想计算文件,而不是目录。

0

counter = -1;

因为我在ls -ltr | grep ^d | awk '{ print $NF }'

count=`ls -l $i | grep ^- | wc -l` 

    if [[ $count -gt $counter ]]; then 
      counter=$count 
      Directory=$i 
    fi 

呼应$目录

相关问题