A Gentle Introduction to the Command Line

The command line is often considered a monolithic, esoteric interface used only by the battle-scarred veterans and hardcore programmers. I was indeed one of those people who avoided the command line like a plague.

Before continuing, let me tell you that my mindset towards the command line has completely changed. After seeing and tasting, I now understand why some developers opt for using the command line when performing certain tasks.

Historically, the command line was the only way to interact with a computer. Nowdsys, the computer is often considered an essential household item. Because of the personal PC, computers now come with an operating system that provides Graphical User Interfaces (GUIs) that runs commands with merely a click of a button/mouse.

Purpose of the Command Line

We now have sophisticated GUIs that provide access to powerful features, are easy to use and understand. Why then, do we have to type in commands to get what we want.

What is it’s purpose? What benefits does it bring?

To those that are unfamiliar with the command line, it may be considered a redundant program. In the hands of the trained, it provides the following benefits

  1. Faster access to certain commands and features (E.g. navigating the file directory).
  2. The ability to perform complex tasks that cannot be achieved with the available features provided by the operating system’s GUIs.
  3. Automation of time-consuming tasks.
  4. Lastly, it is ubiquitous, meaning, it is available on most computers.

Because the command line interface (CLI) provides us with a nice set of built-in commands, it becomes easier for users to automate tasks. Without the command line interface, the very act of automating simple, repetitive tasks becomes more difficult.

Getting Started

Well, that’s all good, but how can I start learning the command line? Good question! Without proper guidance, venturing into this area can feel like being thrown into the deep end of a pool without any proper support.

In the upcoming section, I will be going through some commands to get you started. Just like learning a new command language, only practice and frequent use will ensure that you get used to it. The start is slow and laborious, but as time passes, you will be able to do cool things that you could not do in the past.

Please be aware of the fact that there are different environments, each with their own unique set of commands and parameters. I will be using Git Bash (click to download Git) in my explanations below. FYI, I am running git bash on Windows 10. Install Git and it comes with Git Bash.

Alternatively, if you are using a PC that runs a unix derivative (E.g. Macbook), you don’t have to install anything. Just simply access the terminal and you are good to go.

For those that are curious, bash is a Unix shell shell and command language. It’s command line interface is different to that of Windows, meaning its commands will not work on the Windows command line.

I recommend worrying not too much about the technical details until much later. Let’s get you started.

Common Commands

For each input, you will see parameters in front of a command.

ping -t 192.168.0.1

In the example above, after the ping command, the user inputs -t 192.168.0.1. Notice that the parameters are separated by an empty space. It is through these spaces that the command line interpreter parses the inputs entered by the users and runs the command. So make sure you are including the spaces at the right places.

To see a list of what possible input you can enter for each feature, type --help after the command. If you are running the terminal on a Unix environment, you also have access to man, which can be used like this: man ls. This, in my opinion, provides instructions that are easier to follow, which is why I prefer the Unix like environment as opposed to using Git Bash on windows. Git Bash does not natively support man, which must be downloaded and installed separately.

E.g.

ping --help
ls --help
cat --help

You will see something similar to the following printed on the terminal

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all                  do not ignore entries starting with .
-A, --almost-all           do not list implied . and ..
--author               with -l, print the author of each file
-b, --escape               print C-style escapes for nongraphic characters
--block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
1,048,576 bytes; see SIZE format below
-B, --ignore-backups       do not list implied entries ending with ~
-c                         with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C                         list entries by columns

Sometimes, the instructions can be fairly verbose and often confusing. In such cases, Google is your best friend.

It is also possible to combine the command line options.

# Print the list of files/directories in long format and do not ignore entries starting with .
ls -la

Note that it is possible to split the options into individual pieces E.g. ls -l-a. However, most developers opt for the former, because it requires less typing.

ls

An abbreviation of the word list, it is used to list the information regarding the target directory such as the file name, size, last accessed/modified, etc.

The default command prints out, on the console, a list of the names of available files/directories in the target location.

However, you might want all the available information regarding files in a certain directly. We can ask the computer to fetch us detailed information by passing the -l option.

ls -l

As mentioned in the previous section, we can combine options to perform more powerful and complex operations. For example, the command below will list all files starting with a dot (generally hidden files) in the long format. We already saw one example, which was

ls -la

Now, that is not the only possible combination.  Here is a slightly more complex command.

ls -lrS

The command on the previous line performs the following.

  1. -l – Print the list in long format.
  2. -r – Reverse the order of the sort.
  3. -S – Sort by the file size, largest first.

Essentially what this does is print the files inside of the current directory ordered by file size, with the smallest file appearing first (since we applied -r to reverse the order).

ls -lrS
total 77
-rw-r--r-- 1 Jay Lee 197121   226 1월  18 13:34 README.md
-rw-r--r-- 1 Jay Lee 197121  2450 1월  18 13:34 about.md
-rw-r--r-- 1 Jay Lee 197121  7630 1월  18 13:34 api-documentation.md
-rw-r--r-- 1 Jay Lee 197121  8827 1월  18 13:34 index.html
-rw-r--r-- 1 Jay Lee 197121 49529 1월  18 13:34 api-documentation.pdf

touch

If you can guess what this command does without any prior experience or consultation, you have my respect. Touch creates a new file in the specified location.

# Create a file named newFile.txt
touch newFile.txt

If you take a look at the --help file, you will notice that a majority of the options are geared in manipulating the properties of the target file.

cat

No, the computer does not do anything with an actual cat or generate a bunch of cat pictures when you run this command. Cat reads from a file and outputs the read values onto the console. This can be particularly used for read and writing to another file.

Cat is short for concatentation FYI.

# Read the contents from main.js and append it to combined.js
cat main.js >> combined.js

tar

Used mainly for compressing / decompressing a file, don’t let the plethora of options scare you. Admittedly, the extensive number of options it supports can make it look daunting. In most cases, you will only use it to compress and decompress files.

# Compress, Zip and save to File 'compressedArchive.tar' the following files
# file1, file2, file3, file4 
tar -czf compressedArchive.tar file1 file2 file3 file4

-czf option is telling the compute to Compress, then Zip then write to a file. It will then create a file called ‘compressedArchive.tar’ with the following files.

Conversely, in order to unzip a file, we need to pass in a different set of options

# Extract the contents of compressedArchive.tar into the current directory
tar -xf compressedArchive.tar

Here, -x stands for extract files from an archive. As a matter of fact, you can find these abbreviations in the contextual help provided by typing tar --help as shown below

-x, --extract, --get       extract files from an archive

man

Standing for manual, it offers contextual help on the command. While available immediately in Unix environments, man is not native to Git Bash and must be downloaded separately. Cygwin ships the man command, if deemed necessary. Alternatively, users can refer to --help as show in previous examples

# display ls manual.
man ls

Practice Makes Perfect

Remember, you only retain knowledge through deep immersion. One of the ways to immerse yourself is through practice. Although it may be laborious and rather mundane, in the end, the hours of practice will yield dividends and save you way more in terms of hours and possibly even monetarily.

If you look around, GitHub, you can find some great resources that will help you get your feet wet. One example is Scott Joseph’s GitHub Repo, which contains a fair selection of exercises along with solutions. I recommend downloading the file and removing the solutions before attempting to go through the exercises. This way, you can focus purely on writing the solution without being distracted/tempted to look at the solution.

Code academy is free and has decent courses for those that are just getting started.  Remember though, that the exercises provided are only just enough to get you started. Afterwards, it is up to you to start learning how to mix and match the various pieces.

Benefits of Shell Scripting

Cool, I now know some basic commands. What’s next?

We can tell the computer to execute a set of specific orders. Can’t we do this all using the GUI provided by Microsoft Windows? Hold your horses!

These basic commands are building blocks. Combined together, they can be used to execute specific actions designed to meet your immediate and existing needs.

Although Shell Scripting is outside of the scope of this post, I would like to mention that it is used to string the commands together to perform more complex tasks.

Maybe you need to generate a template or series of files/folders from the contents of a document/text file. Well, shell scripts can make your life a lot easier so that you don’t have to manually create these files. Therefore, I felt that mentioning the possibilities of what you can do with some of your new-found knowledge is a good idea.

Careers where the Command Line may be used

The command line can arguably be utilized by any individual who is willing to look past some of its less appealing features. However, you will likely find individuals using the command line who have the following occupations

  • Accountant
  • Actuary
  • Programmer
  • Software Engineer
  • Tech Support Engineer

There may be other jobs that I have failed to list. If you come across any other possible occupations that is likely to rely on the command line, please let me know!

About the Author Jay

I am a programmer currently living in Seoul, South Korea. I created this blog as an outlet to express what I know / have been learning in text form for retaining knowledge and also to hopefully help the wider community. I am passionate about data structures and algorithms. The back-end and databases is where my heart is at.

follow me on:
0 Shares