Knowledge Base

Introduction to Linux commands

This article is an introduction to some essential 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.

The command line environment

When you log in to your account using SSH, the first thing you see is the Linux command line prompt:

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

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:

ls

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:

ls -l

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:

pwd

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:

cd public_html

To change back to your home directory, type the following command:

cd ~

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:

cd ..

Also as in DOS, the current directory is represented as one period (.).

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:

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:

man find

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:

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:

whereis grep
Creating and removing directories

To create a directory, use the mkdir command. For example, to create a directory named backup, type the following command:

mkdir backup

To remove a directory, use the rmdir command. For example, to remove a directory named backup, type the following command:

rmdir backup
The rmdir command only works for directories that are empty. If you try to remove a directory that contains files, you receive an error message.
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:

    cp index.html backup
    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:

    mv index.html backup
    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:

    rm old.html
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:

du -h

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:

df -h
The df command is more suitable for a VPS or dedicated server, because you control the entire disk. It is less useful on a shared hosting account, where you share the disk with other accounts.

More Information

This article is only an introduction to the Linux command line environment. There are many, many more 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.