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.
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
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 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 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
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.
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.
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 -llist all the files in a single column, along with their permissions, sizes and timestamps.
ls -alists all files, including hidden files.
You can combine flags:
ls -lafor 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 myfolderwill list the contents of the folder
myfolderin your current directory.
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
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
Create a file with: touch
touch will create an empty file with the name you provide.
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.
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.
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
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
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.
gwill enter the
gotomode. You can then enter a line number to go directly to that line.
/will enter search mode — type in text to find and it’ll scroll to wherever that text is found. Pressing
nwill 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.
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
Change the contents of a file with: vi or vim
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
After you are done making your changes changes, you’ll want to go back into the
normal mode by pressing
- To make changes to a file, enter
iand type away.
- To save your changes, you enter
- To quit, you enter
escapeand type in
:qto 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.
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.
man ls will show the manual for the
If there isn’t a
manual for it, you can also try passing in a
--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
./ 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:
mv *.png images will move all
.png files into the
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.
Save the results of a command to a file with:
>> 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.
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 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.
!! will repeat the previously command.