Tuesday, 13 April 2010

Perl One Liner: Remove whitespace from file name

It is often a pain to have spaces in file names when working at the command line, so remove them, or at least replace then with underscores. In this case it works on all bed files in the current directory.

ls *.bed |perl -ne 'use File::Copy;chomp;$old=$_;s/\s+/_/g;move($old,$_);'
You could replace the ls with a find for more control. 

1 comment:

  1. Didn't work for my needs as there were too many files and the ls blew up. Instead I used

    find . -name "*.xml" | perl -ne 'use File::Copy;chomp;$old=$_;s/\s+/_/g;move($old,$_);'

    which worked