Linux命令获取文件和目录的大小,在一个特定的文件夹?

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

1525989 次浏览

du命令。

目录和/或文件的大小,以一种人性化的方式:

$ du -sh .bashrc /tmp

我把它记为一个不存在的英文单词dush


--apparent-size命令行开关使它测量表观大小(ls所显示的),而不是实际的磁盘使用量。

文件使用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)将以人类可读的格式提供大小(kbmbgb,…)

有关更多信息,请参阅man lsman du

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

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

ls -l——block-size=M将给出一个很长的格式列表(需要实际查看文件大小),并将文件大小四舍五入到最近的MiB。

如果你想要MB(10^6字节)的单位而不是MiB(2^20字节)的单位,使用——block-size=MB代替。

如果你不希望文件大小附加M后缀,你可以使用——block-size=1M。感谢Stéphane Chazelas的建议。

这在ls的手册页中描述;男人和搜索大小。它还允许使用MB/MiB以外的单位,并且从它的外观(我没有尝试过)来看,它还允许任意块大小(因此,如果您愿意,您可以将文件大小视为412字节块的数量)。

注意——block-size参数是在Open Group的ls之上的GNU扩展,所以如果你没有GNU用户区(大多数Linux安装都有),这个参数可能不起作用。GNU coreutils 8.5中的ls支持如上所述的——块大小。

还有一个很棒的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退出

文件大小(MB)

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

文件大小(GB)

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

你可以使用:

ls -lh

使用此命令,您将看到目录的表观空间和文件的真实空间,以及所显示文件的详细名称,以及每个文件的大小和创建日期。

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

我一直在做以下事情:

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

注:

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

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

ls -lrtsh

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

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

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

如果你使用stat命令,你可以并排看到两个数字。

stat file.c

如果你想要一个更紧凑的目录视图,你可以使用ls -ls,它会给你1KiB单位的使用量。

ls -ls dir

此外,du将给出真实的磁盘使用情况,单位为1KiB,或者带有-u标志的dutree

例如:1字节文件的使用情况

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

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

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

如果你在脚本中使用它,使用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

特定的目录然后运行下面的命令

# 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

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

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

使用带-h参数的ls命令:[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

进入所选目录并执行:

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

Du -sh [file_name]

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

你所需要的只是-l--block-size标志

工作目录下所有文件和目录的大小(以mb为单位)

ls -l --block-size=M

工作目录下所有文件和目录的大小(单位:gb)

ls -l --block-size=G

指定文件或目录的大小

ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/

ls --help

-l使用长列表格式

--block-size=SIZE:在打印之前通过SIZE缩放大小;例如, '--block-size=M'以的单位打印大小 1048576个字节;参见下面的SIZE格式

SIZE是一个整数,可选单位(例如:10M是10*1024*1024)。 单位为K, M, G, T, P, E, Z, Y(1024的幂)或KB, MB,… (1000的幂).


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

安装

$ sudo apt-get install ncdu

分析

$ ncdu <directory>

enter image description here

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

< >强劲输出, < / p > 5.6米

我更喜欢ll -sha这个命令。