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"?
5 Answers
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...
- 466
- 3
- 3
The top answer didn't work on my system. This one did.
To show a test run:
rename -nv ":" "_" *.webp
To run:
rename ":" "_" *.webp
- 708
- 6
- 7
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."
- Save this to a file (say) rename.py.
- Change
rootdirto 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, putrootdir = "/.../My HDD"NOTrootdir = "/.../My\ HDD"(Note the "s) [I'm being this explicit only because you may be unfamiliar with python. No disrespect intended.] - Change
logfileto desired location of logfile. In the end, this file will contain the list of files renamed - for future reference. - 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.
- 1,216
I realize this is a very old post. AeroGT80's recursive solution is very thorough, but it is pretty slow. I had it running for about half an hour on a slower server and it only got about half done - I think it was about 73,000 files in about 300 sub-directories.
The solution below will operate much quicker (it operates on all files in a directory, instead of each file one-by-one - using AeroGT80's non-recursive solution). The only problem is, it only goes down one directory deep (so it's not fully recursive like his solution is), but I'm sure it could be modified easily enough.
#!/bin/bash
for D in *; do
if [ -d "${D}" ]; then
#echo "${D}"
pushd "${D}" > /dev/null
rename 's|:|-|g' *
popd > /dev/null
fi
done
- 211
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)
- 2,116