Working with filenames containing spaces in Bash
The problem: spaces in filenames
Sometimes you’ll need to manipulate a list of filenames which contain spaces using a ‘for’ loop. Let’s say you have the following two files:
Command:
$ ls -1
Output:
Aisling, Sanda and Justin.jpg
Monika, Sanda, Justin, Michelle and Ben.jpg
They list perfectly well - one file on each line. (I used the ‘-1’ [minus one] option to ‘ls’ to make a single-column list)
However something strange happens when you want to reference them individually for a ‘for’ loop. Here I use a ‘for’ loop to see how the filenames are being passed to the ‘echo’ command:
Command:
$ for line in $(ls -1) ; do echo ${line} ; done
Aisling,
Sanda
and
Justin.jpg
Monika,
Sanda,
Justin,
Michelle
and
Ben.jpg
Aaagh! The filenames are being chopped up at the spaces and at the newlines!
The solution: a custom value for $IFS</h2>
<p>I need to be able to tell bash that I only want it to chop up lists at the newline character. For this, I modify the value of the built-in variable, IFS. The name stands for 'Internal Field Separator'.</p>
<p>IFS normally has the value " \t\n" - that's to say, space, tab and newline. Let's reset it so that it only contains the newline character:</p>
<p><code>$ IFS=$'\n'</code></p>
<p>See how I specify the newline character? That's important. It's different from how you'd normally set a shell variable. I don't want the literal string '\n', I want what that string represents - a newline.</p>
<p>Now I run the same 'for' loop again:</p>
<p><em>Command:</em></p>
<p><code>$ for line in $(ls -1) ; do echo ${line} ; done</code></p>
<em>Output:</em><code><br /></code><strong><br /></strong><code>Aisling, Sanda and Justin.jpg<br />Monika, Sanda, Justin, Michelle and Ben.jpg</code><br /><br />Now I can manipulate the filenames as I originally intended.<br /><h2>An example: get rid of those spaces!</h2>
<p>Here's a shell script that uses the 'tr' command to rename a list of files, replacing tab and space characters in filenames with underscores:</p>
<p><code>#!/bin/bash<br /><br /># Back up the current value of IFS<br />_IFS=$IFS
IFS=$‘\n’
for line in $(ls -1) ; do<br /> newname=$(echo ${line} | tr ' \t' '_')<br /> escapedname=$(echo ${line} | tr ’ \t’ “ ”)
mv ${escapedname} ${newname}<br />done<br /><br /># Restore IFS<br />IFS=$_IFS
Here’s what my files look like after I’ve run that script:Aisling,_Sanda_and_Justin.jpg
Monika,_Sanda,_Justin,_Michelle_and_Ben.jpg