是否有可能在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根据文件系统、块大小、... .产生不同的结果
可能会发生文件大小不同的情况,例如比较您的本地硬盘和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 -h .
要显示相同大小的信息,但不递归打印它们的子目录(可能是一个巨大的列表),只需使用——max-depth选项:
du -h --max-depth=1 .
您需要的命令是'du -sk' du = "磁盘使用率"
-k标志提供以千字节为单位的输出,而不是磁盘扇区的du默认值(512字节块)。
-s标志将只列出顶级目录(即默认情况下的当前目录或命令行上指定的目录)中的内容。奇怪的是,在这方面du和ls表现相反。默认情况下,du将递归地提供每个子目录的磁盘使用情况。相反,ls只会给出指定目录下的列表文件。(ls -R提供递归行为。)
从ls中只检索以字节为单位的大小。
ls -ltr | head -n1 | cut -d' ' -f2
du -h --max-depth=1 . | sort -n -r
输入“ls -ltrh /path_to_directory”