Sunday, February 06, 2005

Basic Linux Commands

To get started it is a good idea to be sure where you are.

Type: pwd and press enter.
This program is "print working directory" which will print the full path to your current location in the shell.
Ex. /home/your account name
This is most likely the home directory of the user that you logged in as. Most of the things that you do will be here. Each user has a home directory which they own and have control over. Organize your files and directories here.

To see what is in your home directory type ls. This is the ls (list) program which lists the contents of the current directory.
You will probably see something like this:
Mail/ documents/ desktop/ etc.
The ls program accepts arguments which can give you more control over the display. By adding "arguments" you can specify certain behaviors. You can also call ls on a directory other then the one where you are currently located.

Some handy arguments are:

ls -a lists all files. Hidden files were previously unseen.
ls -l lists more information about all of the files.
ls -R lists files recursively which includes all subdirectories.
ls -X lists files alphabetically by their extension.
Type ls --help to find out more possibilities.

After you have messed around with your current directory for a while you probably want to venture off to somewhere else. This is easy, just use the cd (change directory) program. To move to a directory within your current directory simply type cd directory_name and you will move into it. To move back simply type cd ../ and you are back where you started. You can move directly to another directory by typing cd path_to_directory and you will go there.

Type: cd /usr/local/lib and you will move to that directory.
Type: cd ~ and you will move back into your home directory.
The tilde (~) character is your shortcut home.
Type cd --help and you will find that you have a lot of power with this simple program.

To look at the contents of a file you have several options. Two "quick and dirty" options are more and less. Type the program followed by the name of the file that you want to view. More lets you view the file from beginning to end. Less, allows you to scroll the file up and down. Both programs take q to quit.

The shell you are using is remembering the commands that you have been making. Use the up and down arrows to scroll through the commands that you have made. This is a real time saver. If you are trying to remember a command but have forgotten what it is called you can simply type the first few letters and press tab. You will get a listing of the available commands which start with those letters.

Wild cards are another great feature. "*" matches anything. If you are searching for a file which starts with the letter "t" you can type ls t* to get a listing of all files in the directory beginning with "t". Or, perhaps you want to list all .jpg files. Type ls *.jpg.

You can print the results to a file by typing ls *.jpg > mygraphics.txt. You can then append your .gif files to the list by typing ls *.gif >> mygraphics.txt

Note that > creates a file if it is not present, if the file is present it will over write it. >> appends an existing file or creates a new one if it does not exist.

Remember that you can type the full path for the file if you want to place it somewhere else.

Here are some other simple programs: (Take your time, learn one at a time and you will be proficient in no time.) If you find that a program creates a process that you want to stop press Ctrl-c to stop it.

touch filename - creates a new empty file with that filename.
rm filename - removes (deletes) filename.
mkdir dirname - creates a directory
rmdir dirname - removes a directory (must be empty or use "rm -rf dirname").
mv source destination - moves a file from source to destination
cp source destination - copys a file from source to destination
date - shows the current date and time.
Here are a few more complex programs which are extremely useful:

grep [options] pattern filename - this searches the file for the pattern and returns the results.
Ex grep -in date mydates.txt looks for "date" case insensitive and returns the line numbers and text of any matches.
find [path...] expression searches for files under the path which match the expression.
diff [options] file1 file2 reports the difference between two files.
ispell filename checks the spelling of the contents of a file and suggests replacements.
There are many programs included in most Linux distributions which can provide you with almost anything that you would ever need. You can scroll through the available commands by typing each letter and pressing tab. You will find hundreds of interesting commands. If you find something that seems to be interesting type command --help to find more information.

A few of special interest are:

tar a package and compression program
bzip2 another compression program
pico a simple text editor
vi a full blown text editor
pine an email client
free displays available system memory and usage
ping a tool for checking if a system is up and responding
netstat a tool for analyzing your network
telnet a program used for connecting to a remote host
And many others.



A great feature of Linux systems is that they contain their own manuals. Each command that you type into a shell is most likely a unique program. You do not have to wade through pages of a manual to find a specific feature, you can just ask each program directly. Although there is some variation, the following commands will likely help you find exactly what you are looking for.

To find help type: program_name --help
For example: ls --help will display the help file for the ls program.
To find more in depth information type: man program_name
For example: man ls will display the manual pages for the ls program.
To exit the man pages, type q (quit)
If TextInfo is installed you can type: info program_name
For example: info ls will give you comprehensive information.
Again, type q to quit TextInfo.

No comments: