如何在Linux中查看文件和目录的大小?如果使用df -m然后它显示顶层所有目录的大小,但对于目录中的目录和文件如何检查大小?
mk.*_*k.. 432
这很简单.使用ls命令用于目录的文件和du命令.
检查文件大小
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 */
Run Code Online (Sandbox Code Playgroud)
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*/
Run Code Online (Sandbox Code Playgroud)
包括-h选择上述任何命令(用于例如:ls -lh *或du -sh)会给你在人类可读的格式大小(kb,mb,gb,...)
有关更多信息,请参阅man ls和man du
Max*_*kin 134
有du命令.
目录大小:
$ du -sh .bashrc /tmp
Run Code Online (Sandbox Code Playgroud)
文件大小:
$ du -sh .bashrc /tmp
Run Code Online (Sandbox Code Playgroud)
--apparent-size命令行开关使其可以测量表观大小(ls显示的内容)而不是实际的磁盘使用情况.
Tho*_*hor 66
使用ls -s列出的文件大小,或者如果你喜欢ls -sh的人可读的大小.
对于目录使用du,再次,du -h对于人类可读的大小.
Rav*_*dra 18
文件大小(MB)
ls -l --b=M filename | cut -d " " -f5
Run Code Online (Sandbox Code Playgroud)
文件大小(GB)
ls -l --b=G filename | cut -d " " -f5
Run Code Online (Sandbox Code Playgroud)
avt*_*ton 15
还有一个很棒的ncdu实用程序 - 它可以显示目录大小以及有关子文件夹和文件的详细信息.
Ubuntu的:
$ sudo apt-get install ncdu
Run Code Online (Sandbox Code Playgroud)
只需输入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
Run Code Online (Sandbox Code Playgroud)
删除当前突出显示的元素d,用CTRL+ 退出c
Adi*_*Adi 13
ls -l -block-size = M将为您提供长格式列表(实际查看文件大小所需)和圆形文件大小,最接近最接近的MiB.
如果您想要MB(10 ^ 6字节)而不是MiB(2 ^ 20字节)单位,请改用--block-size = MB.
如果您不希望M后缀附加到文件大小,则可以使用类似--block-size = 1M的内容.感谢StéphaneChazelas的建议.
这在ls的手册页中有描述; 男人ls并搜索SIZE.它允许MB/MiB以外的单位,并且从它的外观(我没有尝试)任意块大小(所以你可以看到文件大小为412字节块的数量,如果你想至).
请注意, - block-size参数是Open Group的ls之上的GNU扩展,因此如果您没有GNU userland(大多数Linux安装都这样做),这可能不起作用.来自GNU coreutils 8.5的ls确实支持如上所述的--block-size.
Gio*_*ous 10
所有你需要的是-l和--block-size标志
工作目录下所有文件和目录的大小(以 MB 为单位)
ls -l --block-size=M
Run Code Online (Sandbox Code Playgroud)
工作目录下所有文件和目录的大小(以GB为单位)
ls -l --block-size=G
Run Code Online (Sandbox Code Playgroud)
特定文件或目录的大小
ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)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 的幂)。
您可以使用ncdu磁盘使用分析器。它在 ncurses 界面中显示文件和目录的大小。您可以导航到每个目录并从同一界面查看文件大小。
安装
$ sudo apt-get install ncdu
Run Code Online (Sandbox Code Playgroud)
来分析
$ ncdu <directory>
Run Code Online (Sandbox Code Playgroud)
如果在脚本中使用它,请使用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
Run Code Online (Sandbox Code Playgroud)
这将为您提供字节大小。请参阅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
Run Code Online (Sandbox Code Playgroud)
我一直这样做:
$ du -sh backup-lr-May-02-2017-1493723588.tar.gz
Run Code Online (Sandbox Code Playgroud)
注意:
-s, --summarize
display only a total for each argument
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
Run Code Online (Sandbox Code Playgroud)
转到所选目录并执行:
$ du -d 1 -h
Run Code Online (Sandbox Code Playgroud)
哪里:
-d 1 is the depth of the directories
-h is the human-readable option
Run Code Online (Sandbox Code Playgroud)
您会看到这样的内容:
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 .
Run Code Online (Sandbox Code Playgroud)
使用带有 -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
Run Code Online (Sandbox Code Playgroud)
小智 5
要获取目录的总大小或文件使用的总大小,
du -csh <directory or filename*> | grep total
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
744139 次 |
| 最近记录: |