This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:
I have a directory full of files where the filenames are hash values like this:
fd73d0cf8ee68073dce270cf7e770b97
fec8047a9186fdcc98fdbfc0ea6075ee
These files have different original file types such as png, zip, doc, pdf etc.
Can anybody provide a script that would rename the files so they get their appropriate file extension, probably based on the output of the file
command?
Answer:
J.F. Sebastian's script will work for both ouput of the filenames as well as the actual renaming.
Here's mimetypes' version:
Example:
Following @Phil H's response that follows @csl' response:
Here's a snippet for old python's versions (not tested):
It should work on Python 2.3.5 (I guess).
You can use
to get a MIME-type. You could potentially lookup the type in a list and then append an extension. You can find a list of MIME-types and example file extensions on the net.
Of course, it should be added that deciding on a MIME type just based on file(1) output can be very inaccurate/vague (what's "data" ?) or even completely incorrect...
Following csl's response:
I'd suggest you write a script that takes the output of
file -i filename
, and returns an extension (split on spaces, find the '/', look up that term in a table file) in your language of choice - a few lines at most. Then you can do something like:in bash, or throw that in a bash script. Or make the get_extension script bigger, but that makes it less useful next time you want the relevant extension.
Edit: change from
for f in *
tols | while read f
because the latter handles filenames with spaces in (a particular nightmare on Windows).Agreeing with Keltia, and elaborating some on his answer:
Take care -- some filetypes may be problematic. JPEG2000, for example.
And others might return too much info given the "file" command without any option tags. The way to avoid this is to use "file -b" for a brief return of information.
BZT