I need to count directories inside an archive file (e.g. tar.bz) without extracting the archive. Note that the archive is too big so that it will be hard to extract then count directories using
ls | wc -l.
This should list all the files from the archive, then find the lines ending with
/ (folders) and then take the line count of it:
tar -tvf file.tar | grep -c '/$'
edit: if you want to only count the top level dirs:
tar --exclude='./*/*' -tvf file.tar | grep -c '/$'
If you want to go recursively one level deeper:
tar --exclude='./*/*/*' -tvf file.tar | grep -c '/$'
and so on...
The only method that works correctly in all cases is to use
star -t -v -f file.tar -find -type d | wc -l
works in all cases, even when the directory names do not end in a slash. The dirty trick to append a slash to directory names has been introduced around 1980, when the tar format did not yet know about the file type "directory". POSIX does not require it and there are tar implementations that do not append the slash to directory names.
Note that you may need to grep for
'^ 0 d' in order to make sure that you only count the beginning of real list lines in case that the pathnames contain newline characters, or by using:
star -t -f file.tar -find -type d -print0 -false
and then to count the number of nul characters in the output like by piping the output to:
LC_ALL=C tr -cd '\0' | wc -c
bsdtar, you can use:
tar tvf file.tar | grep -c '^d'
bsdtar give the file type as the first character on the line in the long list output (
tv) in a similar way as
ls -l and render newlines in file names as
\n, so that still works if archive member names contain newline character.
That's more reliable than looking for trailing
/ characters which like @schily says are not guaranteed to be there.
POSIXly, you can use:
pax -o listopt=%.1M < file.tar | grep -cx d
-o listopt is not supported by all
pax implementations, in particular not the http://www.mirbsd.org/pax.htm pax found on Ubuntu.
pax -v < file.tar | grep -c '^d'
would be more portable but could fail with file names that contain newline characters.