如何在Linux中查看文件和目录的大小?如果使用df -m,那么它会显示所有目录在顶层的大小,但是,对于目录内的目录和文件,我如何检查大小?


当前回答

使用ls命令带-h参数:[root@hots19 etc]# ls -lh H:供人阅读。

Exemple: 
    [root@CIEYY1Z3 etc]# ls -lh
    total 1.4M
    -rw-r--r--.  1 root   root      44M Sep 15  2015 adjtime
    -rw-r--r--.  1 root   root     1.5K Jun  7  2013 aliases
    -rw-r--r--   1 root   root      12K Nov 25  2015 aliases.db
    drwxr-xr-x.  2 root   root     4.0K Jan 11  2018 alternatives
    -rw-------.  1 root   root      541 Jul  8  2014 anacrontab
    -rw-r--r--.  1 root   root      55M Sep 16  2014 asound.conf
    -rw-r--r--.  1 root   root       1G Oct  6  2014 at.deny

其他回答

文件使用ls命令,目录使用du命令。

检查文件大小

ls -l filename   #Displays Size of the specified file
ls -l *          #Displays Size of All the files in the current directory
ls -al *         #Displays Size of All the files including hidden files in the current directory
ls -al dir/      #Displays Size of All the files including hidden files in the 'dir' directory

Ls命令不会列出目录的实际大小(为什么?)为此,我们使用du。

检查目录大小

du -sh directory_name    #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh *                #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format

在上述任何命令中包含-h选项(例如:ls -lh *或du -sh)将以人类可读的格式给出大小(kb, mb,gb,…)

有关更多信息,请参阅man ls和man du

你可以在Linux中使用ls -sh,也可以进行排序 你需要去目录,在那里你想检查文件的大小

ls -l——block-size=M将给出一个很长的格式列表(需要实际查看文件大小),并将文件大小四舍五入到最近的MiB。

如果你想要MB(10^6字节)的单位而不是MiB(2^20字节)的单位,使用——block-size=MB代替。

如果你不希望文件大小附加M后缀,你可以使用——block-size=1M。感谢Stéphane Chazelas的建议。

这在ls的手册页中描述;男人和搜索大小。它还允许使用MB/MiB以外的单位,并且从它的外观(我没有尝试过)来看,它还允许任意块大小(因此,如果您愿意,您可以将文件大小视为412字节块的数量)。

注意——block-size参数是在Open Group的ls之上的GNU扩展,所以如果你没有GNU用户区(大多数Linux安装都有),这个参数可能不起作用。GNU coreutils 8.5中的ls支持如上所述的——块大小。

您可以使用下面的命令以易于人类阅读的格式获取文件列表。

ls -lrtsh

文件大小(MB)

ls -l --b=M  filename | cut -d " " -f5

文件大小(GB)

ls -l --b=G  filename | cut -d " " -f5