Tips & Tricks
Featured Article: The Newbie's Guide to the Command Line

July 2003

So you want to know a bit about the command line? You could ask the resident alpha geek, but you loathe his sighs and eye rolls. You just want to know some basic things. Who can help? Sigh. We can. And we will.

The best way to learn it is by doing it, so launch a terminal window. It's Menu > System Tools > Terminal in Red Hat Linux 8.0 or 9 or by clicking the panel launcher in other versions. We'll learn to mess with files and directories.

You should be looking at a terminal. Now where are you?

[user@localhost user]$ pwd (it's short for "print working directory", type it and hit "Enter")
/home/user (this is the output, you are in "/home/user")

All right, let's make a new directory in there.

[user@localhost user]$ mkdir stuff

Is it there? List it.

[user@localhost user]$ ls

Let's go in and change directory to /home/user/stuff:

[user@localhost user]$ cd stuff
[user@localhost stuff]$ pwd

Make n file in that directory.

[user@localhost stuff]$ touch testfile
[user@localhost stuff]$ ls

You can do a number of things with a file, copy it with "cp file_1 file_2", move it with "mv", and remove it with "rm". In order:

[user@localhost stuff]$ cp testfile copy_file

[user@localhost stuff]$ ls
copy_file testfile

[user@localhost stuff]$ mv copy_of_test_file moved_copy_of_test_file

[user@localhost stuff]$ ls
moved_file testfile

[user@localhost stuff]$ rm testfile

[user@localhost stuff]$ ls

Cool trick: you can type a few letters of a file and hit your TAB key to auto complete the name. You can "tab complete" directory paths as well. Type 'rm mov' and hit TAB and then hit Enter. The file is gone. Now:

[user@localhost stuff]$ cd ..
[user@localhost user]$ rmdir stuff/

The first command moved you up one directory, the second, as you probably guessed or ls'd, removed the 'stuff' directory. You could also have used 'rm -r', the -r flag makes it recursive.
A couple of last tricks. You can do things with the output of some commands. Let's say you have a directory of music files and you want to make a list.

[user@localhost stuff]$ ls > music_list.txt

That will make your list. You can also "pipe" output through another command. Say you want to know how many files are listed:

[user@localhost stuff]$ cat music_list.txt | wc -l

Looks crazy, huh? You'll get used to it. Practice the first few commands and then look at something like and practice some more.