This is a cross-post from my personal blog.

Sometimes I have to do a repetitive task for a project, that would take hours if done manually. Shell commands are normally the way to go, but I've always had a hard time learning them, and after I did, I forgot them quickly, because I need them very infrequently. So I found another solution.

Scripting! Especially with interactive ruby.

For example, recently I had to rename a bunch of files, removing ".inline".

The files I had looked like this:

icon.inline.svg

logo.inline.svg

(and a bunch more)

And they should be renamed to

icon.svg

logo.svg

Okay, lets do this.

Navigate to the directory where the files are and start the interactive ruby shell with irb (if Ruby is installed) Get all filenames in a variable:

irb ( main ): 001 : 0 > filenames = Dir . glob ( "*.svg" )

Rename them in a loop:

irb ( main ): 002 : 0 > filenames . each { | f | irb ( main ): 003 : 1 * File . rename ( f , f . sub ( ".inline" , "" )) irb ( main ): 004 : 1 > }

Done. Quite easy, right?

Now, this is a simple use-case. Looking up a shell command wouldn't take too long.



But what if it gets more complicated?



What if the file needs to contain a special text? What if the file size has to exceed a particular limit? What if ...

You know where I'm getting at.



With Ruby, I know what to do. Just look up whatever I don't know and combine everything with the constructs I, as a developer, am familiar with.

Since I have never extensively used it, I'd have a lot more problems with shell commands/scripts. I'd need to look up how to do the subproblems (e.g. getting the file size) AND how to combine them with pipes, arrows and whatever else exists.

Additionally, Ruby statements are usually a lot easier to remember. Staying with the example of getting the file size, this is how it works with bash (accepted answer in the first Google result):



stat -f %z icon.inline.svg

With Ruby:



File . size ( "icon.inline.svg" )

Doesn't Ruby look a lot more straightforward?

So, if you're like me and don't know how to do complex tasks with shell, give interactive ruby a chance. It will infinitely speed up those annoying repetitive tasks.

Follow me on Twitter for more of my thoughts, articles, projects and work.