在输出中查找没有大小的目录中最大的文件

1 linux bash ls find

我需要找到最大的文件

  1. 只应列出一个文件
  2. 搜索应该在给定的目录和子目录中工作
  3. 输出应显示文件名的文件的绝对路径

    find "$PARAM" -type f | xargs ls -1S | head -n 1
    
    Run Code Online (Sandbox Code Playgroud)

有效,但给了我类似的错误

ls: 无法访问 Over: 没有那个文件或目录

gle*_*man 5

不要解析ls。让我们find为您完成这项工作:

find "$PARAM" -type f -printf "%s\t%p\n" | sort -n | tail -n 1 | cut -f 2- 
Run Code Online (Sandbox Code Playgroud)

没有 find,我们可以使用 bash 的递归通配符:

shopt -s globstar nullglob
stat -c $'%s\t%F\t%n' ** \
| awk -F'\t' '$2 == "regular file"' \
| sort -n \
| tail -n 1 \
| cut -f 3-
Run Code Online (Sandbox Code Playgroud)

OSX 上的 stat 将有不同但等效的 stat 选项,并且可能会为“常规文件”吐出不同的字符串。