This article is an introduction to some essential and basic Linux commands. A basic understanding of the Linux command line environment (also commonly known as the "shell" or the "terminal") is important if you ever need to set up a special configuration, install an application manually, or you just want to have a better understanding of how your web site works.
When you log in to your account using SSH, the first thing you see is the Linux command line prompt:
usern[email protected] [~]#
The tilde symbol ~ indicates that you are in your home directory, which is usually /home/username. But where do you go from here? The following sections show you how to accomplish some basic tasks from the Linux command line environment.
The first thing you probably want to do is see what's in the current directory. To do this, type the following command and then press ENTER:
The ls command provides a listing of a directory's contents. Its function is similar to the dir command in DOS. To view more detailed information about a directory's contents, type the following command:
The -l option generates a listing that shows permissions, owner and group membership, size, modification date, and the name for everything in the directory.
To determine which directory you are currently working in, type the following command:
The pwd command shows the current directory (pwd stands for “print working directory”). When you first log in to your account, the working directory is /home/username. This directory is your home directory, and is also represented by the tilde sign ~.
To change to another directory, use the cd command. For example, to change to the public_html directory, type the following command:
To change back to your home directory, type the following command:
Just as in DOS, the parent directory is represented as two periods (..). So to change to the parent directory (in other words, to go back up one level in the directory tree), type the following command:
Also as in DOS, the current directory is represented as one period (.).
Now that you know how to move through directories and list their contents, you may be wondering how to find specific files. To do this, use the find command. The find command looks for a file or set of files in a directory and all subdirectories beneath it. For example, to list all files that end with a .html extension in the current directory and subdirectories, type the following command:
find . -name '*.html'
In this command, we use one period (.) to specify the current directory as the search location. The -name option specifies that we are searching for files by their filename (instead of their type or size, for example). The '*.html' option uses a wildcard to indicate that we are interested in any files that end with a .html extension.
The find command has a large number of options available. You can view information about how to use the find command (or any of the other commands described in this article) by using the man command. The man command shows manual pages for a particular command. For example, to view the manual page for the find command, type the following command:
You can use the Page Up and Page Down keys to scroll through the manual page. To exit, type q.
You can use the whereis command to find the path to a particular program. For example, to find the path to the grep program, type the following command:
whereis -b grep
This command shows paths to the executable (binary) programs. To view the paths to an executable program and its related help page files, do not use the -b option. For example, type the following command:
To create a directory, use the mkdir command. For example, to create a directory named backup, type the following command:
To remove a directory, use the rmdir command. For example, to remove a directory named backup, type the following command:
Working with files is straightforward, and if you have done similar tasks in DOS, you should have no problem learning how to do so in Linux:
To copy a file, use the cp command. When you copy a file, you specify the source and destination locations. For example, to copy the index.html file in the current directory into a directory named backup, type the following command:
cp index.html backup
To move a file, use the mv command. As with the cp command, you specify the source and destination locations. For example, to move the index.html file in the current directory into a directory named backup, type the following command:
mv index.html backup
To delete a file, use the rm command. For example, to delete a file named old.html, type the following command:
To determine how much space a directory occupies, use the du command (du stands for “disk usage”). By default, the du command runs through the current directory and all subdirectories, adding up the amount of space that files occupy, and providing a total at the end of the listing. By default, the du command outputs disk usage in bytes. To make the output totals easier to read, type the following command:
For example, if you run this command from your home directory, you can quickly see how much total disk space your account occupies, as well as which directories take up the most space.
To determine how much free space a disk has, use the df command (df stands for “disk free”). To make the output totals easier to read, type the following command:
This article is only an introduction to basic Linux commands and its command line environment. There are many, many more Linux commands and features, and numerous tutorials on the web. For example, the linuxcommand.org website provides an in-depth tutorial about how to use the command line, as well as a downloadable book.