如何在Linux中查看文件和目录的大小?如果使用df -m,那么它会显示所有目录在顶层的大小,但是,对于目录内的目录和文件,我如何检查大小?
当前回答
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命令,目录使用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
文件大小(MB)
ls -l --b=M filename | cut -d " " -f5
文件大小(GB)
ls -l --b=G filename | cut -d " " -f5
您必须区分文件大小和磁盘使用情况。两者之间的主要区别在于文件被“切割成碎片”并存储在块中。
现在的块大小是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中的文件大小
我自己是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
如果您在脚本中使用它,请使用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
推荐文章
- Linux Bash中双&和分号有什么区别?
- 在SSH会话中查找客户端的IP地址
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- 如何删除文件中的特定行?
- Linux删除大小为0的文件
- Spring引导应用程序作为服务
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- Windows和Linux上的c++编译:ifdef开关
- Linux: kill后台任务
- 使用Java重命名文件
- 如何修改Linux系统中打开文件的数量限制?
- 如何从Python包内读取(静态)文件?
- errno线程安全吗?
- 为什么我得到“Pickle - EOFError: run out of input”读取一个空文件?