The Ten Commands

by John Raithel

The following describes ten simple Linux (UNIX) commands. By learning them, and a few simple ways to combine them, you'll quickly gain an understanding of how Linux works, and why it is so powerful. Linux commands are like atoms, so although you are only learning ten of them here, you can combine them into many different kinds of "molecules".

The ten commands described on this page are:

In addition, you'll use the character ">" to redirect command output to a file.

But before you can start, two things:

  1. You need to know what a "command" is, and what a "file" is.
  2. You need to know how to "log in" to a Linux system, so that you can use it, and how to "log out" when you are done.

Commands and Files

A "command" is an instruction you give to the Linux system to tell it to do something. For example, the command "ls" is short for "list", and if you tell the system "ls", it lists the files available.

A "file" is where you store information on Linux systems. A file could be, for example, a list of names and telephone numbers of friends. Another file might be a computer program, a photo, or a movie. This text you are reading now is just another file on a Linux system. A file can be just about anything. Files can be grouped together in "directories", which are shown as folders in a graphical user interface (GUI) such as a desktop.

In the following discussion, you will learn how to use commands and files.

Starting and Stopping ("Logging-in" and Logging-out")

If you have an account (also called a login name) on a Linux system, you have probably also been given a password. If you do not have an account on the Linux system you are going to use, you have to ask the person in charge of the computer to make one for you. This person is usually called the system administrator. (If you are in charge and don't know how to do this, you really need to read a good book on Linux and Linux system administration - this introduction is just for first time users without system administrator-type responsibilities.)

Note: Alternatively, you may be able to use the online emulation of a Linux system at http://bellard.org/jslinux/ which should boot up when you access it and soon give you a "#" prompt. You will not need a user name or password to use this online emulation.

To start using the system, log in with your account name (user name). You should see something like this:

	login:

Type your account name and then press the Enter key (called the Return key on some keyboards). You should then see something like this:

	password:

Enter your password and press Enter. If all went well, you should see a Linux prompt. There are almost as many different-looking prompts as there are Linux systems, but they usually end in either a "$", "%", or "#" symbol. Here are some examples of what you might see:


	%

	$

	fred$

	/home/fred_%

and so on. If you simply press the Enter key, the prompt should just repeat itself:

	fred$ <Enter key pressed>
	fred$ <Enter key pressed>
	fred$ <Enter key pressed>
	fred$

Once you have the prompt, you are ready to begin. One more thing though—when you are finished using the system, enter "exit" (followed by the Enter key), and you will be logged-out:

	fred$ exit

	login:

A new login prompt appears for the next time you want to use the system.

The Ten Commands

In this section, you use commands to do various things, including creating, viewing, and deleting files. When you have a Linux system prompt, you can enter a command, for example:

	$ ls

In this example, the Linux system prompt is shown as the "$" symbol. (Your prompt may look different but the examples here will use the "$" symbol.) The command is "ls" which means "list". After you have entered a command and anything else you want the system to deal with, you must press the Enter key, to tell the system to go ahead.

The "ls" Command

Use the "ls" command to list the files available to you. For example:

	$ ls
	Bin	Images	News	Notes	Temp
	broken	netscape	new.xinitrc	notes	phonelist
	$

Or, if you do not have any files yet:

	$ ls
	$ 

the system just returns the prompt after not listing anything.

The "touch" Command

The "touch" command is a simple way to create an empty file. For example:

	$ touch myfile
	$

If you didn't have any files before, but now enter an "ls" command, you will see:

	$ ls
	myfile
	$

Or, if you already had some files, you will now see a new file name in the list, "myfile".

You may not use the "touch" command much at first, but it can be useful, and I use it in this introduction so that you can easily create some files.

The "mkdir" Command

The "mkdir" command creates a special kind of file, called a "directory". On systems that represent these things more visually, a directory is usually depicted as a file folder - a place where you can store other files. For example:

	$ mkdir newdirectory
	$

creates a directory (or "folder") called "newdirectory". You can see that the new directory is there with the "ls" command:

	$ ls
	myfile	newdirectory
	$

The "pwd" Command

Use the "pwd" command to tell you which directory you are currently looking at. ("pwd" stands for "Present Working Directory".) For example:

	$ pwd
	/home/fred
	$

Since I don't know how your system is arranged, or what your name is and so on, your output from the "pwd" command will almost certainly look different. What the output from "pwd" shows you is the name of the directory you are looking at (the directory you are "currently in"), as well as all the directories which are "above" it. In the example output, you would be in the directory named "fred" which is a subdirectory of a directory named "home".

If your output from the "pwd" command looked like this:

	/usr/export/users/elaine

that means you are in the directory "elaine", which is a subdirectory of "users" which is a subdirectory of "export" which is a subdirectory of "usr".

These "subdirectories" are just folders within folders.

The "cd" Command

Use the "cd" command to "change directories". When you used the "ls" command up to now, you were looking at the files in a certain directory—the directory you automatically see when you first log in to the system.

Now that you have created a new directory (called "newdirectory") in your home directory, you can use the "cd" command to begin to use it:

	$ cd newdirectory
	$

Since you just created the directory, there will be nothing in it;

	$ ls
	$

To see that you are in the new directory, use "pwd":

	$ pwd
	/home/fred/newdirectory
	$

To go back to your home directory, just enter "cd" by itself:

	$ cd
	$

Now when you do a "pwd", you will see that you are "back home":

	$ pwd
	/home/fred
	$

The "cp" Command

Use the "cp" command to make copies of files.

First, change to the new directory that doesn't have any files in it yet:

	$ cd newdirectory
	$ pwd
	/home/fred/newdirectory
	$ ls
	$

And create a new file:

	$ touch file1
	$

and take a look to see that it is there:

	$ ls
	file1
	$
Now, make a copy of "file1" called "file2":
	$  cp file1 file2
	$
and take a look:
	$  ls
	file1	file2
	$

The "mv" Command

Use the "mv" command to "move" a file to a different directory or to give it a different name. You can use the "mv" command to rename "file2" to "file3" for example:

	$  ls
	file1	file2
	$  mv file2 file3
	$  ls
	file1	file3
	$

You can also move or copy files between directories. In the following example, fred goes back to his home directory and moves "file3" into the directory called "newdirectory":

	$  cd
	$  pwd
	/home/fred
	$  ls
	myfile	newdirectory
	$  mv myfile newdirectory
	$  ls
	newdirectory
	$  cd newdirectory
	$  ls
	file 1	file3	myfile
	$

The "rm" Command

Use the "rm" command to remove a file completely. For example:

	$  ls
	file1	file3	myfile
	$  rm file1
	$  ls
	file3	myfile
	$

(On some systems you will be asked if you are sure you want to remove the file. This is just a safety feature, because once you remove a file you cannot get it back! Type "yes" if you want to remove the file it is prompting you about.)

A Note on Redirection with the ">" Symbol

Up to this point, you've created a few files and a directory just to learn a few things about how to manipulate them. But the files have been empty, there is nothing in them.

There are many ways to put content into a file, but an easy one is with the use of the ">" (greater than) symbol. When you perfromed the "ls" command, the ouytput (the list of files) appeared on your screen. But instead, you could have redirected that output to a file. For example:

        $  ls
        file3   myfile
        $  ls > my_ls_output.txt 
        $  ls
        file3  my_ls_output.txt  myfile

The file "my_ls_output.txt" now contains the ouput of the "ls" command as you will see in the following discussion of the "cat" command.

The "cat" Command

To see the contents of a file, use the "cat" (short for concatenate) command:

	$  cat my_ls_output.txt
        file3  my_ls_output.txt  myfile
	$

The command shows the results of the "ls" comand you perfromed previously, because you sent the output of the "ls" command to the file "my_ls_output.txt". Even if you remove some files now, that will not change the contents of your "my_ls_output.txt" file:

        $  rm file3 myfile
        $  ls
        my_ls_output.txt  
        $  cat my_ls_output.txt
        file3  my_ls_output.txt  myfile
        $

Note that the "ls" command now just shows the "my_ls_output.txt", because you removed "file3" and "myfile". But your "my_ls_output.txt" file still contains the name of all three files, because you made it by redirecting the "ls" command when all three files were there.

You can also use the "cat" command with the ">" symbol, to create a file that has something useful in it. For example, create a file called "names" with a list of names in it like this:

	$  cat > names
	Fred
	Eileen
	Jose
	^d
	$

(Press the Enter key after typing in each name, and when you have entered all three names, hold down the Control key (usually labeled "Ctrl") while also pressing "D"—that is what "^d" means.)

Now, see which files are in your current directory:

	$  ls
	file3	names
	$

and then look at ( or "cat") the "names" file:

	$  cat names
	Fred
	Eileen
	Jose
	$

The "less" Command

Use the "less" command to look at bigger files - files that are longer than the screen. To demonstrate the "less" command, first create a long file. You can do this simply by concatenating the "names" file several times and then storing those names in a new file. For example, if you enter:

	$  cat names names names

you will see:

	Fred
	Eileen
	Jose
	Fred
	Eileen
	Jose
	Fred
	Eileen
	Jose
	$

This time, put that in a file names "manynames":

	$  cat names names names > manynames
	$

Take a look at what you have:

	$  ls
	file3	manynames	names
	$  cat manynames
	Fred
	Eileen
	Jose
	Fred
	Eileen
	Jose
	Fred
	Eileen
	Jose
	$

Keep doing this until you have a file that is too long for your screen:

	$  cat manynames manynames manynames names names > evenmore
	$  cat evenmore

	<list of names scrolls by>

When you've finally got a file that is too long for your screen, you can use the "less" command to see a screenful at a time:

	$  less evenmore

	<one screenful of the file appears>

To see the next screen press the spacebar, then to go back to the previous screen, press the "b" key. Press the "q" key to go back to a system prompt.

Note: if your system doesn't seem to know about the "less" command, try doing the same but use the "more" command. On very old systems, there is just a "more"command that does not let you go backwards! Tell your system administrator about "less"...

For More Information

Use the "man" command to get more information on a command. For example:

	$  man less

will tell you about the "less" command. Do not be intimidated by the kind of "help" you get with the "man" command—these help pages were really written more as a memory refresher for people who were already familiar with the command and its many uses. For real help, get a good introduction to Linux, and ask questions.

Sometimes a quick synopsis of a command is available by typing "--help" after the command name, for example:

	$  less --help