Basic Linux commands


The comand line is often cited in the press as one of the biggest drawbacks to Unix operating systems, including Linux. This is rather unfortunate since the command line is one of Linux's most powerful and compelling tools. There are very few tasks that can't be accomplished ù often significantly faster than by using their GUI (graphical user interface) counterparts ù by using the command line.

The command line takes some time to learn, and even longer to master. However, like other aspects of Linux, the return from the time invested in learning is substantial. In this article we will only scratch the surface, but we'll return next month to cover the command line in a little more detail.

The first commands to learn are the basic navigation commands. When you first log on to a Linux box, you start in your home directory. The home directory is a place for you to store all of your personal files. You can move out of this directory by using the cd (change directory) command. Try typing cd /usr to move to the /usr directory. If you would like to list the files in this directory, type the ls command. You can always return to your home directory using the cd command with no directory specified. Try it now: type cd, and then type ls.

Next, let's look at how to copy a file. First we'll create a file called test using the following command: touch 'test' (don't worry about the touch command for now). You can create a new copy of this file by typing:

cp test test2

The cp command copies a file to a new file. In this case, we copied the test file and named the new copy test2. If you want to see the newly created file, list the contents of the directory again using the ls command.

Sometimes you will want to move a file completely instead of making a new copy. In this case, you will need to use the mv command. Try typing mv test2 test3. Now you should see that the file test2 has been replaced by test3 (or in this case renamed to test3). For a file to be properly moved, you will need to move it to another directory.

To create a directory in Linux, type mkdir followed by the directory name. For example, try the following command:

mkdir testdir

Enter ls and you should see that the 'testdir' directory has been created. Now move the test3 file into that directory by typing:

mv test3 newdir/test3

Use the cd and ls commands that you learnt earlier to verify that the file is in the newly created directory.

Caption: The Linux command line is a powerful tool,
but it's not easy to master

The next crucial commands to learn are the rm and rmdir commands, which remove files and directories respectively. However, before you can remove a directory, you must first remove all of the files in the directory. To try this out, return to your home directory by typing cd. Next, remove the test3 file from the testdir directory by typing rm testdir/test3. Notice how Linux did not prompt you asking you if you were sure you wanted to remove the file. If you tell Linux to delete a file, it does it ... no questions asked! Next, remove the testdir by typing rmdir testdir.

Another trick with moving and copying files is that you can copy and move multiple files to a new directory by including only the name of the destination directory. For example, recreate the testdir by typing mkdir testdir. Next, create two more files by typing touch test2 test3. Now we can move those two files to the new directory by typing mv test2 test3 testdir/. The only rule with moving multiple files is that the last argument must be the name of a valid directory. The same trick works with copying files: try typing cp test1 testdir/.

We've moved fairly quickly due to the small amount of space available, so take some time to play with the commands mentioned and try to get more comfortable with the concepts presented. Next month we'll look at some more advanced command line uses.

- Travis Simon


Category:Linux
Issue: July 1999

These Web pages are produced by Australian PC World © 1999 IDG Communications