DEV Community

Dominik Weber
Dominik Weber

Posted on • Updated on • Originally published at domysee.com

Using Scripting to quickly execute repetitive Tasks

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.

  1. Navigate to the directory where the files are and start the interactive ruby shell with irb (if Ruby is installed)
  2. Get all filenames in a variable:
irb(main):001:0> filenames = Dir.glob("*.svg")
Enter fullscreen mode Exit fullscreen mode
  1. 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> }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

With Ruby:

File.size("icon.inline.svg")
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (3)

Collapse
 
rami1983 profile image
rami1983

Did not try ruby yet but after reading this article it sound promissing , i will give it a shot .
Yes bash scripts may get sometimes a little bit ugly.
But recently , i was using bashops.net , it an online tool for writing and sharing bash scripts , it also addresses all bash scripting diffeculties .
Great article , thanks man

Collapse
 
domysee profile image
Dominik Weber

Actually, I didn't want to make it that much about Ruby, but more about any language you can use for that purpose. I think Python might be a good fit too.

Ruby is just the language I sort of know with which I can achieve that.

Collapse
 
rpalo profile image
Ryan Palo

And don’t forget that you can use ‘ruby -e’ to run one liners without opening irb! Nice article, thanks for sharing 😁