Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any command which tells me in the specific directory which types of files exist?

I can find out the file type by using a command like od -c myfile | less.

But I don't know how to do it for all files in a directory.

share|improve this question

3 Answers 3

up vote 12 down vote accepted

Although od -c will indeed show the contents of a file, it is not a good way to get its file type. While some files will contain a header with the file type, not all will. A better way is the command file:

$ echo "hello" > foo.txt
$ file foo.txt
foo.txt: ASCII text

So, to get a list of all file types in a directory, you can do:

for file in dir/*; do file "$file" | cut -d: -f 2; done | sort -u

Example output:

 PNG image data, 1500 x 500, 8-bit/color RGBA, non-interlaced
 ASCII text
 directory
 GIF image data, version 89a, 22 x 22
 ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=becf821e4d814fdb69306d0b3f686eb06992f5e5, stripped

Explanation

  • for file in dir/*; do ... done; : iterate through everything in dir (dir is just an example, you should change this to the name of the actual directory you want to search through), saving each item in turn as $file
  • file "$file" : run file on each of the items found.
  • cut -d: -f 2 : print only the second field (fields defined by :)
  • sed 's/^ //; s/ +/ /g' : remove spaces from the beginning of the line and convert consecutive spaces into a single space.
  • sort -u : remove duplicate file types
share|improve this answer
    
thanks, But I exactly copy and past "for file in dir/*; do file "$file" | cut -d: -f 2 | sort -u; done" in my terminal and I got " ERROR" –  lion Apr 2 at 17:15
1  
@alex first of all, please always mention the exact error you get, saying that you got "ERROR" is not very useful. In any case, the error is probably because you have no directory named dir/, that was just an example name, you will need to change that to the name of a real directory: for file in /home/alex/*; do ... done for example. –  terdon Apr 2 at 17:18
    
thanks, If you did my wrong work(...I thought dir is command like ls not directory -sorry- also I think better to edit your answer)You just see "ERROR" and nothing else. –  lion Apr 2 at 17:22
    
nice job, thnaks, but I saw for example four line "Audio file with ID3 version 2.3.0, contains" or more than one"ISO Media, MPEG v4 system, version 2 ". I am not sure about "remove duplicate". –  lion Apr 2 at 17:35
    
@alex fair enough, answer edited. You should not be getting any duplicates. The sort -u will remove them, the lines must be different (they might also have a different number of spaces, that makes them non-identical). –  terdon Apr 2 at 17:40

I would probably do something like this -

find . -type f -exec file {}  \;

That will search from the current path, for files (e.g. no directories) and then execute the file command on each file.

share|improve this answer

Try this:

ls | xargs file -b | sort | uniq

How it works:

ls: list directory contents

file -b: determine file type; do not prepend filenames to output lines

sort: sort lines

uniq: omit repeated lines

share|improve this answer

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.