renaming files containing a space from the Linux command line
the journal of Michael Werneburg
twenty-seven years and one million words
Every year I make a book of photos from the kids' past year. It's my Christmas present to my parents and other family members. This means I'm managing a lot of photo files. Recently my wife's contributions come in the form of .heic files from her iPhone. For some reason my photo-editing software doesn't work well with .heic files so I use a converter to make .png or .jpg files.
Sadly, the converter inserts " conv" into the file name of its output. This introduces a space into the file name. Because my website CMS is designed around a certain file naming convention, I then have to remove the " conv" string from the names.
And that's where batch operations with Linux hit a snag. I've worked around the problem in a two-part script. The first script generates the second, so I run them like this:
./fix.sh > fixes.sh
./fixes.sh
Here are the contents of fix.sh:
#!/bin/basha=$(echo '*conv*') # remember this trick?
for b in $a
do
c="'$b'" # put single quotes around the name containing the space character
d=$(echo $b | sed 's/ conv//')
echo "mv $c $d"
done