Search code examples
pythonbashperlbatch-rename

Changing filenames that have special characters in bash


We are running a Ubuntu server that automatically FTPs files from a customer and these files, lately, are showing up now as ... 'file.csv;' 'file2.csv;

I have been trying, with no luck, to formulate bash and Python solutions with no luck. I am just trying to strip out the single quotes and semicolons and keep what's left. This doesn't have to be bash, it could be python or even perl. I included code below of what isn't working. I can't even seem to get a directory listing. Can anyone point me in the right direction?

for i in \'* 
    do
    echo $i
done

Note: corrected code to remove errant $echo'


Solution

  • Use find ... -exec rename like so:

    find . -name "*[;']*" -exec rename "tr/';//d" {} \;
    

    Example:

    # Create example input files:
    $ touch "f'o''o'" "b;a;;r;" "b';a;'';z;'"
    
    # Build the command by first confirming that `find` finds them all:
    $ find . -name "*[;']*"                            
    ./f'o''o'
    ./b';a;'';z;'
    ./b;a;;r;
    
    # Find and rename them, one by one:
    $ find . -name "*[;']*" -exec rename "tr/';//d" {} \;
    
    # Confirm that rename worked as expected:
    $ ls -1rt | tail -n 3                                
    foo
    bar
    baz
    

    You can also do a bulk rename for speed using xargs, such as

    find ... -print0 | xargs -0 ...
    

    but in your case I assume that renaming the files one by one is fast enough.


    The command-line utility rename comes in many flavors. Most of them should work for this task. I used the rename version 1.601 by Aristotle Pagaltzis. To install rename, simply download its Perl script and place into $PATH. Or install rename using conda, like so:

    conda install rename