This tutorial is the first in a series of introductory Linux lessons. This first article will cover navigating around a Linux filesystem along with a brief passage -- with examples -- on using ten of the most essential GNU/Linux commands.
You should have access to a Linux system in order to perform the example commands as we progress through the tutorial. If you don't have a dedicated Linux box, you can use a Live Linux CD-ROM-based distribution such as Knoppix. Knoppix will let you run Linux directly from the CD without modifying anything on your hard drive.
Once you're logged in to a Linux system, open a terminal session. Each of the commands covered here will be typed directly into a command line terminal window. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, have a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)
pwd
The first command we will use is 'pwd' -- which stands for 'print working directory'. The pwd command shows you your current position within the Linux filesystem. The position is known as your 'current working directory'. Type pwd now. The example below shows my command prompt and the pwd command followed by the output from the pwd command:
[rayy@barton0 rayy]$ pwd
/home/rayy
[rayy@barton0 rayy]$
>From the output (/home/rayy) we can tell that I am in my 'home directory' -- the directory where I keep my personal files and the directory where I always start out in a new session.
ls
The ls command lets you list files. For example, here is the (shortened) output of an ls command on my system:
[rayy@barton0 code]$ ls
artdir countdir machine
tsardir sortdir
[rayy@barton0 code]$
Alternatively, you can get a 'long listing' that shows file sizes, timestamp, ownership, and permissions as follows:
[rayy@barton0 code]$ ls -l
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 artdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 countdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 machine
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 sortdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 tsardir
[rayy@barton0 code]$
You can also supply a target directory to the ls command. For example, to view the contents of the /tmp directory, I enter the following:
[rayy@barton0 code]$ ls /tmp
flp kde-rayy mcop-rayy
[rayy@barton0 code]$
For more information on the ls command you can reference the manual page for ls with the following command:
[rayy@barton0 code]$ man ls
cd
This next command, 'cd', lets you change your current working directory. for example, you can change your current working directory to /usr/bin by entering the following command:
[rayy@barton0 rayy]$ cd /usr/bin
[rayy@barton0 bin]$
Note that after I entered the cd command, my command prompt changed to reflect the change in the last node of my current working directory. Your command prompt may not be configured to do that.
Change your current working directory to /usr/bin now and enter the ls command.
[rayy@barton0 code]$ cd /usr/bin
[rayy@barton0 bin]$ ls
addr2line mcheck
addr2name.awk mcomp
addresses mcookie
.
.
.
[rayy@barton0 bin]$
The preceding is a partial listing. There are many, many files in the /usr/bin directory on most Linux systems.
file
If you have a background in Windows or are familiar with DOS, you are used to file extensions that signify the file type. Linux (and Unix) have no such requirement. That is, an executable program can be named anything. Therefore, a handy command is supplied with Linux named 'file'. For example, I have a file named 'sample.c' in my code directory. I can learn a bit about that file by entering the following command:
[rayy@barton0 code]$ file sample.c
sample.c: C++ program text
[rayy@barton0 code]$
Alternatively, I can use the '*' wildcard -- which represents all filenames -- to examine all of my code files at once. The following is a shortened example:
[rayy@barton0 code]$ file *
bbsdir: directory
code.tar: GNU tar archive
genart.c: ASCII C program text
sample.c: C++ program text
xor: ELF 32-bit LSB executable
[rayy@barton0 code]$
The file command can be very useful to avoid minor annoyances -- such as when using one of the following three commands.
cat
The cat command is useful for concatenating multiple files -- or just for dumping a single text file to the screen. Before you use the cat command to dump a file to the screen, use the file command to make sure it's some variety of text file such as ascii text, commands/text, C source code, html/text, etc. The following is a shortened example of using file and cat to identify and dump a text file:
[rayy@barton0 code]$ file xor.c
xor.c: ASCII C program text
[rayy@barton0 code]$ cat xor.c
main()
{
int x;
unsigned char buff[128],
.
.
.
[rayy@barton0 code]$
more
The more command is useful when a text file is larger than a single screen. The following is a shortened example of using more to view a large C program:
[rayy@barton0 code]$ more xor.c
main()
{
int x;
unsigned char buff[128],
.
.
.
printf("n%sn",buff1);
printf("n%sn",buff2);
}
--More--(29%)
Note the '--More--(29%)' at the end of the screen. That means that 29% of the file is above that line, implying that another 71% of the file is below. Press the space bar to page through the file, a screenful at a time. Press the b key to back up. If you finish looking before reaching the end of the file, press the q key to quit.
grep
The grep command, short for 'get regular _expression_ and print', is useful for finding occurances of a particular string in a text file. To find the 'printf' statements in the example C program above, enter the following command:
[rayy@barton0 code]$ grep printf xor.c
printf("n%sn",buff1);
printf("n%sn",buff2);
[rayy@barton0 code]$
The grep command has far more capability than I describe here and, as usual, enter
[rayy@barton0 code]$ man grep
for more information.
cp
The cp command will let you copy files. Unlike the commands used above, this one includes a hazard; if you copy filename1 to filename2 and filename2 already exists, you will destroy the original filename2 file. Use cp with caution!
To make a duplicate copy of my xor.c file I could enter the following command:
[rayy@barton0 code]$ cp xor.c xor.c.bak
[rayy@barton0 code]$ ls xor.c*
xor.c xor.c.bak
[rayy@barton0 code]$
Note that the cp command returned no output -- I had to enter an ls command to see the results of the copy. [By adding the * wildcard to the original filename, I asked for a listing of all files that started with xor.c -- including those with no additional characters in the name.]
rm
The rm command is used for removing files. To remove the duplicate file I created in the cp command example, I would enter the following:
[rayy@barton0 code]$ rm xor.c.bak
[rayy@barton0 code]$ ls xor.c*
xor.c
[rayy@barton0 code]$
Again, note the absense of any feedback from the rm command. I had to enter an ls command to verify that the xor.c.bak file had really been removed.
As with other commands, rm can remove multiple files at once when used with wildcards or with the -r (recursive) option. See the man page for more information on rm.
mkdir/rmdir
Ok, this is really two commands, but they are complementary. Use the mkdir command to make a new directory and use the rmdir command to remove an empty directory. For example:
[rayy@barton0 tmp]$ mkdir testdir
[rayy@barton0 tmp]$ ls
testdir
[rayy@barton0 tmp]$ rmdir testdir
[rayy@barton0 tmp]$ ls
[rayy@barton0 tmp]$
In the preceding series of commands I first created a new directory named 'testdir'. I then used the ls command to verify its presence. Then, I removed 'testdir' and verified that it was gone by using ls again
|
|