Ubuntu: Replace All Colons From Filenames With Terminal



Question:

I have a GB's worth of music on my HDD that was formatted with EXT4. I want to move these files to a FAT formatted HDD partition. However, I can't move most of my files because they have ":" in the names (For example, "Act 2: ....." for operas). Is there a way with command line to rename all of my files from "XXXX:XXXX" to "XXXX-XXXX"?


Solution:1

If all your files are in a single directory, try:

rename 's|:|-|g' *  

(where * can be changed to something more restrictive if you'd like)

If you have many files in a directory tree, try this from the base of the tree:

find . -name "*:*" -exec rename 's|:|-|g' {} \;  

You can add the option -n right after rename to have it tell you what it WOULD do without ACTUALLY doing it. This might help you avoid accidentally stepping on other files or something else bad...


Solution:2

Im not at my linux machine right now so I can only give rough information.

Linux has a commandline utility called rename which works with among other inputs, regular expressions or regexes. You should be able to use that along with a regex like "s/:/-/" on your files to achieve the rename you want.

Just to be safe, do it in small batches and/or first test with the simulation (rename has a flag which just gives you the oldname and new name of the file without actually renaming - use this to verify first)


Solution:3

This is a solution in python which handles the case where the files are not necessarily in the same directory.

import os  torename = []  rootdir = "The main directory"  logfile = "Path to log file" #Eg: logfile = "/home/prasanth/renamelog"  for (path, dirs, files) in os.walk(rootdir):      for dirname in dirs:          if ':' in dirname:              torename.append(os.path.join(path, dirname))      for filename in files:          if ':' in filename:              torename.append(os.path.join(path, filename))    print "No of files/dirs to rename =", len(torename)  a = raw_input("Continue? (y/n) ")  if a == 'y':      torename.reverse()      for oldname in torename:          directory, filename = os.path.split(oldname)          newname = os.path.join(directory, filename.replace(':', '-'))          command = 'mv %s %s > /dev/null' % (repr(oldname), repr(newname))          os.system(command)          f = open(logfile, 'a')          f.write(oldname + '\n')          f.close()  else:      print "Aborted."  
  1. Save this to a file (say) rename.py.
  2. Change rootdir to the outermost directory under consideration (Use complete path name starting with /. No shorthands or env variables like ~ allowed). No need to put \ before special characters - for example if the outermost dir is /.../My HDD, put rootdir = "/.../My HDD" NOT rootdir = "/.../My\ HDD" (Note the "s) [I'm being this explicit only because you may be unfamiliar with python. No disrespect intended.]
  3. Change logfile to desired location of logfile. In the end, this file will contain the list of files renamed - for future reference.
  4. Call python rename.py.

Warning: Do test on a small sample before risking an entire GB of music.

Details: Renaming is performed from the innermost files outward.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »