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.
This article assumes that you know how to access your account using SSH (Secure Shell). If you do not know how to do this, please read this article first.
Table of Contents
The command-line environment
When you log in to your account using SSH, the first thing you see is the Linux command line prompt:
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.
Listing files and directories
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.
Navigating the directory tree
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 in /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:
Finding files
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:
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.
Finding the path to a program
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:
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:
Creating and removing directories
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:
Copying, moving, and deleting files
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:If a directory named backup does not exist, then the cp command copies the index.html file to a file named backup in the current directory.
- 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:If a directory named backup does not exist, then the mv command moves the index.html file to a file named backup in the current directory. Note that this is equivalent to renaming the file.
- To delete a file, use the rm command. For example, to delete a file named old.html, type the following command:
Determining disk usage and free space
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:
More Information
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.
Did you find this article helpful? Then you’ll love our support. Experience the 99RDP difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.