如何查看linux中的文件和目录的大小?

时间:2022-07-23 15:04:54

How can I see the size of files and directories in Linux? If use df -m then it shows the size of all the directory at the top level but for the directories and files inside the directory how to check size?

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

12 个解决方案

#1


227  

It's simple. Use ls command for files and du command for directories.

这很简单。文件使用ls命令,目录使用du命令。

Checking File Sizes

检查文件大小

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

ls command will not list the actual size of directories(why?). Therefore, we use du for this purpose.

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

Checking Directory sizes

检查目录大小

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*/

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, ...)

包括-h选项在上面的任何一个命令中(对Ex: ls -lh *或du -sh)将以人类可读格式(kb, mb,gb,…)

For more information see man ls and man du

更多信息请见man ls和man du

#2


90  

There is du command.

du命令。

Size of a directory:

一个目录的大小:

$ du -sh /tmp

Size of a file:

文件大小:

$ du -h /tmp/xyz

--apparent-size command line switch makes it measure apparent sizes (what ls shows) rather than actual disk usage.

——尺寸大小的命令行开关使它能够测量明显的尺寸(ls显示的),而不是实际的磁盘使用情况。

#3


38  

Use ls -s to list file size, or if you prefer ls -sh for human readable sizes.

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

For directories use du, and again, du -h for human readable sizes.

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

#4


13  

You can use:

您可以使用:

ls -lh

ls lh

Using this command you'll see the apparent space of the directory and true space of the files and in details the names of the files displayed, besides the size and creation date of each.

使用这个命令,您将看到目录的明显空间和文件的真实空间,并详细说明显示的文件的名称,除了每个文件的大小和创建日期。

#5


9  

ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.

ls -l——块大小=M将为您提供一个很长的格式列表(实际上需要查看文件大小),以及到最近的MiB的圆形文件大小。

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.

如果你想MB(10 ^ 6字节)而不是MiB(2 ^ 20字节)单位,而是使用——块= MB。

If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.

如果不希望将M后缀附加到文件大小,可以使用—块大小=1M。谢谢Stephane Chazelas的建议。

This is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

这在ls的手册页中描述;人类在寻找尺寸。它还允许使用MB/MiB以外的单元,并且从它的外观(我没有尝试那样做)可以看到任意的块大小(因此您可以看到文件大小为412字节的块的数量,如果您愿意的话)。

Note that the --block-size parameter is a GNU extension on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support --block-size as described above.

注意,块大小的参数是在Open Group的ls之上的一个GNU扩展,所以如果您没有GNU userland(大多数Linux安装都是这样做的),那么这可能不会起作用。来自GNU coreutils 8.5的ls支持——如上所述的块大小。

#6


6  

File Size in MB

文件的大小(以MB为单位)

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

File Size in GB

在GB文件大小

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

#7


5  

I do the following all the time:

我一直在做以下事情:

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

#8


3  

There is also a great ncdu utility - it can show directory size with detailed info about subfolders and files.

还有一个很棒的ncdu工具——它可以显示目录大小和关于子文件夹和文件的详细信息。

#9


2  

You can use below command to get list of files in easily human readable format.

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

ls -lrtsh

ls -lrtsh

#10


2  

I'm a Ubuntu 16.04 user myself and I find that the ll command is by far the easiest way to see a directory's contents. I've noticed that not all Linux distributions support this command, but there's probably a workaround/install for each distro out there.

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

Example:

例子:

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

The biggest advantage for me is that it's quick and really intuitive to use.

对我来说最大的优势是它的使用速度很快,而且非常直观。

UPDATE: what I didn't know was that on Ubuntu it's a pre-configured alias. You can easily set it yourself by executing alias ll="ls -la" on the command line, or by adding this entry in your .bashrc config file:

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

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

#11


1  

You have to differenciate between file size and disk usage. The main difference between the two comes from the fact that files are "cut into pieces" and stored in blocks.

您必须区分文件大小和磁盘使用。两者之间的主要区别在于,文件被“切割成块”并存储在块中。

Modern block size is 4KiB, so files will use disk space multiple of 4KiB, regardless of how small they are.

现代的块大小是4KiB,所以文件将使用磁盘空间乘以4KiB,不管它们有多小。

If you use the command stat you can see both figures side by side.

如果您使用command stat,您可以同时看到这两个图形。

stat file.c

If you want a more compact view for a directory, you can use ls -ls, which will give you usage in 1KiB units.

如果您想要一个更紧凑的目录视图,可以使用ls -ls,这将使您在1KiB单元中得到使用。

ls -ls dir

Also du will give you real disk usage, in 1KiB units, or dutree with the -u flag.

同样,du将给你真实的磁盘使用,在1KiB单位,或dutree与-u标志。

Example: usage of a 1 byte file

示例:使用一个字节文件

$ echo "" > file.c

$ ls -l file.c
-rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c

$ ls -ls file.c
4 -rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c

$ du file.c
4 file.c

$ dutree file.c
[ file.c 1 B ]

$ dutree -u file.c
[ file.c 4.00 KiB ]

$ stat file.c
 File: file.c
 Size: 1 Blocks: 8 IO Block: 4096 regular file
Device: 2fh/47d Inode: 2185244 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ nacho) Gid: ( 1000/ nacho)
Access: 2018-04-30 20:41:58.002124411 +0200
Modify: 2018-04-30 20:42:24.835458383 +0200
Change: 2018-04-30 20:42:24.835458383 +0200
 Birth: -

In addition, in modern filesystems we can have snapshots, sparse files (files with holes in them) that further complicate the situation.

此外,在现代的文件系统中,我们可以有快照、稀疏文件(其中有漏洞的文件),这会使情况进一步复杂化。

You can see more details in this article: understanding file size in Linux

您可以在本文中看到更多细节:了解Linux中的文件大小

#12


0  

you can use ls -sh in linux you can do sort also you need to go to dir where you want to check the size of files

您可以在linux中使用ls -sh,也可以进行排序,您还需要到要检查文件大小的dir

#1


227  

It's simple. Use ls command for files and du command for directories.

这很简单。文件使用ls命令,目录使用du命令。

Checking File Sizes

检查文件大小

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

ls command will not list the actual size of directories(why?). Therefore, we use du for this purpose.

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

Checking Directory sizes

检查目录大小

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*/

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, ...)

包括-h选项在上面的任何一个命令中(对Ex: ls -lh *或du -sh)将以人类可读格式(kb, mb,gb,…)

For more information see man ls and man du

更多信息请见man ls和man du

#2


90  

There is du command.

du命令。

Size of a directory:

一个目录的大小:

$ du -sh /tmp

Size of a file:

文件大小:

$ du -h /tmp/xyz

--apparent-size command line switch makes it measure apparent sizes (what ls shows) rather than actual disk usage.

——尺寸大小的命令行开关使它能够测量明显的尺寸(ls显示的),而不是实际的磁盘使用情况。

#3


38  

Use ls -s to list file size, or if you prefer ls -sh for human readable sizes.

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

For directories use du, and again, du -h for human readable sizes.

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

#4


13  

You can use:

您可以使用:

ls -lh

ls lh

Using this command you'll see the apparent space of the directory and true space of the files and in details the names of the files displayed, besides the size and creation date of each.

使用这个命令,您将看到目录的明显空间和文件的真实空间,并详细说明显示的文件的名称,除了每个文件的大小和创建日期。

#5


9  

ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.

ls -l——块大小=M将为您提供一个很长的格式列表(实际上需要查看文件大小),以及到最近的MiB的圆形文件大小。

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.

如果你想MB(10 ^ 6字节)而不是MiB(2 ^ 20字节)单位,而是使用——块= MB。

If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.

如果不希望将M后缀附加到文件大小,可以使用—块大小=1M。谢谢Stephane Chazelas的建议。

This is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

这在ls的手册页中描述;人类在寻找尺寸。它还允许使用MB/MiB以外的单元,并且从它的外观(我没有尝试那样做)可以看到任意的块大小(因此您可以看到文件大小为412字节的块的数量,如果您愿意的话)。

Note that the --block-size parameter is a GNU extension on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support --block-size as described above.

注意,块大小的参数是在Open Group的ls之上的一个GNU扩展,所以如果您没有GNU userland(大多数Linux安装都是这样做的),那么这可能不会起作用。来自GNU coreutils 8.5的ls支持——如上所述的块大小。

#6


6  

File Size in MB

文件的大小(以MB为单位)

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

File Size in GB

在GB文件大小

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

#7


5  

I do the following all the time:

我一直在做以下事情:

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

#8


3  

There is also a great ncdu utility - it can show directory size with detailed info about subfolders and files.

还有一个很棒的ncdu工具——它可以显示目录大小和关于子文件夹和文件的详细信息。

#9


2  

You can use below command to get list of files in easily human readable format.

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

ls -lrtsh

ls -lrtsh

#10


2  

I'm a Ubuntu 16.04 user myself and I find that the ll command is by far the easiest way to see a directory's contents. I've noticed that not all Linux distributions support this command, but there's probably a workaround/install for each distro out there.

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

Example:

例子:

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

The biggest advantage for me is that it's quick and really intuitive to use.

对我来说最大的优势是它的使用速度很快,而且非常直观。

UPDATE: what I didn't know was that on Ubuntu it's a pre-configured alias. You can easily set it yourself by executing alias ll="ls -la" on the command line, or by adding this entry in your .bashrc config file:

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

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

#11


1  

You have to differenciate between file size and disk usage. The main difference between the two comes from the fact that files are "cut into pieces" and stored in blocks.

您必须区分文件大小和磁盘使用。两者之间的主要区别在于,文件被“切割成块”并存储在块中。

Modern block size is 4KiB, so files will use disk space multiple of 4KiB, regardless of how small they are.

现代的块大小是4KiB,所以文件将使用磁盘空间乘以4KiB,不管它们有多小。

If you use the command stat you can see both figures side by side.

如果您使用command stat,您可以同时看到这两个图形。

stat file.c

If you want a more compact view for a directory, you can use ls -ls, which will give you usage in 1KiB units.

如果您想要一个更紧凑的目录视图,可以使用ls -ls,这将使您在1KiB单元中得到使用。

ls -ls dir

Also du will give you real disk usage, in 1KiB units, or dutree with the -u flag.

同样,du将给你真实的磁盘使用,在1KiB单位,或dutree与-u标志。

Example: usage of a 1 byte file

示例:使用一个字节文件

$ echo "" > file.c

$ ls -l file.c
-rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c

$ ls -ls file.c
4 -rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c

$ du file.c
4 file.c

$ dutree file.c
[ file.c 1 B ]

$ dutree -u file.c
[ file.c 4.00 KiB ]

$ stat file.c
 File: file.c
 Size: 1 Blocks: 8 IO Block: 4096 regular file
Device: 2fh/47d Inode: 2185244 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ nacho) Gid: ( 1000/ nacho)
Access: 2018-04-30 20:41:58.002124411 +0200
Modify: 2018-04-30 20:42:24.835458383 +0200
Change: 2018-04-30 20:42:24.835458383 +0200
 Birth: -

In addition, in modern filesystems we can have snapshots, sparse files (files with holes in them) that further complicate the situation.

此外,在现代的文件系统中,我们可以有快照、稀疏文件(其中有漏洞的文件),这会使情况进一步复杂化。

You can see more details in this article: understanding file size in Linux

您可以在本文中看到更多细节:了解Linux中的文件大小

#12


0  

you can use ls -sh in linux you can do sort also you need to go to dir where you want to check the size of files

您可以在linux中使用ls -sh,也可以进行排序,您还需要到要检查文件大小的dir