15

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? – Stéphane Chazelas Feb 6 '13 at 10:33
20

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 Jan 15 '18 at 13:42
  • @bli no, at least not on my Ubuntu here. – Leonid Feb 8 '18 at 14:39
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
1

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 Feb 7 '20 at 21:22
  • Thanks, you're right Jeff. Typo corrected, parenthesis left, just to look clearer. – Ivo Babinec - Leo Feb 9 '20 at 16:56

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.