Bash Oneliner of the Day

From time to time in my day, I need to do a bunch of edits quickly and easily, so I make up these quick little one line bash scripts to do this for me. From time to time I will post them here so that other might be able to bask in the glory that is a handy dandy “oneliner”.

So… Lets kick this blog of the proper way, with a “oneliner” bash script that will parse all the files in the current directory and rename them with a new name.

for i in `find ./ -name “*template*” -type f`; do mv $i `echo -n $i | sed ‘s/template/quickform/’`; done

Pretty simple huh? What does it do I hear you ask? Well lets break it down (remember that “ or backticks causes everything inside the back ticks to be executed).

for i in `find ./ -name “*template*” -type f`; goes out and searches the current directory for any file (-type f) who’s name contains template and then passes each returned result and places its value in the variable i.

do mv $i `echo -n $i | sed ‘s/template/quickform’`; do is executed each time the value of $i changes from the previous for statement. What this does is executes mv $i and then we echo out the current value of $i to sed via a pipe (used to pass information from one application to another) to sed’s search and replace command (s/needle/replacement) to change the word template in the variable $i with quickform.

done just tells bash that the script is over and to start on the next loop if there is one.

That is all for now… stay tuned for cool things you can do with SSH.

Comments (0)

› No comments yet.

Pingbacks (0)

› No pingbacks yet.