Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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've 600gb of data's from Mac Users, saved in a disk formatted in HFS+. A lot of folder and file name contain 'final spaces'. I don't know how users inserted them , but the result is that via Samba, a folder named "Customer ABC " (with the final space) become for example "EHFJ~1". In addition, users used special charachters like • and others strange file name.

How to massive rename that files/folder removing final spaces ? Is it possible using a linux / mac os script ?


share|improve this question
up vote 2 down vote accepted

I have written a small python script to remove all characters from filenames that makes handling them under *nix difficult.

Perhaps this can help you as well.

#! /usr/bin/python
# -*- coding: UTF-8 -*-

usage: FILE...

Renames FILEs to sensible names, avoiding collision. 

import sys
import os
from string import maketrans

def fixFileName(file):
    move file to filename:
    - without spaces, pipe characters, quotes
    intab = ' |'
    outtab = '__'
    trantab = maketrans(intab, outtab)
    newFileName = file.translate(trantab, '\'\"').replace('_-_', '-')
    if file != newFileName:
        #only renames file if it's name contains any unwanted characters
        if os.path.exists(newFileName):
            print "ERROR: Not renaming %s, %s exists already" % (file, newFileName)
            print "renaming %s to %s" % (file, newFileName)
            os.rename(file, newFileName)
#    else:
#        print "file %s and newFilename %s are equal" % (file, newFileName)

if __name__ == "__main__":
    if not len(sys.argv) > 1:
        print __doc__
    for file in sys.argv[1:]:

Anyone is free to use and or improve this. If you have any improvements I'd like to hear about them.

share|improve this answer

Yes, it is! I don't knwo exactly how, but since nobody else answered this yet, I think it is better than nothing. Using some regular expressions, that match final whitespace it should be possible. I don't know however how to get the truncated part of the filename

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.