bash OSX bash递归查找按大小排序的文件,文件夹除外

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17576370/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:53:17  来源:igfitidea点击:

OSX bash recursively find files sorted by size except in folder

macosbashfilesearchfile-listing

提问by Dave

I came across the following command, which nearlydoes what I need:

我遇到了以下命令,它几乎可以满足我的需要:

find . -type f -print0 | xargs -0 ls -l | sort -k5,5rn > ~/files.txt

find . -type f -print0 | xargs -0 ls -l | sort -k5,5rn > ~/files.txt

Now, I don't have a clue what any of this means (would love an explanation, but not that important).

现在,我不知道这意味着什么(想要一个解释,但不是那么重要)。

The one thing I need to add is to not bother with specific folders (i.e. I have a Documents folder with 10s of thousands of Word docs, which is making this command take a long long time).

我需要添加的一件事是不要打扰特定的文件夹(即我有一个包含成千上万个 Word 文档的 Documents 文件夹,这使得这个命令需要很长时间)。

Can anyone suggest an addition to the above command that will have find ignore a given folder(s)?

任何人都可以建议对上述命令进行添加以忽略给定的文件夹吗?

回答by Ansgar Wiechers

Exclude paths matching */Documents/*from find:

排除匹配路径*/Documents/*find

find . -type f ! -path "*/Documents/*" -print 0 | ...

回答by Amory

Since you asked for an explanation...

既然你要求解释...

find . -type f -print0

That's the findutility, which travels through the file system to find something that matches what you want it to. The .essentially means it will try to find anything, but since you specified -type fit will only find "regular files." -print0, as you may have guessed, simply prints the full path to the standard output (useful for piping). It uses a null character at the end of each line (as opposed to -print, this will be relevant in a moment).

这就是find实用程序,它遍历文件系统以查找与您想要的内容相匹配的内容。在.本质上意味着它会尝试找到任何东西,但因为你指定-type f它只能找到“常规文件”。 -print0,正如您可能已经猜到的那样,只需打印标准输出的完整路径(对管道很有用)。它在每一行的末尾使用一个空字符(与 不同-print,稍后会相关)。

xargs -0 ls -l

xargstakes a list of things from standard input and then executes a given command ("utility") using what is passed to it as an argument. In this case, the utility is the command ls -lso xargs takes the results from findand performs ls -lon them, giving you the long, full path; this is basically just a way to turn your list of files into a list of files with information such as size. The -0option allows xargs to interpret null characters as the separator between lines, which exists (almost?) solely to allow it to work with the -print0option above.

xargs从标准输入中获取事物列表,然后使用作为参数传递给它的内容执行给定的命令(“实用程序”)。在这种情况下,实用程序是命令,ls -l因此 xargs 从中获取结果find并对其执行ls -l,为您提供长而完整的路径;这基本上只是一种将文件列表转换为包含大小等信息的文件列表的方法。该-0选项允许 xargs 将空字符解释为行之间的分隔符,它的存在(几乎?)只是为了允许它与-print0上面的选项一起使用。

sort -k5,5rn > ~/files.txt

sortis pretty cool. It sorts things. -ktells it which column to sort by, in this case column 5 (and only column 5). The rnbit means sort using numbers and reverse the order. The default is largest at the bottom so this puts largest first. Sorting numerically can get confusing if you use unit-suffixes (B, K, M, G, etc.) using ls -lh.

sort很酷。它整理东西。 -k告诉它按哪一列排序,在这种情况下是第 5 列(并且只有第 5 列)。该rn位表示使用数字排序并反转顺序。默认值在底部最大,因此将最大放在首位。如果您使用单位后缀(B、K、M、G 等)使用ls -lh.

回答by Lri

Different options or other ways to find large files:

查找大文件的不同选项或其他方法:

  • find ~ -size +100M ! -path ~/Documents\* ! -path ~/Library\*
  • find ~ -size +100M | grep -v "^$HOME/Documents/" | while IFS= read -r l; do stat -f'%z %N' "$l"; done | sort -rn
  • shopt -s extglob; find ~/!(Documents) -type f -exec stat -f'%z %N' {} \; | sort -rn | head -n200
  • mdfind 'kMDItemFSSize>=1e8&&kMDItemContentTypeTree!=public.directory' | while IFS= read -r l; do stat -f'%z %N' "$l"; done | sort -rn
  • find ~ -size +100M ! -path ~/Documents\* ! -path ~/Library\*
  • find ~ -size +100M | grep -v "^$HOME/Documents/" | while IFS= read -r l; do stat -f'%z %N' "$l"; done | sort -rn
  • shopt -s extglob; find ~/!(Documents) -type f -exec stat -f'%z %N' {} \; | sort -rn | head -n200
  • mdfind 'kMDItemFSSize>=1e8&&kMDItemContentTypeTree!=public.directory' | while IFS= read -r l; do stat -f'%z %N' "$l"; done | sort -rn

You might also just use Finder:

您也可以只使用 Finder: