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


当前回答

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

ls -lrtsh

其他回答

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

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

文件使用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

ls -sh video.mp4 | sed s/video.mp4//g

输出, 5.6米

文件大小(MB)

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

文件大小(GB)

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

进入所选目录并执行:

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