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


当前回答

进入指定目录,然后运行以下命令

# du -sh * 

4.0K    1
4.0K    anadb.sh --> Shell file
4.0K    db.sh/    --> shell file
24K     backup4/  --> Directory
8.0K    backup6/  --> Directory 
1.9G    backup.sql.gz  --> sql file

其他回答

如果您在脚本中使用它,请使用stat。

$ date | tee /tmp/foo
Wed Mar 13 05:36:31 UTC 2019

$ stat -c %s /tmp/foo
29

$ ls -l /tmp/foo
-rw-r--r--  1 bruno  wheel  29 Mar 13 05:36 /tmp/foo

这将给出以字节为单位的大小。有关更多输出格式选项,请参阅man stat。

OSX/BSD的等效版本是:

$ date | tee /tmp/foo
Wed Mar 13 00:54:16 EDT 2019

$ stat -f %z /tmp/foo
29

$ ls -l /tmp/foo
-rw-r--r--  1 bruno  wheel  29 Mar 13 00:54 /tmp/foo

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

我一直在做以下事情:

$ 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)

要获得目录的总大小或文件使用的总大小,

du -csh <directory or filename*> | grep total

进入所选目录并执行:

$ 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 .