Ruby Developer - Essential Bash commands
Most of us, Ruby programmers, know Ruby pretty well, but how well do you know your console. In this series we dive into improving your bash skills.
So why is this important? Well, you can move a lot of files into well organized folders. All within minutes.
Commands used in the video above:
Or dump all your postgres databases into separate files.
Commands used in the video above:
Improving this skill will make you a skilled programmer, while debugging production servers or doing server configurations.
Most articles about bash focus on individual commands in great detail, instead I will cover a lot of commands with most common uses and focus on combining the commands together to perform powerful actions.
I encourage you, the reader, to download a copy of this repository to follow. It will be much easier to understand and you will gain greater depth of knowledge.
Getting a quick summary from Git
Lets looks at the commands that we will need or this example:
Pipe aka “|”
This is not really a command but a single character. For all the commands following, we are going to use the pipe to feed the output stream from one output to the other. Picture one command printing a single line and passing it to the next command. This links the commands together and allows us to do some powerful things.
Cat
Stands for concatenate. It can combine the output of many files together, but most of the time I just use it to print the output a file.
Example
Single step, output the contents of a single file.
Grep
(globally search a regular expression and print) wikipeida: https://en.wikipedia.org/wiki/Grep#cite_note-3
Grep is your search command. It is the way you filter your data using regular expressions. it can match exact strings or do complex regular expressions to filter the output to a precise match.
If you don’t know what a regular expression is, I highly recommend you download reggy, to try it yourself. I will not be covering regular expressions here.
Example - Full example
We are going to pretend that “repo_log.txt” (See the download sample), contains the output of git log. We are trying to get a summary of all commits done on Mar 22. To do this, we are using the cat command, which just outputs a file to standard out. We then grep for “Mar 22” and using the “-A” (after) option to get the 3 following lines. Finally we grep for some space to get only the commit messages.
Step 1 - output the file (usually done with git log command)
Step 2 - filter out commits we want and Grabbing the date 3 lines before that.
Step 3 - Grab for spaces and Done.
Renaming a large number of files
Here we will rename a large number of files. Let’s look at the commands that we will need for that.
Ls
Simple list command. We all use it everyday, but did you know that you could list a set of files on individual lines using this command. It’s not very well known and it will be really apparent why this important in a little bit. For now here is the command:
Sed
This is where things start to get interesting. The streamed editor. Allows you to easily do substitutions on a console. As well as repetition if needed. Again it uses regular expressions to find and replace. Source
Here we are going to strip out “log-file-“ and “.log” from file names printed and just get dates.
Follow along by running “ruby generating_files.rb” (see the download package).
Step 1
Step 2 filter, just get the files names we want:
Step 3, substutide “log-file-“ with nothing.
Step 4, remove the “.log” on the files
Awk
Pattern-directed scanning and processing language. (From the man page). I generally only use awk to grab individual columns of data from an output. It’s pretty good at guessing columns. This is done by using the ‘{print $1}’ syntax.
In the following command we will rename 200 files all at once.
Step 1. Grab the last command from the last example:
step 2. Tell awk to re-print the first column and add the move command on each line.
Step 3. Pipe into bash. This will execute whatever is printed to the screen one line at a time.
Step 4. Check your output.
Notice that the “super-awesome” and the dates now appear in the front of each line.
I really hope you have enjoyed this brief look at your bash console and how it can speed up your productivity in certain cases. As usual if you have any questions or comments please add them below.