File Size Viewing and Quantity Statistics in Linux

1. How to check disk partition and usage

In Linux, there are various commands to view disk partitions, among which the commonly used commands are as follows:

Order illustrate
lsblk This command is used to display all block devices, including disks and their partitions. After executing this command, the device name, disk size, partition status and other information of all disks will be listed
df -h This command is used to view the usage of all mounted file systems. After executing this command, information such as mount points, total size, used size, and available size of all file systems will be displayed
df -h ./test View the mounting status of the test folder
fdisk -l This command is used to view hard disk partition and partition table information. After executing this command, the detailed information of each partition of all hard disks will be listed, such as device name, size, start and end sectors, etc.
parted -l This command is a relatively powerful partition tool, which can create, delete, adjust partitions, etc. You can use partedcommands to operate interactively, or you can directly display partition information
parted /dev/sda Enter interactive mode and operate the sda ​​disk

df command

When using dfthe command , -hthe file size can be displayed in human-readable form, such as KB, MB, GB, etc., via the option. However, -hthe option can only display the file size in smaller units (such as KB, MB) by default. If you want to display the file size in larger units (such as GB), you need to use -BGthe option.

To display the results of df ./a command , use the following command:

df -BM .
df -BG .

2. View the file size

In Linux, you can use duthe command to view the size of files or folders in the current directory. duThe syntax of the command is generally:

du [选项] [路径]

Commonly used options include:

  • -h: Displays the file size in human-readable form, such as KB, MB, GB, etc.;
  • -s: Only the total size is displayed, not the size of each file and directory;
  • -c: Show the total size, and show the sum on the last line.

The following are several examples of commonly used commands to view the size of files or folders in the current directory:

project Value
du -h View the size of all files or folders in the current directory
du -sh */ View the size of subdirectories under the current directory, without displaying the size of each file
du -h test/ View the size of a folder in the current directory. For example, to view the size of a folder testnamed

View the size of each file or folder in the current directory and display the sum:

du -h | awk '{
    
    total += $1} END {
    
    print "总大小:", total "MB"}'
//注意是否需要除以1024
du -h | awk '{
    
    total += $1} END {
    
    print "总大小:", total/1024/1024 "MB"}'

Among them, awkthe command is used to calculate the sum of the sizes of all files or folders and output the sum in MB.

3. View the number of files

You can use lsthe command to see the number of files in the current folder. lsThe command option -acan display all files (including hidden files), -land can output long format, where the second column indicates the number of hard links of the file. Therefore, the number of files in the current folder can be counted by ls -a | wc -lthe command . It should be noted that since each folder contains at least two directory items, namely "." and "...", the calculation result needs to be subtracted by 2.

The following is the specific command to view the number of files in the current folder:

ls -a | wc -l    # 包括隐藏文件,文件+文件夹
ls | wc -l       # 不包括隐藏文件,文件+文件夹

If you want to exclude subdirectories and only want to count the number of files but not the number of directories, you can use the following command:

ls -l | grep "^-" | wc -l

The in the above command grep "^-"will only match -those starting with a minus sign in the listed file name information, that is, directories are excluded and only files are counted.

Guess you like

Origin blog.csdn.net/shouhu010/article/details/130932530