du 跳过符号链接

kai*_*naw 10 shell symlink wildcards

默认行为du在我的系统是不正确的默认行为。

如果我是ls我的/data文件夹,我会看到(删除不重要的内容):

ghs
ghsb -> ghs
hope
rssf -> roper
roper
Run Code Online (Sandbox Code Playgroud)

每个文件夹内都有一组以数字为名称的文件夹。我想获得所有名为 的文件夹的总大小14,所以我使用:

du -s /data/*/14
Run Code Online (Sandbox Code Playgroud)

我看...

161176 /data/ghs/14
161176 /data/ghsb/14
8 /data/hope/14
681564 /data/rssf/14
681564 /data/roper/14
Run Code Online (Sandbox Code Playgroud)

我想要的只是:

161176 /data/ghs/14
8 /data/hope/14
681564 /data/roper/14
Run Code Online (Sandbox Code Playgroud)

我不想看到符号链接。我试过-L, -D,-S等。我总是得到符号链接。有没有办法去除它们?

phe*_*mer 17

这不是du解析符号链接;这是你的外壳。

*是一个壳球;在运行任何命令之前,它会被 shell 展开。因此,实际上,您正在运行的命令是:

du -s /data/ghs/14 /data/ghsb/14 /data/hope/14 /data/rssf/14 /data/roper/14
Run Code Online (Sandbox Code Playgroud)

如果你的 shell 是 bash,你就没有办法告诉它不要扩展符号链接。但是,您可以改用find(GNU 版本):

find /data -mindepth 2 -maxdepth 2 -type d -name 14 -exec du -s {} +
Run Code Online (Sandbox Code Playgroud)

  • 完美运行。有人可以在这里解释一下 `{}` 和 `+` 的使用吗? (2认同)

Eri*_*ski 6

使du跳过符号链接:

du不够聪明,不能不追逐链接。默认情况下find将跳过符号链接。因此,在finddu、 和之间建立邪恶的联盟awk,正确的黑魔法咒语变为:

find /home/somedirectory/ -exec du -s {} + | awk '{total = total + $1}END{print total}'
Run Code Online (Sandbox Code Playgroud)

产生:

145070492

强制输出是人类可读的:

find /home/somedirectory/ -exec du -s {} + | awk '{total = total + $1}END{print (total / 1024 / 1024) "MB"}'
Run Code Online (Sandbox Code Playgroud)

产生:

138.35MB

这里发生了什么:

/home/somedirectory/      directory to search.
-exec du -s +             run du -s over the results, producing bytes
awk '...'                 get the first token of every line and add them up,
                          dividing by 1024 twice to produce MB
Run Code Online (Sandbox Code Playgroud)