Getting to Know the Command-Line
This guide is intended for individuals who are new to the command line and wish to learn the fundamentals.
What is the Command-Line?
The command line is your computer's ultimate power source. You can do astounding feats of speed and wizardry using the command line, taming your computer and getting it to do exactly what you want. Unfortunately, complexity comes as a price for this power because nobody ever said controlling your computer would be simple.
At its core, the command-line is just a place where you may type commands to the computer. Your dutiful servant, the computer, will try to carry out any instructions it can understand. Unfortunately, the machine does not speak any human languages, including English (although it has recognizable elements). We must first begin studying the language of the computer in order to issue commands to it.
NOTE: The command line contains risks, just like any other form of power. You have the capacity to tell the computer to perform any action it is capable of performing. The computer will happily erase all of your data if you order it to do so. Never run a command to test its functionality. Before you run any command, especially one that modifies or deletes files, be sure you understand what it is designed to accomplish.
Using the Command-Line
Grammar is boring, so let's get this over with. There are three components to any command: the utility, the flags, and the inputs. Utility is always put first. Depending on the command you choose, the other two portions may not require you to provide any flags or arguments at all. The following is an example of a command you might enter into a command line:
ls -l ~/Desktop
Let's break this command down into parts:
ls is a utility. Due to the fact that they convey the broad contours of what you want, utilities are occasionally referred to be commands on their own. The majority of the time, a utility can be executed without any flags or parameters. Typically, a command has only one utility.
-l is a flag that modifies the utility's behavior. The program will typically function flawlessly with the default, but occasionally you wish to slightly alter how it operates. Flags are similar to options or preferences in this regard. Flags often appear between the utility and the arguments and invariably begin with one or two dashes (-).
~/Desktop is an argument to the utility. When there is no obvious default configuration and the utility needs to know exactly what you want for a specific action, arguments are used. You can consider it more as a discussion than a disagreement: You use an argument to respond to the utility's "I don't know how I should do this!" statement by saying, "Here, this is how you should do it." Arguments typically follow the utility and the flags at the end of the command (if any flags are used). The amount of arguments used typically varies depending on the utility: some don't require any, some only need one, some need several, and some are flexible in how many arguments they can accept.
This command uses the ls utility, which is used to list the contents of directories. We use the -l flag to indicate to the utility that we want more information than it usually provides, and so it should show us the directory contents in a long format (-l is short for "long"). Last, the utility wants to know, "But which directory should I list the contents of?" Using the argument, we reply, "Show me the contents of my Desktop."
In all cases, to submit a command to the computer, press enter. Now, let's start learning some useful commands!
Basic Utilities
Here is a list of basic utilities that you will use on a regular basis. Anything in capital letters that starts with a dollar sign, like $THIS, is an argument to the utility. You should replace $THIS with the actual argument you want to give the computer.
man $UTIL
manual. Get information for how to use any utility. Replace $UTIL with any utility, like ls, cd, or even man! Press the up and down arrows to scroll through the documentation. Press Q to quit and go back to the command line.
ls $DIR
list. Lists the contents of the directory $DIR. If no directory is specified, lists the contents of the current working directory. Use the -l flag to get more information.
cd $DIR
change directory. Changes the current working directory to the directory $DIR. In effect, moves you around the computer.
pwd
print working directory. If you ever get lost in the computer, run this command to get a trail of breadcrumbs all the way down from the top level of the computer to see where you are.
less $FILE
Displays the contents of a file. Press the up and down arrows to scroll though the file. Press Q to quit and go back to the command line.
cp $FILE $LOCATION
copy. Copies the $FILE to the $LOCATION.
mv $FILE $LOCATION
move. Moves the $FILE to the $LOCATION.
rm $FILE
remove. Deletes a file permanently: there is no way to get it back. Be careful when using this command!
sudo $CMD
super user do. When you use this utility, you use an entire command as a single argument: for example,
sudo ls -l ~/Desktop
sudo asks for your user account password. As a security measure, the screen does not display anything as you type, not even asterisks (*). If the password is typed in correctly, sudo executes the $CMD with elevated permissions. Be careful when using this command!
A note about using sudo: The computer has a few built-in safety restraints to prevent normal users from doing bad things, like deleting critical files. The super user has no such restraints. Note that the super user is not necessarily bad: you must use sudo to install programs and do anything else that affects how your computer runs.
Moving Around the Computer
Let's start by using ls to look around your computer. Try typing ls into the command line and pressing enter. The computer will reply with a list of names. These names are the names of files and folders in the directory you are currently in. Whenever you open up a new command line, you start in your home directory, which is the directory that generally contains all of your files.
Well, that's nice. But what if we want to go someplace else? That's what cd is for. cd requires an argument: if you tell the computer you want to go somewhere, you also have to tell it where you are going. Try entering this command:
cd Documents
Remember, to press enter once you have finished typing. The computer will not reply, but you are now sitting in your Documents directory. You can test this by running ls again: the list of names will be different.
So where do we go from here? How do we know which of these names are folders (that we can go into) and which are files (that we can't)? For that, we need more information from the ls command. Let's give it the -F flag to tell us about files and folders. Try entering this command:
ls -F
You will notice that this time, some of the names that the computer returns to you will have a slash after them. These names are folders: the rest are files. You can always cd into a folder by running cd with the folder name as an argument, as long as you can see that folder with ls -F.
When you're done looking in folders, it's time to go back up. But how? Luckily, every folder contains a hidden link back up. To see these hidden links, we will use the -a flag for ls to see all. There are at least two hidden links in every folder. The . (one period) link takes you back to the same folder you are currently in — it doesn't take you anywhere. The .. (two periods) link takes you back up to the parent folder. In fact, you can give the ls command multiple flags, like so:
ls -aF
If you run this command, you will notice that the . and .. hidden links have slashes next to them, which means you can use them with cd! To go back up a folder, you can always run:
cd ..
Remember that if you ever get lost in the computer, you can run pwd to see where you are.
Neat Tricks
There would always be techniques to do more with less work. Here are some of those techniques:
Tab Autocompletion
Whenever you need to type out a location in an argument (for example, in the cd command), you don't have to type out the whole thing: the first few letters will do. Once you've typed three or four letters, press the tab key, and the command line will fill in the rest for you! For example, if you are in your home directory, and you type cd Desk and then press the tab key, the command line will automatically complete the command to read cd Desktop! You can also use this if you find yourself mistyping folder names: tab autocompletion will always fill it in correctly.
Shortcuts
The command line has a few shortcuts built in. For example, to see your previously typed command, just press the up button. You can do this to submit the same command multiple times, or to edit a command that you didn't type in quite right. Another shortcut: you can use ~ (tilde) to refer to your home directory: cd ~ will take you back there.
Warnings
Remember, when you use the command line, the computer will cheerfully do anything it can for you. If you ask it to do something bad, it will try to do so. Some people take advantage of this fact by telling novice command line users to run commands that do nasty things to your computer. Here are a few to watch out for. Never run any of these commands! They can and will destroy your computer!
sudo rm -rf /
The command to remove a file is rm. You can also use rm to remove multiple files at a time. This command tells the computer to start at the top of the file structure, and delete every single file on the computer without stopping. After this command has run, your computer be empty. If you turn it off, it will not be able to turn back on until you reinstall an operating system on it.
:(){ :|:& };:
This interesting-looking piece of code is called a fork bomb. Like a virus, it will continually multiply and subdivide itself, asking for more and more resources from the computer, until the entire computer is trying to process this code. As a result, the computer has no resources left for any other programs or processes, and will freeze or crash. Fortunately, rebooting your computer should cure it.
With this, we've reached the end of this article. Interesting, right? You would like to see more wouldn't you? Then subscribe to my newsletter so you don't miss my upcoming articles. Gracias.