Whenever you’re working with the file directory structure or with text in general, you can’t do any better than using the command line. For example, I was recently copying a bunch of files off of some old CDs because I was noticing that they were starting to develop bit-rot. (I couldn’t access all the files anymore) For reasons I don’t wish to get into right now, it’s easier to not have spaces in filenames in Linux. It’s not because Linux can’t handle spaces in filenames – it can. But if you’re a semi-hacker like me, spaces in filenames can wreak total havoc on your scripts. So I wanted to remove spaces off of around 200 or so files.
I did the first 20 or 30 in Gnome (the GUI) and it was extremely long and tedious. It took me around 15 to 20 minutes. I knew there had to be a better way to do this! So I looked on Google for a script to remove spaces and found the page of my good buddy Penguin Pete.
Now, the script in his main post may look scary, but that’s not important. First of all, you can just copy this into a script and then it’s easy from then on out. Let’s say we call the script stripspaces. Then all you have to do is go to the directory with the files you want to strip the spaces from and then type stripspaces. That’s it! It’s the magic of the command line to have computers do what they do best – a series of repetitive tasks!
But, second of all, a user commented on his blog with an even simpler script:
for x in *\ *;
y=$(echo "$x"| sed y/\ \/_/)
mv "$x" "$y"
That’s IT! Bam! Your spaces are gone. Such is the power of the command line and the reason why I know that anyone who bashes it as outmoded or scary to new users has never had it save them time like this. Oh yeah, and it only took 5 seconds max!
Blogged with Flock