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


当前回答

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

其他回答

我自己是Ubuntu 16.04的用户,我发现ll命令是目前为止查看目录内容的最简单的方法。我注意到并不是所有的Linux发行版都支持这个命令,但是每个发行版都可能有一个解决方案/安装。

例子:

user@user-XPS-15-9560:/$ ll
total 188
drwxr-xr-x  27 root root  4096 Jan 26 09:13 ./
drwxr-xr-x  27 root root  4096 Jan 26 09:13 ../
drwxr-xr-x   2 root root  4096 Jan 22 15:13 bin/
drwxr-xr-x   4 root root 12288 Jan 29 11:35 boot/
drwxr-xr-x   2 root root  4096 Sep  3 18:14 cdrom/
drwxr-xr-x  20 root root  4440 Feb  5 08:43 dev/
drwxr-xr-x 153 root root 12288 Feb  2 15:17 etc/
drwxr-xr-x   4 root root  4096 Sep  3 18:15 home/
...

对我来说,最大的优势是使用起来非常快速和直观。

更新:我不知道的是,在Ubuntu上它是一个预先配置的别名。你可以通过在命令行上执行alias ll="ls -la"来轻松设置它,或者在你的.bashrc配置文件中添加以下条目:

sudo nano ~/.bashrc
...add line described above and save file by pressing Ctrl+X and Y...
source ~/.bashrc

还有一个很棒的ncdu实用程序-它可以显示目录大小和子文件夹和文件的详细信息。

安装

Ubuntu:

$ sudo apt-get install ncdu

使用

只需在命令行中输入ncdu [path]。经过几秒钟的路径分析后,你会看到如下内容:

$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
.  96,1 GiB [##########] /home
.  17,7 GiB [#         ] /usr
.   4,5 GiB [          ] /var
    1,1 GiB [          ] /lib
  732,1 MiB [          ] /opt
. 275,6 MiB [          ] /boot
  198,0 MiB [          ] /storage
. 153,5 MiB [          ] /run
.  16,6 MiB [          ] /etc
   13,5 MiB [          ] /bin
   11,3 MiB [          ] /sbin
.   8,8 MiB [          ] /tmp
.   2,2 MiB [          ] /dev
!  16,0 KiB [          ] /lost+found
    8,0 KiB [          ] /media
    8,0 KiB [          ] /snap
    4,0 KiB [          ] /lib64
e   4,0 KiB [          ] /srv
!   4,0 KiB [          ] /root
e   4,0 KiB [          ] /mnt
e   4,0 KiB [          ] /cdrom
.   0,0   B [          ] /proc
.   0,0   B [          ] /sys
@   0,0   B [          ]  initrd.img.old
@   0,0   B [          ]  initrd.img
@   0,0   B [          ]  vmlinuz.old
@   0,0   B [          ]  vmlinuz

用d删除当前高亮显示的元素,用CTRL + c退出

您可以在这里使用ncdu磁盘使用分析器。它在ncurses接口中显示文件和目录的大小。您可以导航到每个目录,并从同一界面查看文件大小。

安装

$ sudo apt-get install ncdu

分析

$ ncdu <directory>

文件使用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,也可以进行排序 你需要去目录,在那里你想检查文件的大小