Shell:按文件数排序列出目录(包括子目录)
我几乎达到了 Linux 主目录中允许的文件数量限制,我很好奇所有文件在哪里。
在任何目录中,我可以使用例如 find 。 -类型 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
非常感谢任何建议。 (也是对答案的快速解释,这样我就可以从中学习!)。谢谢。
I've nearly reached my limit for the permitted number of files in my Linux home directory, and I'm curious about where all the files are.
In any directory I can use for example find . -type f | wc -l
to show a count of how many files are in that directory and in its subdirectories, but what I'd like is to be able to generate a complete list of all subdirectories (and sub-subdirectories etc) each with a count of all files contained in it and its subdirectories - if possible ranked by count, descending.
Eg if my file structure looks like this:
Home/
file1.txt
file2.txt
Docs/
file3.txt
Notes/
file4.txt
file5.txt
Queries/
file6.txt
Photos/
file7.jpg
The output would be something like this:
7 Home
4 Home/Docs
2 Home/Docs/Notes
1 Home/Docs/Queries
1 Home/Photos
Any suggestions greatly appreciated. (Also a quick explanation of the answer, so I can learn from this!). Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我使用以下命令
find 。 -xdev -类型 f |切-d“/”-f 2 |排序| uniq-c| sort -n
会产生类似以下内容的内容:
I use the following command
find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n
Which produces something like:
这应该有效:
解释:
在上面的命令中将运行“find ~ -type d”来查找主目录的所有子目录。对于每个文件,它都会运行一个简短的 shell 脚本,查找该子目录中的文件总数(使用您已知的“find $dir -type f | wc -l”命令),并回显该数字后跟目录名称。然后运行 sort 命令以按文件总数降序排序。
这不是最有效的解决方案(您最终会多次扫描同一目录),但我不确定您可以使用单行程序做得更好:-)
This should work:
Explanation:
In the command above will run "find ~ -type d" to find all the sub-directories the home-directory. For each of them, it runs a short shell script that finds the total number of files in that sub-directory (using the "find $dir -type f | wc -l" command that you already know), and will echo the number followed by the directory name. The sort command then runs to sort by the total number of files, in a descending order.
This is not the most efficient solution (you end up scanning the same directory many times), but I am not sure you can do much better with a one liner :-)
产生
produces
更简单、更高效:
simpler and more efficient:
这非常有效。
它将按升序显示计数(即最大的在最后)。要使其按降序排列,请在“排序”中添加“-r”选项。
如果您在“/”目录中运行此命令,它将扫描整个文件系统并告诉您包含最多文件和子目录的目录。这是查看所有索引节点的使用情况的好方法。
注意:这不适用于包含空格的目录,但如果您遇到问题,您可以修改它以在这种情况下工作。
This is pretty efficient.
It will display the counts in ascending order (i.e. largest at the end). To get it is descending order, add the "-r" option to "sort".
If you run this command in the "/" directory, it will scan the entire filesystem and tell you what are the directories that contain the most files and sub-directories. It's a good way to see where all your inodes are being used.
Note: this will not work for directories that contain spaces, but you could modify it to work in that case, if it's a problem for you.
请参阅以下示例:按第 2 列反向排序。使用
sort -k 2 -r
。 -k 2 表示按第 2 列排序(空格分隔),-r 表示反向排序。See following example: sort by column 2 reversely. Use
sort -k 2 -r
. -k 2 means sort with column 2 (space separated), -r means reverse.但是,如果您对使用 dirname 的非累积解决方案感到满意(请参阅 wjb 的答案),那么迄今为止更有效的是:
请注意,这不会显示空目录。为此你可以这样做
查找 ~ -type d -empty
如果您的 find 版本支持它。
If however you are fine with the non cumulative solution by using dirname (see answer of wjb) then by far more efficient is:
Note that this does not display empty dirs. For that you may do
find ~ -type d -empty
if your version of find supports it.