是否有可能在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

在翻遍了手册之后,我一无所获。


当前回答

这是我喜欢的

更新:我不喜欢前一个,因为它不显示当前目录下的文件,它只列出目录。

ubuntu上/var的输出示例:

sudo du -hDaxd1 /var | sort -h | tail -n10 .使用实例

4.0K    /var/lock
4.0K    /var/run
4.0K    /var/www
12K     /var/spool
3.7M    /var/backups
33M     /var/log
45M     /var/webmin
231M    /var/cache
1.4G    /var/lib
1.7G    /var

其他回答

Du -sch *在同一目录下。

从ls中只检索以字节为单位的大小。

ls -ltr | head -n1 | cut -d' ' -f2

只是一个警告,如果你想比较文件的大小。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命令

在初始化脚本中放置。bashrc…根据需要调整def。

duh() {
  # shows disk utilization for a path and depth level
  path="${1:-$PWD}"
  level="${2:-0}"
  du "$path" -h --max-depth="$level"
}