Shell Tutorial

For a more complete Unix and Vi introduction, check out Christopher C. Taylor's excellent tutorial. Comments or suggestions should be sent to rocker@datasync.com.

bash -- The Linux Command Interpreter

When you first enter the shell, you will see the system "message of the day". Announcements pertaining to the status of the shell server are displayed here.

After it, you'll see a prompt that looks like this:

osh1:~$

This is the prompt presented by bash, the standard Linux command interpreter. A command interpreter is simply any program which accepts typed-in commands and runs them. You may be familiar with the MS-DOS command interpreter and its C:\> prompt. bash is the GNU Bourne-Again Shell, maintained by the Free Software Foundation. Other shells such as tcsh, an enhanced version of the Berkeley C Shell, are available on our system. This tutorial will be geared to bash, however.

The string osh1 in the prompt is the name of the computer. Between the colon and dollar sign is an indication of the current working directory. The tilde (~) is shorthand for your home directory. On a multiuser system such as Linux, each user has their own home directory in which their own files and directories are stored.

Commands

To run shell commands, you simply type the name of the command, then any optional flags, then any arguments to be passed to the command, all separated by spaces. Flags usually begin with a dash (-) and serve to modify the behavior of the command. Arguments are the things on which the command is to operate, often the names of files.

Files and Directories

A directory is like a folder, in that it contains files. In Linux, files may not only be documents which contain information of some sort, but also executable programs and even directories. A directory is really nothing but a special type of file which contains other files. In fact, even things like hardware devices and links are treated as files in Linux.

Because directories may contain other directories, the whole system is organized into a hierarchical tree of directories and files. At the root of the tree is the root directory named /. Every file has an absolute path consisting of the sequence of directories one must traverse to get to the file.

For example, the absolute path to the bash program is /bin/bash. This is because the bash program is in a directory called bin which is in the root directory.

The shell command to change the working directory to any other directory on the system is cd. For example, if you enter

cd /bin

you will change your working directory to /bin and your prompt will then show

osh1:/bin$

Not only may you cd to a directory by its absolute path, which always begins with /, but by a relative path as well. For example, cd mydir would change to a directory named mydir located in the current directory. Recalling the shorthand for specifying your home directory, cd ~/mydir would change to the directory mydir located in your home directory.

In every directory are two special directories: . and ... The single dot refers to the current directory. The double dot refers to the parent of the current directory, or the directory in which the current directory resides. So, cd .. would take you one step closer to the root directory from where you are.

One helpful feature of cd is the ability to immediately change back to the directory you were previously in. To do this, enter cd -.

Inside your home directory, you may create other directories under your home directory. This is useful for organizing files and for controlling access to them. The command to create a new directory is mkdir. You simply enter mkdir followed by the name of the directory you want to create. To delete a directory, it must be empty, that is, contain no files except . and ... The command to remove the directory is rmdir followed by the name of the directory.

The command for listing the contents of a directory is ls. ls by itself lists all the files in the current directory. It does not show . or .. or any other files whose names begin with a dot. Files with names beginning with a dot are normally special files used by the shell and other programs that aren't created by you. ls -a shows all files including the dot files. ls -l, or ls -la gives a long listing of files including the file permissions, owner, group, file size in bytes, and date and time last modified.

If you list the name of a file or files on the command line after ls and any options such as -l, it will show only the files you specify. If any of the files you specify are directories, ls will show you the contents of the directory.

Permissions and Ownership of Files

The third and fourth fields in an ls -l listing are the owner and group to which the files belong. Every file in Linux has an owner and a group. Every user has a user id, which is your login ID, and group id, which by default is users. Whenever you create a file, it gets your user and group id.

Every file also has permissions associated with it. This is the first field in the ls -l listing. A typical permission string looks like

-rwxr-xr-x

The first character of this string tells the type of file. Most common are -, for regular files, and d, for directories.

The next three characters are the read, write, and execute permissions for the owner of the file. The letters r, w, and x here indicate that the owner has the permission to read, write (alter), and execute (run, if the file is a program or script) the file. If a dash is present instead of the letter, the owner of the file does not have such permission.

The next three characters are the read, write, and execute permissions for the group. These apply to any other user who is a member of the same group but not the owner.

Finally, the next three permissions are for everyone else on the system, collectively referred to as the world. By default, when you create a file, the group and world write permissions are turned off. This makes sense, as you normally do not want anyone but you to be able to write to your files.

The chmod command may be used by the owner of the file to alter the permissions on it. The syntax for chmod is somewhat arcane. An example is

chmod go-rwx file

This command removes group and world read, write, and execute permissions on file. This is useful if you have a file you don't want anyone else to be able to access at all. In this example, go specifies group and other (world). Other letters that may go here are u for setting user (owner) permissions and a for all (owner, group, and world). All is the default if no letters are given before the - or +. As you might expect, a + in place of the minus would mean you want to add these permissions instead. Finally, the letters rwx specify the type of permission you want to alter -- read, write, or execute.

For directories, the read, write, and execute permissions have a slightly different meaning. Read permission on a directory means the ability to list the contents of it with ls. Write permission on a directory means the ability to write to the directory itself, that is, add or delete files from it. Execute permission on a directory means the ability to cd to the directory and to include it in an absolute path. Removing execute permission on a directory effectively blocks those affected from accessing any files beneath it in the directory structure. Hence, it is useful to store private files in a directory beneath your home directory which you have run chmod go-x on.

The user named root is a special case. This is a special account for use by the system administrator. Access permissions to not apply to root. Likewise, root is the owner of most of the files in the system directories such as /bin, /lib, and /etc. These files and directories are protected from alteration by users other than the owner, that is, the system administrator.

Viewing Files

The cat command is the most basic way to look at the contents of a file.

cat file1

will display the contents of file1 to the terminal. You may give more than one filename and cat will display each file in succession. In fact, cat gets it name from "concatenate", since it is used to concatenate files together.

One thing to be careful of are binary files, such as executable programs. If you run cat on a binary file, you'll get nothing but gibberish and a lot of beeping. Worse yet, the terminal may interpret certain sequences of characters as special escape sequences and go into a mode where nothing displays correctly. If this happens, all you can is is try to type reset to put it back into a reasonable state.

Now, many file you want to look at contain more than one screenful of lines, and cat does not pause after each screenful. For viewing longer files you'll want to use less or more. These two interactive programs are similar in functionality with slightly different command sets. Both are powerful tools for browsing long files.

less and more both take a filename (or names) as an argument, display the first screenful of them, and accept single letter commands telling what to do next. Some of the more useful commands are:

  • SPACE scrolls the file forward one screen
  • ENTER scrolls the file forward one line
  • b scrolls the file backward one screen
  • h displays a command summary (help screen)
  • q exits the program

Editing Files

You have many options for editing files in the shell. Editors available on our system include:

  • vi, the "classic" Unix screen editor
  • emacs, a large, powerful editor from the Free Software Foundation
  • pico, the message composer from the Pine mail system
  • joe, a simple editor based on some popular DOS editors
  • vedit, editor from the Pirates BBS system

While vi and emacs are the editors of choice of most power Linux users, they are also the most difficult to comprehend. We recommend the other three to Linux shell neophytes.

joe is very similar to the WordStar and Borland Turbo editors in DOS. If you are familiar with either of these editors, joe is a natural choice.

pico is a good compromise of ease of use and features. vedit is similar to pico in its commands, but is stripped down feature-wise. It is most suited for quick and dirty editing jobs.

All of these editors accept a filename command line argument specifying the file to edit. All except vedit will work without a filename, editing a "scratch buffer" which must be named before saving.

Keep in mind that while you may run an editor on any file you have permission to read, you will not be able to save your work if you lack write permission on the file, which is generally the case unless you own the file and it's in your home directory.

Copying, Moving, and Deleting Files

The cp command is for copying files. In the simple case of copying one file to another in the current directory, you would enter

cp file1 file2

to copy file1 to file2. Of course, you may also specify full pathnames for the files to be copied. Note: if file2 already exists, its previous contents will be lost! By using cp -i instead of cp you can avoid doing this, as you'll be prompted to confirm the copy whenever it would destroy an existing file.

You may specify the name of a directory as the destination file, in which case a copy of the source file with the same name as it will be placed in the specified directory. If the destination is a directory, you may specify more than one source file and all of them will be copied to the specified directory. For example:

cp file1 file2 file3 destdir

will copy file1, file2, and file3 to directory destdir.

The mv command works almost exactly like cp except that it moves files instead of copying them. This is how you rename a file in Linux -- by moving it to a different filename. In reality, all mv does is a rename of the file in most cases, except when the file is being moved to a different disk partition and hence must be copied and the original copy deleted.

The rm command is used to delete files. Any number of files may be given on the command line. Important: when you delete a file in Linux, it's gone forever. There is no undelete program like in DOS. We do make tape backups of user files every couple of days in case of dire emergency, but if the file was created or modified since the last backup, there's nothing we can do. For this reason we recommend using rm -i instead, as this option causes rm to prompt before deleting each file.

You cannot delete a directory unless you use the -r flag with rm. This instructs rm to descend any directory which it is requested to delete, deleting everything beneath it in the directory structure. rm -rf is the quick and dirty way to delete an entire directory tree. The -f flag tells rm to not prompt under any circumstances even if -i was given. Do not use rm -rf unless you are absolutely sure what you are deleting!

Online Help

Documentation on nearly every Linux command may be found in the online manual. The manual pages give detailed information about the command and how to use it, including any and all command line options. If you know the name of the command you want to read about, simply enter

man command

where command is the name of the command. Try man man, for instance. The manual page is formatted by the man program and piped through the less program, so the commands for navigating the manual page are those used by less.

If you don't know the name of the command but know something about what it does, you can search for it with apropos. This program accepts a word or words and gives a list of the manual pages containing the given word or words in the title.

The man command should not be confused with the help command, which is built into the bash shell. help only gives a summary of the shell's built-in commands, mostly of interest for writing shell scripts. External programs must be looked up using man.

  
Home
Services
Support
Account
Frontpage
Search
About

 
  Information
  Bulletin Board
  Unlimited Hours
 
  ADSL Connection
  ISDN Connection
  56K Connection
  V.90 Connection
 
  Analog Statistics
  Internet Installer
  Form Mail
  The Proxy Server
  E-mail Configuration
  News Readers
  F.A.Q. Section
  IRC Help
  Netscape Help
  Shell Tutorial
  Trumpet Winsock
 
  General Information
  Win NT 4.0 Dial-Up
  Win NT 3.51 Dial-Up
  Windows 95 Dial-Up
  Red-Hat Linux
 
  How-To Create
  CGI Programs
  Password Crypto
  Uploading
  Design Hints
  Page Resources
 
Back to Home...

 
 
  [ Home | Services | Support | Account | FrontPage | Search | About ]

For suggestions or comments regarding the Datasync Web Site send mail to the Webmaster
For information on prices or quotes send mail to the Sales Department

Copyright © 1999-2003
Datasync, a division of I-55 Internet Services, Inc.
All Rights Reserved