For more information, see How to decompress the tar.gz file in the linuxlinuxsystem to view the file size.
If you do not want to decompress the tar.gz file to view the file size
Tar tvf my_file.tar.gz
Output format:
-Rwxr-xr-x root/root 2348366 11:09:46 File_1.txt
-Rwxr-xr-x root/root 2383552 12:52:45 File_2.txt
-Rw-r-root/root 89106866 14:30:01 File_3.txt
The third column from the left is the file size (in bytes), and the third column is the file name. To make it clearer, use awk to filter the output result:
Tar tvf my_file.tar.gz | awk '{print $3, $6 }'
In this way, only 3rd and 6th columns are output, as shown in the following figure:
2348366 File_1.txt
2383552 File_2.txt
89106866 File_3.txt
If you want to display the file size in KB/MB/GB, you can use 3rd columns except N 1024. for example, if you want to display the file size as GB, then:
Tar tvf my_file.tar.gz | awk '{print $3/1024/1024/1024, $6 }'
The number of bytes is 1024, which is GB. the output format is as follows:
0.00218708 File_1.txt
0.00221985 File_2.txt
0.0829872 File_3.txt
If a large file (for example, a hundred GB) is contained in a compressed package, this process will take a long time for shell commands to be executed in the background, it is reasonable to output the result to a file, as follows:
Nohup tar tvf my_file.tar.gz | awk '{print $3/1024/1024/1024, $6}'>/root/result.txt &
Nohup ensures that shell commands are not interrupted (when the network conditions are poor, client software such as SecureCRT may interrupt the connection to the server, and the commands will be interrupted before being executed ), not to mention the redirection character. The final result is saved to the result.txt file in the/root/directory.