4

I want to get the number of files (included directories) contained in the root of a 7z archive, but the 7z command on Linux don’t bring a simple way to only get the number and noting else.

For example I have the 7zarchive.7z file witch contain archive.tar and archive.gzip. Then, 7z l 7zarchive.7z print me the following verbose lines:

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
p7zip Version 9.20 (locale=fr_FR.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)

Listing archive: 7zarchive.7z

--
Path = 7zarchive.7z
Type = 7z
Method = LZMA
Solid = +
Blocks = 1
Physical Size = 495
Headers Size = 184

    Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2016-03-14 22:43:10 ....A        20480          311  archive.tar
2016-03-14 23:04:01 ....A          163               archive.gzip
------------------- ----- ------------ ------------  ------------------------
                             20643          311  2 files, 0 folders

In the right bottom, you can see “2 files, 0 folders”, so I want to get the sum of number of folders and number of files.

For this, I try the flowing hideous command : bash 7z l 7z l *.7z | tail -1 | sed -e 's/.* \([0-9]*\) files\, \([0-9]*\) folders/\1 \2/' -e 's/\([0-9]*\) \([0-9]*\)/\$(( \1 + \2))/' | tail -1 | sed -e 's/.* \([0-9]*\) files\, \([0-9]*\) folders/\1 \2/' -e 's/\([0-9]*\) \([0-9]*\)/\$(( \1 + \2))/'

  1. tail extract the last line;
  2. The first sed expression extract the numbers before the words “files” and “folders”.
  3. The second sed expression formate it to be printed like this $(( <first number> + <second number>)).

So, I really obtain the output $(( 2 + 0)), but I want it to be executed to get only the result (2) and not the command to calculate the result.

So, there is a way to do this, or, at list, a more simple way to get the number of files contained by a 7z archive?

2 Answers 2

3

It is simpler if you use positional values, e.g.,

7z l 7zarchive.7z | tail -n 1 |awk '{ print $3 + $5; }'

giving the total of files + folders.

In the last line

20643          311  2 files, 0 folders

$1 is "20643", $2 is "311", $3 is "2", $4 is "files,", $5 is "0" and $6 is "folders". You could make it more definite using a regular expression to match the line.

2
  • I qualified the comment with a note about a regular expression before you made a comment about it. Commented Mar 17, 2016 at 23:33
  • 1
    It’s also a good solution but this also count subfiles and subdirectories. Commented Mar 17, 2016 at 23:33
1

I finally find it, it was more simple that I imagine: 7z l -slt <nameof archive> | grep -c 'Path = [^/]*$'.

1
  • 1
    This code counts the archive itself because the output of 7z l -slt includes a section for it. For example, for an archive containing two files, this code will output 3. Commented Oct 2, 2016 at 21:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.