![](/img/trans.png)
[英]Shell - create zip files in directories without including subdirectories
[英]Shell: list directories ordered by file count (including in subdirectories)
我幾乎達到了Linux主目錄中允許的文件數量限制,我很好奇所有文件的位置。
在我可以使用的任何目錄中find . -type f | wc -l
find . -type f | wc -l
find . -type f | wc -l
顯示該目錄及其子目錄中有多少文件的數量,但我想要的是能夠生成所有子目錄(和子子目錄等)的完整列表,每個子目錄的計數都是包含在其中的所有文件及其子目錄 - 如果可能,按計數排序,降序。
例如,如果我的文件結構如下所示:
Home/
file1.txt
file2.txt
Docs/
file3.txt
Notes/
file4.txt
file5.txt
Queries/
file6.txt
Photos/
file7.jpg
輸出將是這樣的:
7 Home
4 Home/Docs
2 Home/Docs/Notes
1 Home/Docs/Queries
1 Home/Photos
任何建議都非常感謝。 (也是對答案的快速解釋,所以我可以從中學習!)。 謝謝。
我使用以下命令
find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n
產生的東西如下:
[root@ip-***-***-***-*** /]# find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n
1 .autofsck
1 stat-nginx-access
1 stat-nginx-error
2 tmp
14 boot
88 bin
163 sbin
291 lib64
597 etc
841 opt
1169 root
2900 lib
7634 home
42479 usr
80964 var
這應該工作:
find ~ -type d -exec sh -c "fc=\$(find '{}' -type f | wc -l); echo -e \"\$fc\t{}\"" \; | sort -nr
說明:在上面的命令中將運行“find~-type d”來查找home-directory的所有子目錄。 對於它們中的每一個,它運行一個簡短的shell腳本,找到該子目錄中的文件總數(使用您已經知道的“find $ dir -type f | wc -l”命令),並將回顯該數字后跟目錄名稱。 然后運行sort命令以按降序排列文件總數。
這不是最有效的解決方案(你最終會多次掃描同一個目錄),但我不確定你能用一個班輪做得更好:-)
更簡單,更有效:
find ~ -type f -exec dirname {} \; | sort | uniq -c | sort -nr
countFiles () {
# call the recursive function, throw away stdout and send stderr to stdout
# then sort numerically
countFiles_rec "$1" 2>&1 >/dev/null | sort -nr
}
countFiles_rec () {
local -i nfiles
dir="$1"
# count the number of files in this directory only
nfiles=$(find "$dir" -mindepth 1 -maxdepth 1 -type f -print | wc -l)
# loop over the subdirectories of this directory
while IFS= read -r subdir; do
# invoke the recursive function for each one
# save the output in the positional parameters
set -- $(countFiles_rec "$subdir")
# accumulate the number of files found under the subdirectory
(( nfiles += $1 ))
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d -print)
# print the number of files here, to both stdout and stderr
printf "%d %s\n" $nfiles "$dir" | tee /dev/stderr
}
countFiles Home
產生
7 Home
4 Home/Docs
2 Home/Docs/Notes
1 Home/Photos
1 Home/Docs/Queries
find . -type d -exec sh -c '(echo -n "{} "; ls {} | wc -l)' \; | sort -n -k 2
這非常有效。
它將按升序顯示計數(即最后的最大值)。 要獲得它是降序,請將“-r”選項添加到“sort”。
如果在“/”目錄中運行此命令,它將掃描整個文件系統並告訴您包含最多文件和子目錄的目錄是什么。 這是查看所有inode使用位置的好方法。
注意:這對於包含空格的目錄不起作用,但是如果它對您有問題,您可以修改它以適用於該情況。
請參閱以下示例:反向排序第2列。 使用sort -k 2 -r
。 -k 2表示按列2排序(空格分隔),-r表示反向。
# ls -lF /mnt/sda1/var/lib/docker/165536.165536/aufs/mnt/ | sort -k 2 -r
total 972
drwxr-xr-x 65 165536 165536 4096 Jun 5 12:23 ad45ea3c6a03aa958adaa4d5ad6fc25d31778961266972a69291d3664e3f4d37/
drwxr-xr-x 19 165536 165536 4096 Jun 6 06:46 7fa7f957669da82a8750e432f034be6f0a9a7f5afc0a242bb00eb8024f77d683/
drwxr-xr-x 2 165536 165536 4096 May 8 02:20 49e067ffea226cfebc8b95410e90c4bad6a0e9bc711562dd5f98b7d755fe6efb/
drwxr-xr-x 2 165536 165536 4096 May 8 01:19 45ec026dd49c188c68b55dcf98fda27d1f9dd32f825035d94849b91c433b6dd3/
drwxr-xr-x 2 165536 165536 4096 Mar 13 06:08 0d6e95d4605ab34d1454de99e38af59a267960999f408f720d0299ef8d90046e/
drwxr-xr-x 2 165536 165536 4096 Mar 13 02:25 e9b252980cd573c78065e8bfe1d22f01b7ba761cc63d3dbad284f5d31379865a/
drwxr-xr-x 2 165536 165536 4096 Mar 13 02:24 f4aa333b9c208b18faf00b00da150b242a7a601693197c1f1ca78b9ab2403409/
drwxr-xr-x 2 165536 165536 4096 Mar 13 02:24 3946669d530695da2837b2b5ed43afa11addc25232b29cc085a19c769425b36b/
drwxr-xr-x 2 165536 165536 4096 Mar 11 11:11 44293f77f63806a58d9b97c3c9f7f1397b6f0935e236250e24c9af4a73b3e35b/
但是,如果您使用dirname處理非累積解決方案(請參閱wjb的答案),那么效率更高的是:
find ~ -type f -print0 | xargs -0 dirname | sort | uniq -c | sort -n
請注意,這不顯示空目錄。 為此,如果您的find版本支持它,您可能會找到〜-type d -empty。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.