A beginner’s guide to Linux command-line

Joseph Gefroh
9 min readAug 14, 2017

Systems administration and ops are one of my biggest areas of technical weakness.

I used GUI applications almost exclusively during the entirety of my education and first years working professionally. It didn’t help that the IDEs I used for Java, Eclipse and Netbeans, were absolutely amazing tools: I felt I had no reason to switch to what I perceived was a more time consuming way of doing things. In fact, the only command-line tool I used extensively was git.

When I finally started using Ruby on Rails and other scripting languages (and eventually got a Mac), I was at first flabbergasted by the lack of robust GUI tooling available. It was like I had reverted back to using notepad to code. It forced me to start using the terminal and command-line more often.

After a while I realized the power of the command-line. These commands were the APIs to the programs I used and I could use them however I wanted! Better yet, by changing commands together, you could do far more things than you could using a UI.

The journey has been tough. It’s hard to learn all this stuff without a guide. There’s just so much out there, and day-to-day you’ll only end up using a handful of commands.

I’ve written this document to hopefully help others focus their initial search on some common things they’ll end up using on a regular basis. A lot of it has been simplified and it glosses over some important concepts that will come in handy later. However, this should be useful for beginners.

First, some important concepts…

You’ll see these terms referenced throughout the post.

Current Directory

This is the current directory / folder, and is the default context your command will be run in. You can always find your Current Directory by typing pwd.

Arguments

Many of these commands take multiple arguments. In a lot of cases, the required argument is the file the command is being run on. When you see a command like more file.txt, the file.txt is the argument and the file the command is being run on. Every program has different arguments — check the documentation!

Flags

Flags are a way to set options and pass in arguments to the commands you run. Commands you run will change their behavior based on what flags are set. You should read the documentation of each command to know what flags are available.

For example, running ls with the -l flag (ls -l ) will include more information in the result and change the format of what is returned.

Relative Path

Relative Paths are the names / paths to folders and files relative to your current directory. A relative path of myfolder/myimage.png, will mean the myfolder/myimage.png in the current directory, not anywhere else. If I was in another folder, that relative path would not point to the same myfolder/myimage.png.

Absolute Path

Absolute Paths are the names / paths to folders and files that your current directory. An absolute path is valid regardless of your current directory. /myfolder/myimage.png, will mean the same/myfolder/myimage.png regardless of what your current directory is. Note the / in front of the name.

Navigation 101

The first thing to learn is how to move around the directory structure and various folders.

Show your current directory with: pwd

pwd will Print your current Working Directory. It’s great to figure out where you are.

pwd prints the current directory you are in

List files in a directory with: ls

ls lists files and folders in the current directory.

There’s a few flags you can use to make the listing easier to read or more informative:

  • ls -l list all the files in a single column, along with their permissions, sizes and timestamps.
  • ls -a lists all files, including hidden files.

You can combine flags:

  • ls -la for example, will list all files in a single column, including hidden files.

You can also pass in the name of a folder to view the contents of that folder:

  • ls myfolder will list the contents of the folder myfolder in your current directory.
ls will show you the contents of directories

Change directory with: cd

cd changes the directory. By default, the path is relative to the current directory. That means if I type cd myfolder, it will only work if the current directory I am in has a folder called myfolder. You can specify an absolute path by entering / in front of the folder path. cd /myfolder will work only if the root level / has a folder called myfolder.

cd changes the directory to the directory you specify

Manipulation 101

Now that you know how to move around the directory structure, you’ll probably want to start making changes to it.

Create a directory with: mkdir

mkdir will make a directory with the name you pass it. To create a new directory called myfolder in the current directory, you would type in mkdir myfolder.

mkdir will create a folder with the specified name in the current directory

Create a file with: touch

touch will create an empty file with the name you provide.

touch created a file called mynewfile

Move a file with:mv

mv will move a file from the source to the destination. You can actually use mv to rename a file.

mv file_to_move folder_name_herewill move the file into the folder. However, if the second argument turns out to be a file, it will overwrite the file.

mv can be used to move a file or directory. It can also be used to rename files and folders.

Copy a file with: cp

cp will copy a file. It takes two arguments — the name of the file you want to copy, and the new path to the copy (including the file name).

You can use the -r flag to copy a directory.

cp will copy files and directories for you.

Delete a file with: rm

rm will remove a file from your system. If you want to delete a directory, you would use the -r flag (recursive).

Note that this is a rather dangerous command, so be careful with it — there are many horror stories of accidentally deleting important things with a single command (often in conjunction with -r and * wildcards).

rm deletes files and directories

Editing 101

Now that you know how to move around, you’ll probably want to learn how to read, edit, and change files.

Read file contents with: more and tail

more and tail are two commands you can use to read file contents.

more filename.txt will open a reader where you can navigate through the file. When in more, you’ll be able to use some additional commands:

  • You can use space to scroll down a full screen.
  • The up and down arrow keys will scroll up and down a line.
  • Pressing g will enter the goto mode. You can then enter a line number to go directly to that line.
  • Pressing / will enter search mode — type in text to find and it’ll scroll to wherever that text is found. Pressing n will go to the next instance of that found text.

tail filename.txt will print the last 10 lines of a text file. You can use the -f flag (eg. tail -f filename.txt) to follow the file in realtime. Whenever the file changes, it will print the last lines. This is useful if you are watching a log file for an application, for example.

tail displays the last 10 lines of a file.

Search through contents for a specific word with: grep

grep lets you search through text, returning the full line for any matches.

grep "text to find" filename.txt will search for text to find in filename.txt.

grep will return the contents of the lines with any matches

Change the contents of a file with: vi or vim

vi / vim is a ridiculously complicated text-editor that’s often your only option if you are manipulating files on a server. I’ve only learned enough of the basic commands to make small changes, but that should serve you well enough for now.

vi filename will open the file you want to edit.

Note that the editor opens in a mode (normal mode) that doesn’t let you type anything. You’ll need to press the i key to go into insert mode.

After you are done making your changes changes, you’ll want to go back into the normal mode by pressing escape.

  • To make changes to a file, enter insert mode with i and type away.
  • To save your changes, you enter normal mode with escape and press :w to write your changes.
  • To quit, you enter normal mode with escape and type in :q to quit. If you made changes, you’ll need to type :q! to force it to quit.

That’s about all I know about this editor.

Different modes for different folks.

Practicals 101

Now that you know the basics, you’ll want to have this additional information so you can start actually making use of all of this in a real environment.

Get help on a command or find out more about it with: man

man is a command that displays a help manual for the command you pass in as an argument. Not all commands have manuals, and sometimes the manuals are confusing.

For example, man ls will show the manual for the ls command.

If there isn’t a manual for it, you can also try passing in a -help or --help flag. Some programs have a built-in help that will tell you how to use it.

Referencing the Current Directory with: .

. is a way to explicitly reference the current directory. You’ll often see commands like mv ./file.txt ./potato.txt. The first . in ./ is the current directory.

Specify a wildcard with: *

* often represents a wildcard in commands. This means that the command will be run on any file that matches. You can use it to perform actions in bulk, such as running a mv of all files of a specific type:

For example, mv *.png images will move all .png files into the images directory.

It’s very easy to accidentally perform commands on files you don’t want using wildcards, so be careful!

Redirect output to another program with: |

|, known as pipes, are a way to unlock the power of Linux by chaining commands together. It routes the output of the command on the left of the | and passes it to the command on the right of the |.

For example, suppose I want to find all of the running instances of a program called zsh. I could use ps to find the list of all of my processes. However, this list could be massive, and scrolling through it all would be a pain. I could combine it with grep to search through the results of ps and display only what I am looking for: ps | grep zsh.

ps returns a massive list — combining it with grep lets me find exactly what I’m looking for.

Save the results of a command to a file with: > and >>

> and >> will let you run a command and save the results to a file. > will overwrite the file with the results of the command, while >> will append or add the results to the end of the file.

A command like ps > output-of-ps.txt will save the results of ps to the file output-of-ps.txt, overwriting anything that was there previously.

This is a useful method of saving things to a file.

Handy shortcuts

CTRL+R / bck-i-search

CTRL+R / bck-i-search is a miracle tool — it lets you search previously typed commands.

To use it, you press CTRL+R in the terminal and start typing a portion of a command you previously ran. It’ll autocomplete the first match and you can then press enter to execute it. You can cycle through matches by pressing CTRL+R again. This is great if you have long commands you need to type.

history

history shows you a history of previously typed commands. If you’re ever on a server or computer and have no idea what happened, history will save you. You can run any of the commands by typing in an exclamation point followed the number of the command, eg. !197.

Forgot something? Use history!

!!

!! will repeat the previously command.

bang bang

--

--

Joseph Gefroh

VP of Product and Engineering @ HealthSherpa. Opinions my own. Moved to Substack. https://jgefroh.substack.com/