A beginner’s guide to Linux command-line

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 .

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 .

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 , the 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 with the flag ( ) 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 , will mean the in the current directory, not anywhere else. If I was in another folder, that relative path would not point to the same .

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. , will mean the same 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

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

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:

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

You can combine flags:

  • 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:

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

Change directory with: cd

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

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

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 will create a folder with the specified name in the current directory

Create a file with: touch

will create an empty file with the name you provide.

touch created a file called mynewfile

Move a file with:mv

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

will 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

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 flag to copy a directory.

cp will copy files and directories for you.

Delete a file with: rm

will remove a file from your system. If you want to delete a directory, you would use the 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 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

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

will open a reader where you can navigate through the file. When in , 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 will enter the 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 will go to the next instance of that found text.

will print the last 10 lines of a text file. You can use the flag (eg. ) 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

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

will search for in .

grep will return the contents of the lines with any matches

Change the contents of a file with: vi or 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.

will open the file you want to edit.

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

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

  • To make changes to a file, enter with and type away.
  • To save your changes, you enter with and press to your changes.
  • To quit, you enter with and type in to quit. If you made changes, you’ll need to type 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

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, will show the manual for the command.

If there isn’t a ual for it, you can also try passing in a or 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 . 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 of all files of a specific type:

For example, will move all files into the 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 . I could use 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 to search through the results of and display only what I am looking for: .

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 will save the results of to the file , 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 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 to execute it. You can cycle through matches by pressing again. This is great if you have long commands you need to type.

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. .

Forgot something? Use history!

!!

will repeat the previously command.

bang bang

Director of Engineering @ HealthSherpa. Opinions my own. Add me on LinkedIn! https://www.linkedin.com/in/jgefroh/