如何在Linux中查看文件和目录的大小?如果使用df -m,那么它会显示所有目录在顶层的大小,但是,对于目录内的目录和文件,我如何检查大小?
当前回答
你可以在Linux中使用ls -sh,也可以进行排序 你需要去目录,在那里你想检查文件的大小
其他回答
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支持如上所述的——块大小。
使用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
还有一个很棒的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退出
您可以在这里使用ncdu磁盘使用分析器。它在ncurses接口中显示文件和目录的大小。您可以导航到每个目录,并从同一界面查看文件大小。
安装
$ sudo apt-get install ncdu
分析
$ ncdu <directory>
推荐文章
- 在Bash中检查变量是否存在于列表中
- 查看PS命令的全部输出
- 如何在Python中获得所有直接子目录
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 如何在Ruby中创建文件
- Linux命令将域名转换为IP
- 如何从命令行在windows中找到mysql数据目录
- __FILE__宏显示完整路径
- 有效地测试Linux上的端口是否打开?
- 如何从另一个文件A中删除文件B中出现的行?
- 对以制表符分隔的文件进行排序
- 使用sudo时未找到命令
- 当有命令行参数时,如何使用GDB分析程序的核心转储文件?
- 如何强制从另一个SSH会话分离屏幕?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?