是否有可能在Unix中使用ls列出子目录的总大小及其所有内容,而不是通常的4K(我假设)只是目录文件本身?
total 12K
drwxrwxr-x 6 *** *** 4.0K 2009-06-19 10:10 branches
drwxrwxr-x 13 *** *** 4.0K 2009-06-19 10:52 tags
drwxrwxr-x 16 *** *** 4.0K 2009-06-19 10:02 trunk
在翻遍了手册之后,我一无所获。
是否有可能在Unix中使用ls列出子目录的总大小及其所有内容,而不是通常的4K(我假设)只是目录文件本身?
total 12K
drwxrwxr-x 6 *** *** 4.0K 2009-06-19 10:10 branches
drwxrwxr-x 13 *** *** 4.0K 2009-06-19 10:52 tags
drwxrwxr-x 16 *** *** 4.0K 2009-06-19 10:02 trunk
在翻遍了手册之后,我一无所获。
当前回答
嗯,最好的方法是使用这个命令:
du -h -x / | sort -hr >> /home/log_size.txt
然后你就可以在你的服务器上获得所有大小的文件夹。轻松帮助您找到最大的尺寸。
其他回答
find . -maxdepth 1 -exec du --apparent-size --max-depth=0 --null '{}' ';' |\
sort -k1 -nr --zero-terminated |\
cut -f2 --zero-terminated |\
xargs --null -n 1 du -h --apparent-size --max-depth=0
特点:
由于Linux文件名可以有换行符或空格,所以我们使用空字符来分隔文件/目录名。 我们根据文件/目录的大小对它们进行排序。 我们用——-size和du来得到类似于ls的行为。
只是一个警告,如果你想比较文件的大小。Du根据文件系统、块大小、... .产生不同的结果
可能会发生文件大小不同的情况,例如比较您的本地硬盘和USB大容量存储设备上的相同目录。我使用以下脚本,包括ls来计算目录大小。结果以字节为单位,将所有子目录都考虑在内。
echo "[GetFileSize.sh] target directory: \"$1\""
iRetValue=0
uiLength=$(expr length "$1")
if [ $uiLength -lt 2 ]; then
echo "[GetFileSize.sh] invalid target directory: \"$1\" - exiting!"
iRetValue=-1
else
echo "[GetFileSize.sh] computing size of files..."
# use ls to compute total size of all files - skip directories as they may
# show different sizes, depending on block size of target disk / file system
uiTotalSize=$(ls -l -R $1 | grep -v ^d | awk '{total+=$5;} END {print total;}')
uiLength=$(expr length "$uiTotalSize")
if [ $uiLength -lt 1 ]; then
uiTotalSize=0
fi
echo -e "[GetFileSize.sh] total target file size: \"$uiTotalSize\""
fi
exit "$iRetValue"
我总是使用du -sk (-k标志,以千字节为单位显示文件大小)代替。
在初始化脚本中放置。bashrc…根据需要调整def。
duh() {
# shows disk utilization for a path and depth level
path="${1:-$PWD}"
level="${2:-0}"
du "$path" -h --max-depth="$level"
}
Du -sk * | sort -n将按大小对文件夹排序。当你想要清理空间时很有帮助。
或du -sh * | sort -h用于人类可读模式