Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
automatically identify filetypes
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Sujao
l33t
l33t


Joined: 25 Sep 2004
Posts: 677
Location: Germany

PostPosted: Tue Nov 30, 2004 5:20 am    Post subject: automatically identify filetypes Reply with quote

Hello everyone,

I recovered 10.000 files from a broken fat32 partitions and they are all named file####.chk. Is there any program for linux that can recognize the file type automatically and rename them?

It would be enough if it recognizes images (jpg, gif, bmp), video (mpg, avi, mov) and audio (mp3, ogg).
Back to top
View user's profile Send private message
codergeek42
Bodhisattva
Bodhisattva


Joined: 05 Apr 2004
Posts: 5142
Location: Anaheim, CA (USA)

PostPosted: Tue Nov 30, 2004 5:24 am    Post subject: Reply with quote

Code:
# emerge file
$ file /path/to/file
? For instance:
Code:
/home/peter $ file "Music/Foo Fighters/One by One/07 - Halo.ogg"
Music/Foo fighters/One by One/07 - Halo.ogg: Ogg data, Vorbis audio, stereo, 44100 Hz, ~192000 bps, created by: Xiph.Org libVorbis I (1.0.1)

_________________
~~ Peter: Programmer, Mathematician, STEM & Free Software Advocate, Enlightened Agent, Transhumanist, Fedora contributor
Who am I? :: EFF & FSF
Back to top
View user's profile Send private message
Sujao
l33t
l33t


Joined: 25 Sep 2004
Posts: 677
Location: Germany

PostPosted: Tue Nov 30, 2004 5:33 am    Post subject: Reply with quote

I didnt find any possibillity to automatically rename the files. Do you know how to do this?
Back to top
View user's profile Send private message
codergeek42
Bodhisattva
Bodhisattva


Joined: 05 Apr 2004
Posts: 5142
Location: Anaheim, CA (USA)

PostPosted: Tue Nov 30, 2004 5:50 am    Post subject: Reply with quote

Sujao wrote:
I didnt find any possibillity to automatically rename the files. Do you know how to do this?
You might be able to write a small shell script to parse the output of `file <blah>` and use mv to rename it, but other that that, no I don't think it can automatically rename the files.
_________________
~~ Peter: Programmer, Mathematician, STEM & Free Software Advocate, Enlightened Agent, Transhumanist, Fedora contributor
Who am I? :: EFF & FSF
Back to top
View user's profile Send private message
Sujao
l33t
l33t


Joined: 25 Sep 2004
Posts: 677
Location: Germany

PostPosted: Tue Nov 30, 2004 6:19 am    Post subject: Reply with quote

Hmm....this is getting pretty complicated. Do you know where I can find a tutorial with some basics about scripts? Should be really simple cause I dont want to spend the whole day with learning it.

EDIT:

I think this is what I need to do. I need to check 10k lines for the following:

if line contains "jpg" then execute "mv [part of line up to colon - 3 chars] [part of line up to colon].jpg"

if ...... "mpg" .............................[...].mpg etc.
Back to top
View user's profile Send private message
fishhead
Apprentice
Apprentice


Joined: 07 Mar 2003
Posts: 162
Location: Pasadena, CA

PostPosted: Tue Nov 30, 2004 8:09 am    Post subject: Reply with quote

What you want to do is probably something like:
Code:
for name in *.chk; do
    extension=$( file -ib $name | sed 's:/:.:' )
    mv -v $name $name.$extension
done


You'll need to be in the directory the files are in for this to worl.

I guess since you want to learn shell scripting I can go ahead and tell you how this works. What this does is the following ..

1) The for name in *.chk line gets read. The shell treats * as a "wildcard character" meaning "any character occours and this happens zero or more times" and will internaly repace *.chk with all files that meet this criteria (in this case, file0001.chk file0002.chk .... and so on).
2) The shell will take the now expanded set of file names and will one by one assign these values to the shell variable 'name'. It will run the contents of the loop for each assignment.
3) In the loop, the line extension=$( file -ib $name | sed 's:/:.:' ) gets read. The expression extension= assigns whatever follows the equals to the shell variable 'extension'. The $( commands ) will run commands and then act as if the command's output (what you'd normaly see on the terminal) were typed in place of $( commands ). The file -ib $name command returns the mime type for the file that is assgned to the variable 'name' (so you use $variable to access the valuve in variable). Look at the man page to see what the -ib flag does. For a jpeg file this would be "image/jpeg", for a ogg-vorbis file, this would be "application/ogg", and so on. The | is a pipe. It takes the output of the first command, file, and sends it as input to another program, in this case sed. The sed command is pretty powerful, so I won't go into it in detail, but it has several commands, one of which is 'substitute' or 's'. The sed 's:/:.:' reads in one line at a time (and here, file -ib $name only provides one line, the one with the filetype) . The 's:' command in sed is followed by a ':'. This (or whatever is the first character after s) is what the 's' command will use to split up its arguments. the first argument, "/" is the thing to be replaced and the second argument '." is what to replace it with. In other words, this turns file's output, "image/jpeg", into "image.jpeg" and prints it out. Once this is done, the output that sed made, say "image.jpeg", is substuted in place of $( file -ib $name | sed 's:/:.:' ) , so this line would this time through the loop be extension=image.jpeg, so this time through the loop, extension would have the value 'image.jpeg'
4) The line mv -v $name $name.$extension should be prety clear now. It takes the file named '$name' and moves it to "$name.$extension". So it might move file0000.chk to file0000.chk.image.jpeg. The -v option makes mv print out what its doing.
5) After doing this for the first value that matches *.chk, the shell will continue to do these commands for each other file that *.chk expands to.

I hope this helps. I'd reccomend getting a book on shell programming that teaches the basics. You can always look at the man pages or the info pages, but if you're just starting off, they might be a bit terse. Once you get used to them though, you should be able to pick most stuff up by reading them with no problem (and they have a few advantages over books, namely that they're free, they are straight and to the point, and right on the computer).
Back to top
View user's profile Send private message
Sujao
l33t
l33t


Joined: 25 Sep 2004
Posts: 677
Location: Germany

PostPosted: Tue Nov 30, 2004 9:35 am    Post subject: Reply with quote

Thx alot fishhead, meanwhile I read this tutorial so I know the basics now but I didnt know about sed and its also good to have an example. I will try your script after breakfast :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum