bash 如何在目录及其子目录中找到最大的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12522269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the largest file in a directory and its subdirectories?
提问by Rekson
We're just starting a UNIX class and are learning a variety of Bash commands. Our assignment involves performing various commands on a directory that has a number of folders under it as well.
我们刚刚开始一个 UNIX 课程,正在学习各种 Bash 命令。我们的任务涉及在一个目录下执行各种命令,该目录下也有许多文件夹。
I know how to list and count all the regular files from the root folder using:
我知道如何使用以下方法列出和计算根文件夹中的所有常规文件:
find . -type l | wc -l
But I'd like to know where to go from there in order to find the largest file in the whole directory. I've seen somethings regarding a du
command, but we haven't learned that, so in the reperttheitroade of things we've learned I assume we need to somehow connect it to the ls -t
command.
但我想知道从那里去哪里才能找到整个目录中最大的文件。我看过一些关于du
命令的东西,但我们还没有学到,所以在我们学到的东西中,我假设我们需要以某种方式将它连接到ls -t
命令。
And pardon me if my 'lingo' isn't correct, I'm still getting used to it!
如果我的“行话”不正确,请原谅我,我还在习惯它!
回答by tamsler
Quote from thislink-
引用自这个链路
If you want to find and print the top 10 largest files names (not directories) in a particular directory and its sub directories
$ find . -printf '%s %p\n'|sort -nr|head
To restrict the search to the present directory use "-maxdepth 1" with find.
$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head
And to print the top 10 largest "files and directories":
$ du -a . | sort -nr | head
** Use "head -n X" instead of the only "head" above to print the top X largest files (in all the above examples)
如果要查找并打印特定目录及其子目录中的前 10 个最大文件名(不是目录)
$ find . -printf '%s %p\n'|sort -nr|head
要将搜索限制在当前目录中,请使用“-maxdepth 1”和 find。
$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head
并打印前 10 个最大的“文件和目录”:
$ du -a . | sort -nr | head
** 使用“head -n X”而不是上面唯一的“head”来打印前 X 个最大的文件(在以上所有示例中)
回答by xpros
To find the top 25 files in the current directory and its subdirectories:
要查找当前目录及其子目录中的前 25 个文件:
find . -type f -exec ls -al {} \; | sort -nr -k5 | head -n 25
find . -type f -exec ls -al {} \; | sort -nr -k5 | head -n 25
This will output the top 25 files by sorting based on the size of the files via the "sort -nr -k5" piped command.
这将通过“sort -nr -k5”管道命令根据文件的大小进行排序来输出前 25 个文件。
Same but with human-readable file sizes:
相同但具有人类可读的文件大小:
find . -type f -exec ls -alh {} \; | sort -hr -k5 | head -n 25
find . -type f -exec ls -alh {} \; | sort -hr -k5 | head -n 25
回答by nneonneo
find . -type f | xargs ls -lS | head -n 1
outputs
产出
-rw-r--r-- 1 nneonneo staff 9274991 Apr 11 02:29 ./devel/misc/test.out
If you just want the filename:
如果您只想要文件名:
find . -type f | xargs ls -1S | head -n 1
This avoids using awk
and allows you to use whatever flags you want in ls
.
这避免了使用awk
并允许您在ls
.
Caveat. Because xargs
tries to avoid building overlong command lines, this might fail if you run it on a directory with a lot of files because ls
ends up executing more than once. It's not an insurmountable problem (you can collect the head -n 1
output from each ls
invocation, and run ls -S
again, looping until you have a single file), but it does mar this approach somewhat.
警告。因为xargs
试图避免构建过长的命令行,如果您在包含大量文件的目录上运行它,这可能会失败,因为ls
最终会执行多次。这不是一个无法解决的问题(您可以收集head -n 1
每次ls
调用的输出,然后ls -S
再次运行,循环直到您有一个文件),但它确实在某种程度上损害了这种方法。
回答by ghoti
This lists files recursively if they're normal files, sorts by the 7th field (which is size in my find
output; check yours), and shows just the first file.
如果它们是普通文件,这将递归列出文件,按第 7 个字段(这是我find
输出中的大小;检查您的)排序,并仅显示第一个文件。
find . -type f -ls | sort +7 | head -1
The first option to find
is the start path for the recursive search. A -type of f
searches for normal files. Note that if you try to parse this as a filename, you may fail if the filename contains spaces, newlines or other special characters. The options to sort
also vary by operating system. I'm using FreeBSD.
第一个选项find
是递归搜索的起始路径。一种f
搜索普通文件的类型。请注意,如果您尝试将其解析为文件名,如果文件名包含空格、换行符或其他特殊字符,则可能会失败。选项sort
也因操作系统而异。我正在使用 FreeBSD。
A "better" but more complex and heavier solution would be to have find
traverse the directories, but perhaps use stat
to get the details about the file, then perhaps use awk
to find the largest size. Note that the output of stat
also depends on your operating system.
“更好”但更复杂和更重的解决方案是find
遍历目录,但可能用于stat
获取有关文件的详细信息,然后可能用于awk
查找最大大小。请注意, 的输出stat
还取决于您的操作系统。
回答by Kalpana
There is no simple command available to find out the largest files/directories on a Linux/UNIX/BSD filesystem. However, combination of following three commands (using pipes) you can easily find out list of largest files:
没有简单的命令可以在 Linux/UNIX/BSD 文件系统上找出最大的文件/目录。但是,结合以下三个命令(使用管道),您可以轻松找到最大文件的列表:
# du -a /var | sort -n -r | head -n 10
If you want more human readable output try:
如果您想要更多人类可读的输出,请尝试:
$ cd /path/to/some/var
$ du -hsx * | sort -rh | head -10
Where,
在哪里,
- Varis the directory you wan to search
- du command -h option :display sizes in human readable format (e.g., 1K, 234M, 2G).
- du command -s option :show only a total for each argument (summary).
- du command -x option :skip directories on different file systems.
- sort command -r option :reverse the result of comparisons.
- sort command -h option :compare human readable numbers. This is GNU sort specific option only.
- head command -10 OR -n 10 option :show the first 10 lines.
- var是你要搜索的目录
- du 命令 -h 选项:以人类可读格式(例如,1K、234M、2G)显示大小。
- du 命令 -s 选项:仅显示每个参数的总数(摘要)。
- du 命令 -x 选项:跳过不同文件系统上的目录。
- sort 命令 -r 选项:反转比较结果。
- sort 命令 -h 选项:比较人类可读的数字。这只是 GNU 排序特定选项。
- head 命令 -10 或 -n 10 选项:显示前 10 行。
回答by Steve
This will find the largest file or folder in your present working directory:
这将在您当前的工作目录中找到最大的文件或文件夹:
ls -S /path/to/folder | head -1
To find the largest file in all sub-directories:
查找所有子目录中最大的文件:
find /path/to/folder -type f -exec ls -s {} \; | sort -nr | awk 'NR==1 { =""; sub(/^ /, ""); print }'
回答by rindeal
On Solaris I use:
在 Solaris 上我使用:
find . -type f -ls|sort -nr -k7|awk 'NR==1{print ,}' #formatted
or
或者
find . -type f -ls | sort -nrk7 | head -1 #unformatted
because anything else posted here didn't work.
This will find the largest file in $PWD
and subdirectories.
因为这里发布的任何其他内容都不起作用。这将在$PWD
和 子目录中找到最大的文件。
回答by zjhui
Try following command :
尝试以下命令:
find /your/path -printf "%k %p\n" | sort -g -k 1,1 | awk '{if( > 500000) print /1024 "MB" " " }' |tail -n 1
This will print the largest file name and size and more than 500M. You can move the if($1 > 500000)
,and it will print the largest file in the directory.
这将打印最大的文件名和大小并且超过 500M。您可以移动if($1 > 500000)
,它将打印目录中最大的文件。
回答by kenorb
Try the following one-liner (display top-20 biggest files):
尝试以下单行(显示前 20 个最大文件):
ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20
or (human readable sizes):
或(人类可读的尺寸):
ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20
Works fine under Linux/BSD/OSX in comparison to other answers, as find's
-printf
option doesn't exist on OSX/BSD andstat
has different parameters depending on OS. However the second command to work on OSX/BSD properly (assort
doesn't have-h
), installsort
fromcoreutils
or remove-h
fromls
and usesort -nr
instead.
与其他答案相比,在 Linux/BSD/OSX 下工作正常,因为
-printf
在 OSX/BSD 上不存在find 的选项,并且stat
根据操作系统具有不同的参数。然而第二个命令,以正确地在OSX / BSD工作(如sort
不具有-h
),安装sort
从coreutils
或删除-h
从ls
和使用sort -nr
代替。
So these aliases are useful to have in your rcfiles:
所以这些别名在你的rc文件中很有用:
alias big='du -ah . | sort -rh | head -20'
alias big-files='ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20'
回答by Ansgar Wiechers
du -aS /PATH/TO/folder | sort -rn | head -2 | tail -1
du -aS /PATH/TO/folder | sort -rn | head -2 | tail -1
or
或者
du -aS /PATH/TO/folder | sort -rn | awk 'NR==2'
du -aS /PATH/TO/folder | sort -rn | awk 'NR==2'