Find & Replace

Gustav H Meyer gustav at
Tue Feb 21 06:45:34 GMT 2006

Hi Dave,

On 21/02/06 01:15, Dave wrote:
> Is there a bash command (similar to awk) that would allow me to search 
> out all the spaces in my file names and replace them with underscores 
> (recursive from home folder)? Or perhaps one of you already has a  
> script that could help with this task? I have Googled this a number of 
> times and feel like I have exhausted all other avenues, even a point in 
> the right direction would be greatly appreciated. Additionally I would 
> like confirmation that what I'm trying to do won't cause problems, most 
> of the files I want renamed are music and picture files.

I thought of writing the steps myself but Marnus van Niekerk at
wrote the following on Sun, 16 Oct 2005 23:08:17 +0200 (SAST):
> You could do it in a couple of steps with something like:
> find /mnt/hd -print | grep " " > /tmp/list1    #find files with spaces
> sed 's/ /_/g' /tmp/list11 > /tmp/list2 #second file with spaces replaced with _ 
> sed -i 's/^/"/' /tmp/list1; sed -i 's/$/"/' /tmp/list1 # Quote filenames with spaces
> paste /tmp/list1 /tmp/list2 > /tmp/list3 # append lines from list2 to list1 
> sed -i 's/^/mv /' /tmp/list3 # add mv to beginning of every line
> Now you have a file in /tmp (list3) that contain all the mv commands to rename your files...
> Check that it looks good and then simply do "sh /tmp/list3"

Gustav H Meyer

More information about the kubuntu-users mailing list