I'm further extending a previous question to count number of files in tar file (link) to a new question on how to count files under subfolders in a tar file. What I would to have at the end is:
- list the folders that contains files in it
- count the number of files within that folder
My example tar file listing tar -tvf myfile.tar looks like below (the real tar file has more files and directories). There are a total of 2 folders where folder_files_1 has 3 files within and folder_files_2 has 4 files within.
drwxrwxrwx someuser/users 0 2017-08-07 11:43 ./root_folder/subfolder/folder_files_1/
-rwxr-xr-x someuser/users 538962 2017-08-07 11:43 ./root_folder/subfolder/folder_files_1/i716266.MRDC.270
-rwxr-xr-x someuser/users 538962 2017-08-07 11:43 ./root_folder/subfolder/folder_files_1/i716267.MRDC.266
-rwxr-xr-x someuser/users 538944 2017-08-07 11:43 ./root_folder/subfolder/folder_files_1/i716268.MRDC.287
drwxrwxrwx someuser/users 0 2017-08-07 11:50 ./root_folder/subfolder/folder_files_2/
-rwxr-xr-x someuser/users 538696 2017-08-07 11:50 ./root_folder/subfolder/folder_files_2/i717157.MRDC.8
-rwxr-xr-x someuser/users 538694 2017-08-07 11:50 ./root_folder/subfolder/folder_files_2/i717158.MRDC.4
-rwxr-xr-x someuser/users 538692 2017-08-07 11:50 ./root_folder/subfolder/folder_files_2/i717159.MRDC.34
-rwxr-xr-x someuser/users 538696 2017-08-07 11:50 ./root_folder/subfolder/folder_files_2/i717160.MRDC.5
The closest solution I've searched pointed me to using awk after tar (see references here and here).
tar tvf myfile.tar | awk '/^d/ {print $0; /$6/; getline; file_no++} END {print file_no}'
/$6/ is to match the corresponding folder ./root_folder/subfolder/folder_files_1/. But it still is no accurately counting the file numbers under the matching directory, ie. folder_files_1, _folder_files_2.
Any suggestions on how to fix my code?
tar tvf myfile.tar | wc -l