在 Linux 中查找文件然后报告搜索文件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18312935/
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
Find file in Linux then report the size of file searched
提问by Henry Vu
How can i search for file in Linux then report the size of those file i have found ?
如何在 Linux 中搜索文件然后报告我找到的文件的大小?
For example, i want to search file named core.txt in my home directory in Linux, core.txt also appear on some sub-directory under my home dir. Then after found core.txt, the command should also show me file size of those files have founded.
例如,我想在Linux的主目录中搜索名为core.txt的文件,core.txt也出现在我的主目录下的某些子目录中。然后在找到 core.txt 之后,该命令还应该显示这些文件的文件大小。
Cheers
干杯
回答by Alok Singh Mahor
We can use find
command to find the file and du -sh
to find out its size.
我们可以使用find
命令来查找文件并du -sh
找出它的大小。
We will execute du -sh on found files. So final command would be
我们将对找到的文件执行 du -sh。所以最后的命令是
find ~ -name "core.txt" -exec du -sh {} \;
orfind ~ -name "core.txt" | xargs du -sh
find ~ -name "core.txt" -exec du -sh {} \;
或者find ~ -name "core.txt" | xargs du -sh
In 2nd command xargs
will not handle spaces in file name. So We can tell exact delimiter to xargs to handle spaces in file name.
在第二个命令中xargs
不会处理文件名中的空格。所以我们可以告诉 xargs 的精确分隔符来处理文件名中的空格。
find ~ -name "core.txt" | xargs -d '\n' du -sh
find ~ -name "core.txt" | xargs -d '\n' du -sh