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.

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"?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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...

share|improve this answer
    
Single directory solution will work but nested solution won't if directories themselves have :s in their names. This is because in the output of find . the name of a subdirectory precedes its contents. So if you have a file /1:/2: first /1:/ will be renamed to /1-/. Now you cannot rename /1:/2: to /1-/2- because /1:/2: doesn't exist. –  S Prasanth Dec 10 '12 at 13:57
    
For directories: find . -type d -readable -writable -exec rename 's|:|-|g' {} \; And then files: find . -type f -readable -writable -exec rename 's|:|-|g' {} \; –  user8290 Dec 10 '12 at 14:07
    
@Christopher The command for directories will fail if there are two levels of directories with : in their names. Eg: /1:/2:/ where 2: is also a directory. Of course, if this is not the case in Ryan's situation, then this will work. –  S Prasanth Dec 10 '12 at 15:13
    
For multiple levels of sub-dirs use: for i in {1..20}; do find . -maxdepth $i -mindepth $i -name "*:*" -exec rename 's|:|-|g' {} \;; done. Change 20 to the maximum depth of the dir. structure. Doesn't have to be exact - can be larger. Will take time if set to a very large number though. –  S Prasanth Dec 12 '12 at 14:18

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)

share|improve this answer

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.

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.