Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Mac OS 10.8.4, and want to do some batch renaming using the Terminal which is a bash shell I think. I have a large number of folders of large number of files. The filenames have words common to other files in that folder which I'd like to detect and remove (excluding the file suffix).

For example:

Folder 1 has files:

blo 123b 2blah 5blah.pdf
blo hazel 124 2blah 5blah.pdf
blo 125 2blah 5blah.pdf

Folder 2 has files:

534a blee9.pdf
b546 blee9.pdf
back 675 blee9.pdf

And I want to rename these to

Folder 1

hazel 124.pdf

Folder 2

back 675.pdf

I have applications that can find and replace, but these require me to manually specify what to find. But I won't know in advance what these are without manually looking. Furthermore, there might be more than one word to replace.

So I was thinking I could do something like this:

for each folder {
list all the filenames > output.txt
use grep, replace all word breaks with carriage returns
sort the list alphabetically
use uniq in some way to detect the words I want to remove, and list them
iteratively remove all these words from the file names

A few extra considerations: a 'word' may contain characters like =,-,+ and so on. I want to use [space] as my word divider.

Can anyone help with this?

share|improve this question
Phrased differently, couldn't you just rename each file with the first pattern of three contiguous digits found, or is it not that trivial? – slhck Sep 9 '13 at 9:13
I think digits are an example. I think for example he would like to remove the artist/album words from mp3 files to only keep track # and name. So only remove whatever words all files have in common. – mveroone Sep 9 '13 at 9:20
@Kwaio If that's the case (MP3 file names) then the OP should ask about that directly without any abstract examples. That makes it easier to come up with a more suitable solution. – slhck Sep 9 '13 at 9:35
Once again, that's just an example I made up, no idea what he wants, that's why I tried to make my answer generic. – mveroone Sep 9 '13 at 9:36
@Kwaio that's the drift of what i want, although not mp3 files. I gave abstract examples to avoid an abstract description. – Tim Sep 9 '13 at 19:27
up vote 0 down vote accepted

I would do it in perl, because i'm not that good with bash.

  1. loop over all folders recursively (look for a tree-traversal algorithm, rather simple)
  2. For each folder, list all files with ls
  3. use split(' ', sfilename); to make a list of words used. Insert in a list @common_words
  4. For each next file, split its name the same way, and remove any word from @common_words not in $filename. (with perl's grep for example, or use a loop)
  5. Once you have the list of common words, re-loop over files, and for each
  6. For each file, for each word, rename the file to remove the word (using bash's mv with perl's system)
  7. done.

Here is a starting script that would need reworking but addresses your problem.

Problems :

  • If your files all finish with "_word.ext", the extensuion will be removed
  • If you uncomment the substitution supposed to ignore the extension, the mv's don't work anymore, so you would have to save the name in a hash : name_without_extention => "name_with_extension'
  • It only parses a single folder and also uses the subfolders names, so you have to make sure to use it in a folder without subfolder.

Now it's up to you to improve it =)

share|improve this answer

I have written the code for files within one directory. Firstly I navigate to that directory in Terminal then I run the python script. It works really well for removing parts of file names such as '720dpi' without needing to know the exact text beforehand. I am a beginner at Python so the code could probably be improved.

share|improve this answer

Your Answer


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.