Rss Feed
Tweeter button
Facebook button
Digg button
Flickr button
Youtube button
Cryptic Writings …

Replacing characters in multiple file names

by on May.05, 2010, under Technology, Unix programming

I had a large set of shell scripts each of which i needed to run on separate planetLab nodes for which I included the name of the vantage point in every file. I had a pre-string to every file name which i then needed to replace. Unix/Linux makes this things so much more easier in this case.

Original name of file(s): tcpTrace-[vantageName-1].sh

I needed to change the inital string name from tcpTrace to pingTrace: Final name of file(s): pingTrace-[vantageName-1].sh

Since there was over 250 such files in my directory – a manual rename would be really tedious. I wrote the following rather simple shell script to do the job:

#!/bin/bash
for file in tcpTrace*.sh
do
mv $file pingTrace${file#tcpTrace}
done

I use a simple but rather obscure string operator here, the ‘#’. Generally, we know that anything following the # in a shell script is ignored as a comment (except the first line of course). However, the ‘#’ sign can be used within a shell variable to carry out a delete operation. ‘#’ here means that to ‘delete from the left to the first case of what follows’. Further info isĀ here.
In my case, i first add the string ‘pingTrace’ to the start of the file, then take the filename (stored in $file) and delete from that variable, starting from the left till the first case of when ‘tcpTrace’ is encountered. Simple and really effective fix.
No comments for this entry yet...

Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...