5

How can I check if a file is an archive and then extract it with 7z? I understand that I could check it by file command but it won't work in scripts because of its output. I can't predict what type of archive it could be. I just want to do something like:

Can I extract it by 7z?
If yes, extract,
if not, go further

by bash sript.

  • Do you only care about 7z archives? – Jeff Schaller Mar 3 '18 at 14:04
  • @JeffSchaller The 7z utility can cope with many archive formats including most common ones (if all the necessary plugins are installed). – Gilles Mar 3 '18 at 16:14
  • Why do you care whether the file is an archive? That is, why not just try to extract and ignore (or log) failures? – minnmass Mar 3 '18 at 22:32
  • 4
    Why are you so against using the file command? – roaima Mar 3 '18 at 22:36
  • 1
    The file command doesn't know what you're looking for (it's not an "is it this type of file?" but rather a "what type of file is it?". So exit status can at best only report whether or not it could identify the file. – roaima Mar 4 '18 at 17:55
8

The 7z utility returns a non-zero exit code if the operation it performs fails. You can use this fact to try to extract the archive and then do something else if that fails:

if ! 7z e filename 2>/dev/null; then
    # do something else
fi

or, depending on what else you want to do, or not do,

if 7z e filename 2>/dev/null; then
    exit
fi

# do something else

which may be shortened to

7z e filename 2>/dev/null && exit

# do something else

You could obviously wrap this in

if 7z t filename; then

fi

and catch a failure of extraction (due to not enough disk space or whatever other error might occur during extraction) separately from a failure of determining that this is indeed a 7z archive.

The full code may look like

if 7z t filename 2>/dev/null; then
    if 7z e filename 2>/dev/null; then
        echo 'All is good, archive extracted' >&2
    else
        echo 'Archive failed to extract' >&2
    fi
else
    echo '7z failed to process the file' >&2
fi
  • 'File is not a proper 7z archive' is misleading. NT executables, for example, are not 7z archives. – wizzwizz4 Mar 4 '18 at 12:21
  • @wizzwizz4 Could you explain a bit more please? I know nothing about "NT executables". If 7z t fails, then it is not a proper 7z archive. I chose the word "proper" since it could still be a corrupt 7z archive. – Kusalananda Mar 4 '18 at 12:23
  • The program 7zip can extract Windows NT executable files. Iirc 7z t consent.exe will return 0. It's also the case with .zip, .rar etc.. Your text is confusing. – wizzwizz4 Mar 4 '18 at 12:27
  • @wizzwizz4 Well, in that case NT executables are 7z archives. What would you propose otherwise? How would you distinguish "real 7z archives" from "files that 7z can extract"? Would that even make sense to the questioner? – Kusalananda Mar 4 '18 at 12:32
  • The term "7z archive" usually refers to archives in the 7z format, like zip archive refers to ZIP files. – wizzwizz4 Mar 4 '18 at 12:33
11
filename=/tmp/foo.gz

if 7z t $filename; then
    7z e $filename
else
    echo $filename not an archive.
fi
  • 1
    Why even 7z t? Why not just 7z filename && exit and then do whatever else. – Kusalananda Mar 3 '18 at 14:01
  • if 7z t $filename; then will return 0 if it's a valid archive, so the extract will not happen, you need swap the commands between else and then. – αғsнιη Mar 3 '18 at 14:21
  • 6
    @αғsнιη An exit code of 0 means "success" or "true" in the shell (confusing, I know), so it's the right way round in the answer. – Timo Mar 3 '18 at 14:25
  • On Fedora command 7z t $filename extract archive immediately, or return value different from 0 if it wasn't an archive, isn't It like your snipped extract archive twice? – Michał Mar 3 '18 at 14:38
  • 1
    @Kusalananda If the file is an archive but its extraction fails, your proposal would fall back to the non-archive behavior instead of propagating the error. – Gilles Mar 3 '18 at 16:14
6

If 7z t <archive> returns 0, then the file was a valid archive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.