Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 89871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:47:04+00:00 2026-05-10T22:47:04+00:00

This is a question regarding Unix shell scripting (any shell), but any other "standard"

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:47:05+00:00Added an answer on May 10, 2026 at 10:47 pm

    Here’s mimetypes’ version:

    #!/usr/bin/env python '''It is a `filename -> filename.ext` filter.      `ext` is mime-based.  ''' import fileinput import mimetypes import os import sys from subprocess import Popen, PIPE  if len(sys.argv) > 1 and sys.argv[1] == '--rename':     do_rename = True     del sys.argv[1] else:     do_rename = False      for filename in (line.rstrip() for line in fileinput.input()):     output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate()     mime = output.split(';', 1)[0].lower().strip()     ext = mimetypes.guess_extension(mime, strict=False)     if ext is None:         ext = os.path.extsep + 'undefined'     filename_ext = filename + ext     print filename_ext     if do_rename:        os.rename(filename, filename_ext) 

    Example:

     $ ls *.file? | python add-ext.py --rename avi.file.avi djvu.file.undefined doc.file.dot gif.file.gif html.file.html ico.file.obj jpg.file.jpe m3u.file.ksh mp3.file.mp3 mpg.file.m1v pdf.file.pdf pdf.file2.pdf pdf.file3.pdf png.file.png tar.bz2.file.undefined 

    Following @Phil H’s response that follows @csl’ response:

    #!/usr/bin/env python '''It is a `filename -> filename.ext` filter.      `ext` is mime-based. ''' # Mapping of mime-types to extensions is taken form here: # http://as3corelib.googlecode.com/svn/trunk/src/com/adobe/net/MimeTypeMap.as mime2exts_list = [     ['application/andrew-inset','ez'],     ['application/atom+xml','atom'],     ['application/mac-binhex40','hqx'],     ['application/mac-compactpro','cpt'],     ['application/mathml+xml','mathml'],     ['application/msword','doc'],     ['application/octet-stream','bin','dms','lha','lzh','exe','class','so','dll','dmg'],     ['application/oda','oda'],     ['application/ogg','ogg'],     ['application/pdf','pdf'],     ['application/postscript','ai','eps','ps'],     ['application/rdf+xml','rdf'],     ['application/smil','smi','smil'],     ['application/srgs','gram'],     ['application/srgs+xml','grxml'],     ['application/vnd.adobe.apollo-application-installer-package+zip','air'],     ['application/vnd.mif','mif'],     ['application/vnd.mozilla.xul+xml','xul'],     ['application/vnd.ms-excel','xls'],     ['application/vnd.ms-powerpoint','ppt'],     ['application/vnd.rn-realmedia','rm'],     ['application/vnd.wap.wbxml','wbxml'],     ['application/vnd.wap.wmlc','wmlc'],     ['application/vnd.wap.wmlscriptc','wmlsc'],     ['application/voicexml+xml','vxml'],     ['application/x-bcpio','bcpio'],     ['application/x-cdlink','vcd'],     ['application/x-chess-pgn','pgn'],     ['application/x-cpio','cpio'],     ['application/x-csh','csh'],     ['application/x-director','dcr','dir','dxr'],     ['application/x-dvi','dvi'],     ['application/x-futuresplash','spl'],     ['application/x-gtar','gtar'],     ['application/x-hdf','hdf'],     ['application/x-javascript','js'],     ['application/x-koan','skp','skd','skt','skm'],     ['application/x-latex','latex'],     ['application/x-netcdf','nc','cdf'],     ['application/x-sh','sh'],     ['application/x-shar','shar'],     ['application/x-shockwave-flash','swf'],     ['application/x-stuffit','sit'],     ['application/x-sv4cpio','sv4cpio'],     ['application/x-sv4crc','sv4crc'],     ['application/x-tar','tar'],     ['application/x-tcl','tcl'],     ['application/x-tex','tex'],     ['application/x-texinfo','texinfo','texi'],     ['application/x-troff','t','tr','roff'],     ['application/x-troff-man','man'],     ['application/x-troff-me','me'],     ['application/x-troff-ms','ms'],     ['application/x-ustar','ustar'],     ['application/x-wais-source','src'],     ['application/xhtml+xml','xhtml','xht'],     ['application/xml','xml','xsl'],     ['application/xml-dtd','dtd'],     ['application/xslt+xml','xslt'],     ['application/zip','zip'],     ['audio/basic','au','snd'],     ['audio/midi','mid','midi','kar'],     ['audio/mpeg','mp3','mpga','mp2'],     ['audio/x-aiff','aif','aiff','aifc'],     ['audio/x-mpegurl','m3u'],     ['audio/x-pn-realaudio','ram','ra'],     ['audio/x-wav','wav'],     ['chemical/x-pdb','pdb'],     ['chemical/x-xyz','xyz'],     ['image/bmp','bmp'],     ['image/cgm','cgm'],     ['image/gif','gif'],     ['image/ief','ief'],     ['image/jpeg','jpg','jpeg','jpe'],     ['image/png','png'],     ['image/svg+xml','svg'],     ['image/tiff','tiff','tif'],     ['image/vnd.djvu','djvu','djv'],     ['image/vnd.wap.wbmp','wbmp'],     ['image/x-cmu-raster','ras'],     ['image/x-icon','ico'],     ['image/x-portable-anymap','pnm'],     ['image/x-portable-bitmap','pbm'],     ['image/x-portable-graymap','pgm'],     ['image/x-portable-pixmap','ppm'],     ['image/x-rgb','rgb'],     ['image/x-xbitmap','xbm'],     ['image/x-xpixmap','xpm'],     ['image/x-xwindowdump','xwd'],     ['model/iges','igs','iges'],     ['model/mesh','msh','mesh','silo'],     ['model/vrml','wrl','vrml'],     ['text/calendar','ics','ifb'],     ['text/css','css'],     ['text/html','html','htm'],     ['text/plain','txt','asc'],     ['text/richtext','rtx'],     ['text/rtf','rtf'],     ['text/sgml','sgml','sgm'],     ['text/tab-separated-values','tsv'],     ['text/vnd.wap.wml','wml'],     ['text/vnd.wap.wmlscript','wmls'],     ['text/x-setext','etx'],     ['video/mpeg','mpg','mpeg','mpe'],     ['video/quicktime','mov','qt'],     ['video/vnd.mpegurl','m4u','mxu'],     ['video/x-flv','flv'],     ['video/x-msvideo','avi'],     ['video/x-sgi-movie','movie'],     ['x-conference/x-cooltalk','ice']]  #NOTE: take only the first extension mime2ext = dict(x[:2] for x in mime2exts_list)  if __name__ == '__main__':     import fileinput, os.path     from subprocess import Popen, PIPE      for filename in (line.rstrip() for line in fileinput.input()):         output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate()         mime = output.split(';', 1)[0].lower().strip()         print filename + os.path.extsep + mime2ext.get(mime, 'undefined') 

    Here’s a snippet for old python’s versions (not tested):

    #NOTE: take only the first extension mime2ext = {} for x in mime2exts_list:     mime2ext[x[0]] = x[1]  if __name__ == '__main__':     import os     import sys      # this version supports only stdin (part of fileinput.input() functionality)     lines = sys.stdin.read().split('\n')     for line in lines:         filename = line.rstrip()         output = os.popen('file -bi ' + filename).read()                 mime = output.split(';')[0].lower().strip()         try: ext = mime2ext[mime]         except KeyError:              ext = 'undefined'         print filename + '.' + ext 

    It should work on Python 2.3.5 (I guess).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, to send the screenshot, you can use… May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer I figured it out: window.nativeWindow.x = (Screen.mainScreen.bounds.width - window.width)/2; window.nativeWindow.y… May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer Ok I solved my problem. It was not Message.WriteMessage that… May 11, 2026 at 11:41 pm

Related Questions

After, reading and understanding Dan Benjamin's post about installing Ruby, Rails, etc. on OSX
I've queued up over 10,000 files to be uploaded to a UNIX based FTP
This is a continuation of my question about reading the superblock . Let's say
Can anyone point to some code that deals with the security of files access

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.