Command Line Learnings
Between recently finishing one of my final semesters, and working full-time, I haven’t had much time to work on my blog, yet alone completing some of the massive backlog of online courses or many project ideas I have yet to start. But, since the semester is coming to an end and I’ve had some more time added to my day due to COVID-19, I’ve been trying to dedicate a good portion of that time to filling in any technical gaps I currently have, or venturing into new territories.
So, today’s topic is Command Line Learnings. I have a master sheet of topics that either I find interesting to learn about, or I feel as though I don’t have a strong background in. When I come across a seemingly good quality tutorial or course on the topic, I like to link those up on my sheet and have a list handy. This helps me to at least set some sort of goal of completion. This time, I decided to work on my bash skills. Since working as an (Infrastructure/Platform) Engineer I’ve been able to learn quite a bit on the command line from CI/CD pipelines, Linux boxes, MakeFiles, even my personal workstation (switched to a Mac from Windows). But, over the past few months I’ve picked up some neat tips and tricks from co-workers and realized that I was missing some of the basics just because they weren’t in my workflow. This led me to try out a trial of Codecademy Pro, specifically the “Learn the Command Line” course to see what I’ve been missing that a beginner course could offer.
The Course
There are definitely different learning styles out there, some folks may learn better by sitting in a lecture hall and reading powerpoint slides, and some may learn better with more hands on approaches. I’m in the latter group for this, and Codecademy always was alluring because I can learn by doing. In the past I did a few syntax courses, Python, HTML, CSS, maybe PHP? But, none of them were really impressive enough for me to want to spend the money on a subscription. They all seemed like glorified syntax courses. Don’t get me wrong, syntax is important, but, once you learn a language I feel like a ‘Hello World’ program suffices for what you need to hit the ground running. Either way, I really enjoyed this course and it would have been pretty useful for me to take a couple of years ago, but, most of the information was just a review.
The course is split up into five different sections (shown below), I think that the most useful section for me was Redirecting Input and Output
. I’ll talk briefly about each of the sections below.
-
Navigating the File System
This section was your basics, getting around the file system, creating directories and files and figuring out where you actually are with commands like
pwd
to see your current path. Totally recommended for beginners but nothing spectacular. They do give you some projects, but they don’t have the same linter that actually checks your code like the other sections do. This seemed a little half-baked to me if this was a paid feature. -
Viewing and Changing the File System
Here we’ve got some more basics, copying files/directories, deleting files/directories, the most helpful part of this section was probably chaining additional flags on to the
ls
command. This is something that is typically a little less intuitive when beginning to learn the terminal. It is also important to stress that these can be chained, so we’ve gotls -a
to display hidden files,ls -l
to display long file format, andls -t
to display files based off of the last time they were edited. Their last section is essentially to show howls -alt
could be chained together to show the detail of each individual command. I felt like this was a good start, but they were missing an importantls
command flag I wanted to add to this. I usels -lh
quite a bit when I want a quick glance of the file sizes in a directory, the-h
flag converts the file size to human readable sizes (bytes, kb, mb, etc) and is really useful. -
Redirecting Input and Output
This was the meat of the course for me, I actually came out with a better understanding of some commands here because of this section. This section covers redirects, appends, pipelining,
sed
,sort
,uniq
, andgrep
. So, I’ve usedsed
andgrep
some in the past, but I wasn’t great on chaining several commands together in bash outside of a CI file. Before it was something I had to put some time into, but, I have a much better grasp now on it. Redirects were also something that I didn’t do on a daily basis, but are very helpful in certain circumstances. I found some good ways to take log file output on a specific error with grep, and direct that into another file that could also be sorted. This to me was a pretty applicable skill that all developers should know how to do, working with log files is essential to debugging your code, and working more efficient with them, especially during a time of degraded service is crucial. If you have the Pro edition I would recommend this course just for this section alone. It was well broken down and I actually used it similar to a sandbox and played around with other pipelined commands while I was learning this.An example of the above, lets say you are looking for a 502 error, and you have a massive log file, you can use something similar to this
grep ‘502’ ./logs.txt | sort >> 502_error_sorted.txt
which would grep the file
logs.txt
for any502
lines, then it would sort them (if necessary), and it would redirect that output to a file we named502_error_sorted.txt
. -
Configuring the Environment
In this section, the primary focus is on customization and environmental variables. I’m pretty familiar with this stuff, using
export
, finding your path, andenv
to list your environmental variables. What I did find useful here though was modifying your bash_profile to add different aliases. While I haven’t yet set up any to use past the tutorial, I can see how they would be useful if I was running a test locally, or setting up environment variables for production/staging environments to easily switch between them. -
Bash Scripting
The final section of the course, this brings together conditionals, loops, and comparison operators. This is something I’ve used in MakeFiles for parsing based off of a condition. However, I always found variables (with no spaces for assignment) and if statements (ending with
fi
) a little odd compared to modern programming. I like how this section was setup, in particular I like how the operator examples were presented. The course has a blob of text on the left that allows you to see exactly what the commands are used for and what they do. In the past I always have to lookup the less than, or greater than comparison operators (-lt
-gt
) but, hopefully this solidifies it some more. Some important takeaways if I was newer to bash scripting would be how variables are handled and called using$
, and one thing that still occasionally trips me up is the spacing in brackets that bash has rules on. For example…if [ $variable -lt 1 ]
Note the spaces between the brackets, if there isn’t any spaces, you can run into some problems. That is a common mistake I made when first learning conditionals in bash about a year ago. I think after dealing with tabs in MakeFiles spacing is something I pay much more attention to now.
Overall, I think the course was pretty good for the ~2-3 hours I spent on it, and I got some good takeaways and a much needed review. As far as whether or not I would actually pay for the Pro version for this, probably not. But, I think there are some other alternative courses such as LinuxAcademy that are similar teaching styles that may allow you to take those for free. If all else fails, there are plenty of resources online to learn the basics!