(-: Home of the backwards smiley.

HOWTO fix filenames on Linux

Date/Time Permalink: 02/01/07 01:00:44 pm
Category: HOWTOs and Guides

A while back, I posted the Bash for-loop one-liner for batch file operations from the command line. Handy though it is, it has limited applications.

I work online, and have to share a lot of files back and forth with clients. A problem that comes up frequently is when a Windows user sends me a glob of files which have spaces in the file names, along with the standard ALL-UPPERCASE characters and ridiculous punctuation characters. It's the Microsoft file system's way of mangling file sharing for everybody just like their web browser mangles web pages. One-liner loops don't always solve the problem cleanly.

But no sweat; here's the Bash script I cobbled up to safely replace spaces with underscores in whatever directory the ugly things pop up:


ls > /tmp/list
# we have to make the list file in a separate 
# directory so that it doesn't get included 
# in the list of the current directory!
mv /tmp/list ./list
cat list | tr ' ' '_' > listnew
# this turns spaces into underscores
FILE_COUNT=$(wc -l list | awk '{print $1}')
while [ "$LOOP" -le "$FILE_COUNT" ]
# the command to feed awk
# output will be 'FNR==1', 'FNR==2', ..
  OLDFILE=$(awk $AWKFEED list)
  NEWFILE=$(awk $AWKFEED listnew)
# keep the quotes to make the shell read file 
# names with spaces as one file

rm list
rm listnew

exit 0

Quite a messy problem, and to my knowledge there is no one-line fix. (Well, you *could* do it in one line, but ugh!)

Calling your attention to a few highlights: the line calling "tr ' ' '_'" swaps spaces for underscores. You can change this with any other annoying characters you get (like bangs (!)) and the preferred character, or make multiple calls to tr in the same line. For that matter, the one-liner fix examples I gave earlier would knock out most cases of UPPERCASE names and other annoyances. This is just the most optimal solution I could come up with for spaces.

Having thus rendered the barbaric output of lesser systems into something more well-behaved, we may then go on with life in our civilized file environment. *sniff*

animated neon sign image

NOTE: If you're wondering why we Linux types get into such a snit over spaces, see why spaces in pathnames are a dumb idea.

Another update note: Not everybody cares what their file names look like. It is more of an aesthetic issue. Which filename is easier to read, "%233NEW%20YORK%21.jpg" or "New_York_3.jpg"? This also bothers command-line cavemen like me more than desktop-only users. For one thing, a standard file naming system makes it easier to automate file management tasks with scripts!

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon