Caution to reader: this blog post is 3 months or older. Blog posts older than three months may contain details about the Hack Reactor program that are no longer accurate. Please refer to other pages on our website to confirm current information and email us with questions.
Graduating student Greg Lull found himself doing a “very anti-programmer thing” while resubmitting a git repository to an automated solution checker:
“I added a comment to each file and made a pull request. Meaning, I manually added the same comment to 40 files in 40 different directories.”
After some reflection, Lull did a very programmer thing and figured out how to automate this manual process. Even better, he shared the solution in a blog post, so that others can learn from his effort. The post explores bash commands and how to use them to append multiple documents with for-loop structures, globbing and re-direction.
“Imagine you created 100 different word documents in 100 different folders but you forgot to add your name to the end of each document,” Lull explains. “Instead of manually opening up each file, scrolling to the end, and writing your name, you can use a very simple 3-line for-loop in the terminal and append any text to each of those files.”
The process incorporates globbing to allow for some nuance while using this process:
“If your filenames or folders have certain patterns to them, e.g. resume_v1.0, resume_v2.0, resume_v2.1, then globbing gives you precision on selecting only the files you want to alter or perform other commands on.”
While Lull’s simple code packs a punch by itself, he plans to expand it so that it can do bigger jobs:
“I would like to build up a stronger bash foundation and create scripts to automate large tasks. For example, recently I have been moving, deleting, and copying a lot of different file types between different folders on my local drive and an online server for testing purposes. Normally this requires multiple commands, but now I can quickly execute this task with one line of code using my own bash scripts.”