CHAPTER 9

Hacking the Mainframe

TIME LIMIT: 5 HOURS

Whenever someone is hacking a computer in a movie, they are often depicted using a command line interface. The hacker is generally seen controlling the computer by entering text commands - as opposed to the point-and-click commands of graphical user interfaces.

In this chapter, we are going to get acquainted with this powerful command line. We will use the command line to explore and navigate your operating system and its attendant filesystem. We will learn how to write commands to create and delete files and folders (a folder is also often referred to as a directory).

We will be using the command line in almost every chapter up to Part 5, so don’t worry about memorizing this particular chapter - you are going to get lots of practice! I recommend starting a list of commands you find useful in your Coding Journal. Appendix B: Workflow Cheatsheet also contains many common, useful commands.

Let’s begin by talking about how the command line serves as the user controls for your operating system.

Operating Systems and Command Lines

In the last section, we installed a source code editor. However, the source code (“application code”) we are going to write can’t operate itself – it needs an operating system. An operating system is the connection between source code and the actual computer hardware that all source code runs on. As a developer of source code, you must learn to talk to your operating system. You must learn to use commands to tell it what to do.

As of 2019, the most important operating systems are Linux, macOS, and Microsoft Windows.

It is worth noting Linux’s prominence. Linux is open-source and is by far the most commonly used operating system in the world. Most of the world’s “smart” devices run on it. All of the 500 most powerful computers in the world run on it. Android is a Linux variant. Most websites’ HTML, CSS, and JavaScript files are hosted on computers (“servers”) running the Linux operating system.

On Linux and macOS, you can access the command line via a program called Terminal (Microsoft uses a program called Powershell). Inside the Terminal program, both Linux and macOS are running a particular type of the CLI software called bash, which is an acronym for Bourne Again Shell. You can configure the Terminal to use other shells, but most people stick with bash.

Because Macs and Linux are both running bash, it is easy write Linux-compatible code on a Mac. This is a big reason why many professional web developers use Macintoshes instead of Windows machines (unless developing specifically for the Microsoft software ecosystem). In its quest to keep up, Microsoft recently added support for the bash shell inside Powershell, but most developers are still using macOS or Linux. But we digress.

The terms shell, command line, bash and Terminal are often used interchangeably in everyday practice. We will take the same laissez-faire approach to the terminology throughout the rest of the book, but take note: All of the commands in this book were written for bash. They will work on Linux and macOS Terminal programs. Microsoft Powershell users will need to look up the corresponding commands.

If you are going to become a power user of computers, familiarity with the command line is an absolute requirement. Let’s hack the mainframe.

Hello, Terminal.

Go find the Terminal program on your computer and open it up. I recommend adding it to your desktop dock. When I open my Terminal, it looks like this:

Yes, there is a hamburger emoji in my command prompt. Bash is highly configurable.

Let’s begin by creating some directories and files on your Desktop. Type in the following commands. Hit ENTER after each command to make it run.

pwd
cd ~/Desktop
pwd
ls
mkdir hello
cd hello
touch index.html
open .

You just created a directory named hello on your Desktop and a file named index.html inside that directory. You then opened the directory in your graphical finder program (or whatever else your default program is for opening files). See it?

Now let’s delete the index.html file and then hello directory.

rm index.html
cd ..
ls # everything after a pound symbol is a comment
rmdir hello
ls

Anything you can do using the graphical interface, you can also do from the command line.

pwd
cd ~/Desktop
pwd
touch bye.html
echo "Piping text into a document" > bye.html

A bye.html file should be on your Desktop. Open it, either by manually double-clicking it on your Desktop or open it on the command line.

open bye.html

This should have opened bye.html in your default browser. If you would like to instead open bye.html in your text editor, you could use the following command (assuming you downloaded the Atom editor in your last chapter):

open bye.html -a atom

To delete the file:

rm bye.html

The Structure of Commands

Commands are made up of three parts: the actual command, which is required, and then also options and arguments, which are not required. The general syntax of a command is structured like this:

command -options arguments

Commands are verbs - actions to take. For example: copy (cp), change directory (cd), print working directory (pwd), list files in directory (ls), make a file (touch), or make or remove a directory (mkdir, rmdir, respectively).

Options are adverbs – they modify how the command is run. They are optional. They are preceeded by one - dash if you are using single letter options. If using an option requires that you type out more than one character, you precede it with two -- dashes.

Arguments are the subject - the thing the verb and adverbs are acting on.

An example often illuminates. All of the following are valid commands:

ls
ls -lah
ls -lah ~/Downloads

Let’s break these down.

ls - This first command simply lists whatever visible files are contained in your current working directory. If you are unsure what your current working directory is, you can type pwd to print it.

ls -lah - This is the same as ls, but with three, separate, single-character options added, l, a, and h. -l means list the files in the “long” way, i.e. don’t just show me the files, show me some metadata, like file size. The -a option means show me “all” of the files, even the ones that are hidden (more on this in a minute). The -h option means show me the size of the files in a “human-readable” way, i.e. write “1kb” instead of 1000 bytes.

Because all three of these options are single-characters options, they can be lumped together and prepended with a single dash, -lah. You could also run this command as ls -l - a -h if you wanted!

ls -lah ~/Downloads - This means the same as ls -lah, but instead of listing files from your current working directory, you are listing the files in the directory located at ~/Downloads.

Filesystems

An operating system is really just a bunch of source code files. These files are organized into a bunch of different folders (directories). There is one “root directory” that contains all of the other folders. This is called a virtual file system or a filesystem hierarchy or a directory tree.

There are a number of special folders:

  • The root directory. It contains all other directories in the tree.
  • User directories. “Unix-like” operating systems (which include Linux and macOS) give each user their own folders inside the Users folder. For example, my username is kevmo, so my home directory is located at /Users/kevmo.
  • If you explore the root directory (ls /), you will find it contains directories with names like usr, bin, and etc. These directories are full of software and configuration data your operating system uses. You should feel free to explore them, though I would be careful about moving files around.

Close your Terminal application. Now open a fresh instance of it. Use the following commands to explore your computers directories:

pwd prints the name of your current working directory.

ls lists the visible files in your current working directory.

cd / changes your current working directory to the root directory.

cd ~ changes your current working directory to your home directory. That ~ (“tilde”) is the argument telling us which directory to change into. The tilde is a special shortcut, an alias to your home directory. So when I type cd ~, my command line interpreter internally reads that as cd /Users/kevmo.

open . opens the current working directory in your computer’s graphical navigation program. The period . is an alias for your current working directory.

Let’s put it all together. Enter the following commands to go exploring:

pwd
cd /
ls
cd Users
ls
cd /
cd bin
ls
type ls

Making, Moving, and Removing Files and Directories

You make directories and files using the mkdir and touch commands, respectively.

cd ~
mkdir playground && cd playground
pwd
mkdir directory_1
mkdir directory_2
ls
cd directory_1
touch yourself.txt
ls
cd ..  # The .. is a referent to your current directory's parent directory
pwd

Now let’s move the yourself.txt file from directory_1 into directory_2 using the mv command:

mv directory_1/yourself.txt directory_2
ls directory_1
ls directory_2

As you can see, the yourself.txt has changed directories.

OK, let’s clean all of this up and remove these playground files.

rmdir directory_1
rmdir directory_2

That last command should have thrown a “Directory not empty” error, because it still has yourself.txt in it. To remove a folder that has anything in it, you have to use the rm command with its -r and -f options.

rm -rf directory_2
ls

As you can see, we have now removed all of the sub-directories of playground. Let’s delete that, too.

cd ..
rmdir playground

Hidden Files and Directories

You can hide a directory or file by starting it with a period .:

 mkdir .HIDDEN
 mkdir VISIBLE
 ls
 ls -a

Directories and files hidden in this way are called dotfiles. Dotfiles are commonly used for storing user settings/preferences or saving the state of an application. They are often created without your knowledge. For example, if you downloaded Atom in the last chapter, there is a dotfile located at ~/.atom. You can cd ~/.atom and run ls if you want to see what it’s doing.

Let’s remove the directories we just created.

 rmdir .HIDDEN VISIBLE

Dotfiles serve many purposes, so it’s good to be aware of them.

Go Exploring, But Don’t Fall Down The Rabbit Hole

You just took a major step towards true computer nerd status - congratulations! We will be using the command line throughout the rest of book, so there will be lots of opportunities to practice this newfound skill.

Exercises

  1. If you are having trouble figuring out how to open your command line program, here are some tutorials:
    • Mac OS X: http://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line
    • Ubuntu Linux: https://help.ubuntu.com/community/UsingTheTerminal
    • Windows: https://www.lifewire.com/how-to-open-command-prompt-2618089
  2. I strongly recommend that you either buy or borrow a copy of The Linux Command Line: A Complete Introduction by William E. Shotts (hereafter called The Linux Book), as listed in Appendix A: A Hacker’s Bookshelf.

    This book is a thorough and readable introduction to Linux and the bash command line. It will serve as an excellent teacher and reference manual over the next few years, paying for itself many times over. I recommend skimming a few pages a day for the next few months. As always: Don’t get bogged down in any one chapter of the book.

  3. This “Command Line Crash Course”, which is an appendix of a larger book, is worth reading as well: https://learnpythonthehardway.org/book/appendixa.html.

    This used to be a standalone book, but the author recently made it “Appendix A” of his more popular book, Learn Python The Hard Way. Please note that I am only recommending that you read this short appendix right now, not the entire book (though it is pretty great if you’re interested in Python). When I got serious about programming, I made flash cards from the Command Line Crash Course book.

  4. Learn to use the type command.

       man type
       type ls
       type cd
    
  5. Spend 5-30 minutes reading each of the following articles
    • https://en.wikipedia.org/wiki/Command-line_interface
    • https://en.wikipedia.org/wiki/Bash_(Unix_shell)
    • https://en.wikipedia.org/wiki/Operating_system
  6. How many hidden files can you find in your home directory?

       cd ~
       ls -lah
    

    Run ls -a, ls -h, and ls -l separately, and examine how they provide different output.

  7. Bash comes with a built-in manual. Go skim the man pages for ls, find, and ln:

       man ls
       man find
       man ln
    

    Type q at any point to exit the man pages. Hit the spacebar to keep scrolling.

  8. Who are you?

     whoami
    
  9. If you want to customize your command prompt, edit your PS1 environmental variable:

    echo $PS1
    export PS1='\w \$ '
    

    This will change your command prompt to show you your current working directory, and then a dollar sign. The $ dollar sign is a popular termination character for regular users’ command prompts (as opposed to the superuser or root user, which often terminates in #… but that’s a topic for another day). The Linux Book mentioned in exercise #2 has an entire chapter on customizing the command line prompt.

  10. Open up a fresh instance of Terminal and run the following commands:

      cd ~
      pwd
      cd ..
      pwd
      cd ..
      pwd
    

    .. is an alias for the parent directory of your current working directory.

    Since a file path denoted by .. is determined in relation to whatever your current working directory is, it’s a relative path. Where the cd .. commands takes you depends entirely on whatever your current working directory is.

    In contrast, an absolute path, which will always take you to the same place. cd /usr/local/bin is always going to take you to /usr/local/bin.

  11. If you want to play with some more advanced commands (all safe to try running on your computer), check out Netflix’s Tech Blog article, “Linux Performance Analysis in 60,000 Milliseconds”. http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html