Saturday, March 3, 2007

2-line unix solution to get rid of spaces in filenames

I have over 3000 filenames that looklike 123456789 .txt, to get ride of the space before ".txt". The following two lines will do

% ls *.txt | awk '{printf "mv %s %s\n", $0, substr($0,1,9) }' | bash
% ls | awk '{printf "mv %s %s.txt\n", $0, substr($0,1,9) }' | bash

The first line get rid of not only the space, but also the ".txt", the second line simply add them back.