【发布时间】:2012-07-28 00:53:31
【问题描述】:
如何在 Linux 中查看文件和目录的大小?如果使用df -m,那么它会在顶层显示所有目录的大小,但是对于目录中的目录和文件,我如何查看大小?
【问题讨论】:
如何在 Linux 中查看文件和目录的大小?如果使用df -m,那么它会在顶层显示所有目录的大小,但是对于目录中的目录和文件,我如何查看大小?
【问题讨论】:
有du命令。
目录和/或文件的大小,以人性化的方式:
$ du -sh .bashrc /tmp
我把它记成了一个不存在的英文单词dush。
--apparent-size 命令行开关使其测量表面大小(ls 显示的内容)而不是实际磁盘使用情况。
【讨论】:
-h 标志应该满足您的要求:以人类可读格式打印尺寸(例如,1K 234M 2G)
du 所做的是 Summarize disk usage of the set of FILEs ,也就是说,如果文件非常小(即 2140 字节),“du”的输出(在我的情况下)为 4KB,因为那是集群的大小
--apparent-size。
du -hs *查看当前目录中所有文件和目录的大小。
对文件使用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 命令不会列出目录的实际大小(why?)。因此,我们为此使用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 不会显示目录所有内容的总大小。
du 命令显示文件的磁盘使用情况,这可能大于文件的实际大小。您可以使用du -d 来获取实际大小,就像ls 一样。更多信息:unix.stackexchange.com/a/106278/155224
使用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 的手册页中有描述; man ls 并搜索 SIZE。它也允许使用 MB/MiB 以外的单位,并且从外观上看(我没有尝试过)也可以使用任意块大小(因此,如果需要,您可以将文件大小视为 412 字节块的数量到)。
请注意,--block-size 参数是 Open Group ls 之上的 GNU 扩展,因此如果您没有 GNU 用户空间(大多数 Linux 安装都有),这可能不起作用。 GNU coreutils 8.5 中的 ls 确实支持 --block-size ,如上所述。
【讨论】:
还有一个很棒的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
使用此命令,您将看到目录的表观空间和文件的真实空间,以及所显示文件的详细名称,以及每个文件的大小和创建日期。
【讨论】:
ls -l 会给你包含元数据的文件大小吗?因为我尝试使用的文件之一似乎稍微大了 4kb
你可以在 linux 中使用 ls -sh 你也可以进行排序 您需要转到要检查文件大小的目录
【讨论】:
我一直在做以下事情:
$ 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: -
此外,在现代文件系统中,我们可以使用快照、稀疏文件(其中包含漏洞的文件)使情况更加复杂。
您可以在本文中查看更多详细信息:understanding file size in 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。
$ 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 [文件名]
非常适合获取特定文件的大小。
【讨论】:
您只需要-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'以单位打印尺寸 1,048,576 字节;请参阅下面的SIZE格式
SIZE是一个整数和可选单位(例如:10M 是 10*1024*1024)。 单位为 K、M、G、T、P、E、Z、Y(1024 的幂)或 KB、MB、... (1000 的幂)。
【讨论】:
ls -sh video.mp4 | sed s/video.mp4//g
输出, 5.6M
【讨论】:
sed 部分?
5.6M 所以,我发布了这个,如果有同样需要的人可能会对此有所帮助。 ls -sh video.mp4 用文件名列出文件大小,我添加了sed s/video.mp4//g 用空替换文件名。例如:sed s/$filename/empty/g 这样。希望至少有人和我一样有同样需要的人会发现它有帮助。
我更喜欢这个命令ll -sha。
【讨论】: