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


当前回答

Du -sh [file_name]

可以完美地获取特定文件的大小。

其他回答

进入所选目录并执行:

$ du -d 1 -h

地点:

-d 1 is the depth of the directories

-h is the human-readable option

你会看到:

0   ./proc
8.5M    ./run
0   ./sys
56M ./etc
12G ./root
33G ./var
23M ./tmp
3.2G    ./usr
154M    ./boot
26G ./home
0   ./media
0   ./mnt
421M    ./opt
0   ./srv
2.6G    ./backups
80G .

我自己是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

我一直在做以下事情:

$ du -sh backup-lr-May-02-2017-1493723588.tar.gz

NB:

-s, --summarize
            display only a total for each argument
-h, --human-readable
            print sizes in human readable format (e.g., 1K 234M 2G)

使用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 -s来列出文件大小,或者如果您更喜欢ls -sh来列出人类可读的大小。

对于目录,使用du,对于人类可读的大小,同样使用du -h。