Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
4  
Ouch my ears hurt –  whoami Feb 6 '13 at 10:26
5  
Define "compressed". Are ogg, mp3, gz, zip, xz, rar, 7z, bz2, upx compacted executables, deb, rpm files compressed by your definition? –  Stephane Chazelas Feb 6 '13 at 10:33
add comment

3 Answers

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
share|improve this answer
add comment

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).

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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