The Unix Command
Now we are ready to explore our environment by issuing commands to the shell. Unix has thousands of commands, which you can string together to make more complex commands. You can also create new commands in Unix. All this flexibility makes Unix an extremely powerful operating system.
The reason you have all this power is that commands are treated like any other executable file. When you type a command such as ls, you run the ls executable file, which is located in /bin/ls. When you create a new command, you simply add it to the /bin directory, then use it like any other Unix command. (If that doesn't make sense, you will understand by the end of the hour.)
Unix commands come with switches and options, which can extend or slightly change the action that the command performs. Switches are composed of a dash followed by a letter or series of letters that alter or enhance the command's output. Options consist of two dashes, followed by a word, that perform the same way as switches. In many cases, there is a switch and an option to perform the same task. A switch looks like this:
$ ls -a
An option looks like this:
$ ls --all
Some Basic Unix Commands
After you have logged in for the first time (using your user account, not root!), you should get to know your surroundings. Directories in Linux are based on a tree structure, just like in DOS and Windows. The very base of the directory tree is called the root directory. Below the root directory are more directories, called subdirectories, and below the subdirectories are the files.
Directories and subdirectories are the same thing as folders in Windows.
Getting Your Bearings with the pwd Command
If you lose track of the directory you're in, you can type the pwd command to show you the full pathname of your current location in the filesystem:
$ pwd /home/judith
pwd stands for present working directory. pwd prints the full pathname of your current directory, starting from the root directory and ending at your current directory.
Notice that the slashes that separate directories in Unix are forward slashes (/), not backward slashes (\). Backward slashes also have a meaning in Unix, which we will discuss later.
The root directory is the very lowest level in the directory tree. You might remember the root directory from Hour 2, when you had to assign a mount point to /. Every other directory in your filesystem is a subdirectory of /.
If you enter pwd immediately after logging in, you should see something like /home/ username print to the screen (where username is your user name). When you first log in to Linux, you enter the filesystem at your home directory. Your home directory is where you keep all your work, as well as your personal configuration and other system files.
Listing Directory Contents with the ls Command
So now you know where you are. The next step is to find out what's in your home directory. To list the contents of any directory in Linux, type the following command:
$ls
If you're a new user, there probably won't be anything in your directory, unless your system administrator added files. There actually are files in your directory; you just need to add a switch to the ls command to see them:
$ls -a . .. .bash_logout .bash_profile .bashrc .emacs .screenrc
The -a switch to the ls command means "all." That means to show all the files, including the hidden files that are displayed above. Hidden files are usually configuration files that are used by programs, but anyone can make a hidden file. Notice that no information is given about the files. The ls -a command doesn't tell you if an item is a file or directory, who owns it, who has permission to use it, when it was created, or anything. To get more detailed information about the contents of a directory, you need yet another switch:
$ ls -la total 28 drwx------ 2 judith users 4096 Jul 18 14:29 . drwxr-xr-x 10 root root 4096 Jul 18 14:29 .. -rw-r--r-- 1 judith users 24 Jul 18 14:29 .bash_logout -rw-r--r-- 1 judith users 230 Jul 18 14:29 .bash_profile -rw-r--r-- 1 judith users 124 Jul 18 14:29 .bashrc -rw-rw-r-- 1 judith users 688 Jul 18 14:29 .emacs -rw-r--r-- 1 judith users 3394 Jul 18 14:29 .screenrc
First, ls -la tells you the total size of your directory in blocks (1024 bytes) on the hard disk, which is 28 in our example. Each file and directory is listed with a lot of information.
Notice the first two entries in the ls -la output after the total size are simply a dot and a double-dot. If you are familiar with DOS, you have probably seen these before. The . stands for the current directory. The .. stands for the next-highest directory closer to root. Notice that both the . and the .. directories have a d at the beginning of the first string in the output. The d stands for directory. On some systems, that might be your only way of determining whether an item in a directory listing is a file or a subdirectory.
In Unix, commands are space and case-sensitive. If you forget the space between the command and the argument, or if you capitalize a file that should be lowercase, the command will not work the way you intended. For example, if you enter cd.., you will get an error message.
If you have a color monitor, directories appear in blue in the output of an ls command on a default Red Hat 7.0 system. This behavior is arbitrary, however, and is easily changed.
The letters and dashes that come after the d list the permissions for the directory (and for the files, as well). Permissions specify who has access to the file, as well as what kind of access each kind of user has. The name judith means that the user judith owns the file or directory (notice that root owns the .. directory, which is /home). The date and time, July 18, 14:29, is the date and time that the file was last modified. Finally, the name of the file appears at the far right.
Since you haven't created any files yet, your home directory is kind of boring. Let's use another command to explore the system a bit further.
Changing Directories with the cd Command
The command to change directories is cd. At the shell prompt, type cd followed by the pathname of the directory where you want to go. For example, if you type
$ cd /
you will go to the root directory. If we type ls in this new directory, we see a new set of directories:
$ cd / bin dev home lost+found opt root tmp var boot etc lib mnt proc sbin usr
If we type pwd, we see:
$ pwd /
That way, we know for sure that we are in the root directory. The symbol for the root directory is /. No matter where you are in the directory tree, you can always return to the root directory by typing cd / and then pressing Enter.
Copying Files with the cp Command
Now we are at the second highest level in the directory tree. You will learn about all these directories in Hour 4, but for now let's just explore a bit to help you learn some more commands. Enter the following commands:
$cd /usr/X11R6/lib $ls
The output of the ls command is shown in Figure 3.4. We are going to copy the XF86Config.eg file from this directory to our home directory.
If you are not the administrator of your Linux system, you might not have access to any directories besides your home directory. If you don't have access, ask your system administrator for some sample files to play with.
The command to copy a file in Linux is cp. To copy a file, enter cp filename desired_location. For example:
$ cp XF86Config.eg /home/judith/
This tells the system to copy the file XF86Config.eg from the current directory to the /home/judith directory.
You don't have to be in a directory to copy a file from it. We could have stayed in /home/judith and written the following command instead:
$ cp /usr/X11R6/lib/X11/XF86Config.eg /home/judith/
or simply:
$ cp /usr/X11R6/lib/X11/XF86Config.eg .
As you may recall, the . at the end of the last command stands for "the current directory."
Now let's see what that file we just copied looks like. To return to our home directory from anywhere in the directory tree, we can simply type a tilde (~) at the shell prompt. Type cd ~, then pwd and see what happens:
$ cd ~ $pwd /home/judith
Now type ls and verify that the file is there:
$ls XF86Config.eg
Reading Output with the cat and more Commands and Using Ctrl+C
To view the contents of the file, we can use the cat command. Cat is short for concatenate, because you can also use it to combine the contents of two or more files.
$cat XF86Config.eg
This is a big file! This file is so big that it streams by too fast for you to read it. To slow down the cat command so that you can read one screenful of a long file at a time, you can pipe the cat command to the more command:
$cat XF86Config.eg | more
When you pipe a command in Unix, that means to execute one command, then apply the results of the first command to the second command. In this way, you can build some pretty powerful commands using smaller commands strung, or piped, together. The more command simply means display the output one screenful at a time. To scroll to the next screenful of text, press the spacebar. Continue pressing the spacebar until you reach the end of the file.
If you get tired of reading this very long file before you reach the end, you can issue the abort command, Ctrl+C.
Ctrl+C means to press Ctrl, then press C while holding down Ctrl. In this particular case, the shell isn't case sensitive, so pressing Ctrl+c is the same as pressing Ctrl+C.
Ctrl+C stops the currently running command and displays a shell prompt, ready for the next command.
Deleting Files with the rm Command
After you have finished using the XF86Config.eg file, you should delete it from your home directory, since it doesn't really belong there. To remove a file or directory, use the rm command:
$ rm /home/judith/XF86Config.eg remove file 'XF86Config.eg'? y/n y
Red Hat Linux gives you a prompt to make sure that you really want to delete the file. Other Unices and Linux distributions might simply delete the file with no warning.
There is no Undo button in Unix! Once you have deleted a file, it's gone.
The commands you have learned so far are shown in Table 3.1, along with a few new commands that you can experiment with on your own.
Table 3.1 A (Very) Few Common Unix Commands
Command |
Description |
pwd |
Displays the present working directory |
ls |
Lists the contents of a directory |
cd directory |
Changes to directory |
cp file new_location |
Copies file to a new location or filename |
cat file |
Views file |
more |
Displays the output one screenful at a time |
Ctrl+C |
Aborts the command (only works while the command is still running) |
rm file |
Removes (deletes) file |
mv file new_location |
Moves a file to a new location, also used to rename a file |
mkdir dir_name |
Creates a new directory |
rmdir dir_name |
Removes an empty directory |
rm -r dir_name |
Removes a directory and all its contents |
You can display the last few commands you entered by repeatedly pressing the up and down arrows. This way, you can avoid typing in long commands or filenames over again. You can edit the command on the command line, then press Enter to issue it again.