So I was in a dilemma. I have a gazillion files that have spaces.
Spaces in linux = really annoying to work with mainly in the shell.

A quick explanation of sed (needed to understand these scripts):

sed 's/thingtoreplace/replacewith/g'

This will replace any instance of “thingtoreplace” with “replacewith” in a whole line of text.
you must remember that sed works on a line by line basis, not file by file.

So I needed a way to rename a large amount of dir’s all at once running a simple bash script…

#!/bin/bash ls | grep ' ' | while read file do mv "$file" echo $file | sed 's/ /./g' done #this replaces all spaces on all dir's (or files) in the current dir with a period.

Basically, this scans through every file it finds with a space and replaces it with a .
Also, I had a lot of files that have a “(year)” format. I hate ( and ) because they also are a pain in the butt in linux. So…

#!/bin/bash ls | grep '(' | while read file do mv "$file" echo $file | sed 's/)//g' | sed 's/(//g' done #this will remove all the (year) style names by replacing the ( and ) with nothing.

So we basically run the result of the file through sed twice to remove both characters.
Hopefully this helps some people. Using this template, you can remove anything you want from your files and dir’s…

Blog Logo

Mario Loria


Published

Image

./scriptthe.net

Because 127.0.0.1 gets old after a while.

Back to Overview