Linux basic command -du to view the size of the file

Article Directory

of

command introduction

grammatical format

Basic parameters

Reference example

1) Displays the specified file size in human readable form

2) Display the size of all files in the current directory

3) Only show the size of the directory

4) Display which directory file is the largest under the root

5) Display the size of all files

6) Only the files under the directory are displayed, and the subdirectories under the directory are not displayed

Difference between ll -h and du -h commands 

command summary

of

command introduction

First look at the meaning in the help document

NAME
       du - estimate file space usage

 The function of the du (Disk Usage) command is to view the size of a file or directory. By default, the size of the file is displayed in kb. If it is a directory, it will display the capacity of each file in the directory and the total capacity of the directory.

grammatical format

The grammatical format of the du command is: du [parameter] [file]

SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F

Let's take a look at the parameters of the command.

Basic parameters

-a List the size of all subdirectories or files under the directory
-h Displayed in human readable capacity format
-m List capacity size in MB
-k List capacity size in KB
-s Only the capacity of the directory is displayed, and the capacity of the files under it is not displayed
-S Only the directory and the file capacity under the directory are displayed, and the capacity of the subdirectories under the directory is not displayed

Reference example

1) Displays the specified file size in human readable form

The left side shows the file size, and the right side shows the file name 

[root@localhost ~]# du -h anaconda-ks.cfg 
4.0K	anaconda-ks.cfg

2) Display the size of all files in the current directory

When you directly enter du without any options, du will analyze the hard disk space occupied by the subdirectories in the current directory; including hidden files

[root@localhost /]# du | wc -l
27459
[root@localhost ~]# du | wc -l
104

3) Only show the size of the directory

The directory can be specified, adding the -s parameter will only display the capacity of the directory itself 

[root@localhost ~]# du -sh /etc
42M	/etc

4) Display which directory file is the largest under the root

First check the directory under the root directory, list the capacity in MB, and then sort the file size through the pipe symbol

[root@localhost /]# du -sm /* | sort -nur
4561	/run
3674	/usr
504	/var
122	/boot
42	/etc
4	/root
1	/home
0	/bin

5) Display the size of all files

Display all the files, you can see that only the capacity of 10 files is displayed, if all are displayed, you can use wc -l to see that there are so many files. 

[root@localhost ~]# du -ah | head 
4.0K	./.bash_logout
4.0K	./.bash_profile
4.0K	./.bashrc
4.0K	./.cshrc
4.0K	./.tcshrc
4.0K	./anaconda-ks.cfg
4.0K	./.bash_history
4.0K	./.cache/abrt/lastnotification
4.0K	./.cache/abrt/applet_dirlist
8.0K	./.cache/abrt
[root@localhost ~]# du -a | wc -l
167

6) Only the files under the directory are displayed, and the subdirectories under the directory are not displayed

The result of using the -S command is the same as that of the du command without adding -S. In fact, they all display the directory and the files under it. 

[root@localhost ~]# du -S /etc | wc -l
746
[root@localhost ~]# du /etc | wc -l
746

Difference between ll -h and du -h commands 

Different commands display files, why are the file sizes different?

[root@localhost ~]# ll -h anaconda-ks.cfg 
-rw-------. 1 root root 1.4K 2月   8 00:02 anaconda-ks.cfg

[root@localhost ~]# du -h anaconda-ks.cfg 
4.0K	anaconda-ks.cfg

By using the results of the ll -h file and the du -sh file, it can be found that it is obviously the same file, but the displayed capacity is different.

In fact, it all starts with the block block. 

As we all know, the smallest storage unit in a disk is a sector, and each sector stores 512 bytes by default, which is 0.5KB); and the file system will read multiple sectors at a time by default, that is, read one " "Block" is also a block. The size of a block is usually 4KB , that is, eight consecutive sectors (sectors) form a block.

Then when writing a file, no matter how small the file is, it will occupy 1 block.

Going back to the file size displayed by the command, we can see that the du -h command displays the occupied space of the disk, while ll -h displays the actual size of the file.

attach a picture

Files in windows have actual size and occupied space. 

command summary

        The du command is quite interesting. Whether it is used to check the size of files or the size of directories, it is a good command. If you think the above content is okay, you can like it and support it!

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129228599