18

How to know in Linux whether a particular file is compressed or not using a command?

1
  • 8
    Define "compressed". Are ogg, mp3, gz, zip, xz, rar, 7z, bz2, upx compacted executables, deb, rpm files compressed by your definition? Commented Feb 6, 2013 at 10:33

5 Answers 5

21

You may try using file, for example:

$ file test.sh.gz
test.sh.gz: gzip compressed data, was "test.sh", from Unix, last modified: Wed Feb  6 14:35:33 2013
2
  • Does this work if the file is a symlink to a compressed file?
    – bli
    Commented Jan 15, 2018 at 13:42
  • @bli no, at least not on my Ubuntu here.
    – Leonid
    Commented Feb 8, 2018 at 14:39
5

To add to Ivo Babinec -Leo's answer, You can also have the file command output only the mime-type for a test condition. This is done with the --mime-type flag. The -b --brief flag is necessary to prevent output of the file name.

if [[ $(file -b --mime-type "$yourTestFile") == 'application/gzip' ]]; then
    echo "Yes, yourTestFile is mime-type application/gzip"
fi
4

in bash script

if (file yourTestFile | grep -q compressed ) ; then
     echo "Yes, yourTestFile is compressed"
fi
2
  • 1
    You don't need the parenthesis, and you would want to fix the "commpressed" typo
    – Jeff Schaller
    Commented Feb 7, 2020 at 21:22
  • Thanks, you're right Jeff. Typo corrected, parenthesis left, just to look clearer. Commented Feb 9, 2020 at 16:56
3

Run the file command on it. It will identify compressed files, as well as other common file formats.

Note that ZIP is a common container format. E.g. EPUB and OpenDocument files are actually ZIP files with specific content. My version of file recognises OpenDocument files - but if yours doesn't, it may say that your OpenDocument file is a ZIP file. This may not be the outcome you had in mind :).

To be pedantic, you probably don't mean "is this file compressed", like an OpenDocument, or even an image compressed as PNG or JPEG. You probably mean "is this file an archive", like ZIP, unix tar, or a single-file archive like gzip.

Usually though, you just look at the file extension, like on Windows. Like .ZIP means ZIP file, .gz means gzip. On Linux you're also likely to see .bz2 (bunzip2) and .xz (xz).

1

You can determine whether a file looks like a compressed format by running the file command.

file lorem.txt lorem.txt.gz
lorem.txt:    ASCII text
lorem.txt.gz: gzip compressed data, from Unix, last modified: Thu Feb  7 02:10:44 2013, max compression

file will just say "data" if it doesn't recognize the format. Also, it's up to you to figure out what is compressed or not (e.g. “ASCII text”, “PPM”, “WAVE audio” are uncompressed; “gzip compressed data”, “JPEG image”, “Vorbis audio” are compressed).

Another way to detect if a file looks compressed is to try to compress it. If you can't significantly reduce the size, the file is probably compressed or encrypted.

wc -c somefile
gzip <somefile | wc -c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .