2

How can I get the list of all archive files in a folder recursively without knowing the file extension?

2
2

You can use the file utility to attempt to classify a file based on a sequence of filesystem tests, magic tests, and language tests - for example

$ file pynauty-0.5.tar 
pynauty-0.5.tar: POSIX tar archive (GNU)

$ file opencv-2.4.10.zip
opencv-2.4.10.zip: Zip archive data, at least v1.0 to extract

When run on compressed files, the default is just to report that it contains "compressed data", but you can alter that behaviour using the -z switch

 -z, --uncompress
         Try to look inside compressed files.

i.e.

$ file -b octave-4.0.0.tar.gz 
gzip compressed data, from Unix, last modified: Tue May 26 12:35:47 2015, max compression

whereas

$ file -zb octave-4.0.0.tar.gz 
POSIX tar archive (gzip compressed data, from Unix, last modified: Tue May 26 12:35:47 2015, max compression)

Based on that, you could try running file -zb within a find command, and greping for the word "archive": something like

find . -type f -exec sh -c 'file -zb "$1" | grep -q "archive"' _ {} \; -print

The -b switch prevents false matches on the file name.

4
  • The command find . -type f -exec sh -c 'file -zb "$1" | grep -q "archive"' _ {} \; shows nothing with XZ compressed data.
    – A.B.
    Jun 21 '15 at 13:43
  • @A.B. it works for me on .xz compressed tar archives so I guess it's a matter of semantics: does the OP want archives (whether compressed or not) or compressed files (whether - strictly - archives or not) Jun 21 '15 at 13:48
  • I don't know, he mentioned this link: en.wikipedia.org/wiki/Archive_file
    – A.B.
    Jun 21 '15 at 14:00
  • Anyway, it's a good answer +1 =)
    – A.B.
    Jun 21 '15 at 14:00
0

You can check the file type with this command for an archive/file without an extension:

% file tar-latest            
tar-latest: XZ compressed data

or an other example:

% file foo          
foo: Zip archive data, at least v2.0 to extract

To list all archives

find . -type f -exec file {} \; | awk '/compressed|archive/'

Example

% find . -type f -exec file {} \; | awk '/compressed|archive/'
./tar-latest: XZ compressed data
./foo: Zip archive data, at least v2.0 to extract
1
  • @ScepticalJule Were you satisfied with my answer? Then give me an upvote (∧). If I could solve your problem, then it would be nice if you'd mark my answer (✓). askubuntu.com/help/someone-answers ;)
    – A.B.
    Jun 23 '15 at 18:45

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.