|
The Unix Shell - Part One
Introduction and Basic ConceptsIntroduction The word Unix has traditionally brought to mind images of a mystical operating system for high-end workstations and servers that can only be accessed through a cryptic command line interface known as the Unix Shell. Linux with the help of graphical front ends like GNOME has allowed people the power of Unix without requiring them to learn a complex command line system. However, The Unix Shell still has a lot to offer. From the command prompt simple programs or scripts can be quickly constructed to automate repetitive tasks. Constructing larger and more complex programs can also be done with minimal effort. The Unix Shell is a powerful tool that can make life easier for anyone who is willing to invest a bit of time learning the basic syntax. Basic ConceptsRunning programs from the Unix command line is easy. Under normal circumstances it is a matter of typing in the name of the program you wish to run. For example, if you want to start the Pine email client, just type "pine" at a command prompt. Pine will then take control of the screen until you exit. Only programs located within directories that appear in your PATH can be run in this fashion. All of the directories that normally contain executable programs should be in the default PATH. If you want to run a program that is not in a directory that your path searches you must specify the full path + program name on the command line. For example, if you wanted to run the program hello, located in the /tmp directory you would have to type in "/tmp/hello" the command will work from any directory. Moving Around DirectoriesFiles in the Unix world are stored in directories. The directory structure on a Unix system is like a tree, the top of the tree is called the root directory or "/". The root directory can contain both subdirectories and files. Each subdirectory underneath root can also contain more subdirectories and files. When working in the shell your session always has a current directory. This current directory is the directory that you are "in" at any given time. When you execute commands from the shell, they will run with respect to the current directory. The command "pwd" will display the current directory, and the cd command allows you to change the current directory.
File Management and SummaryFile Management Many shell scripts involve copying, moving, or removing files. All of these tasks can easily be handled from a standard shell. Listing FilesThe command used to view a list of files in the current directory is "ls". Typing "ls" at a command prompt will result in a list of all files in the current directory being displayed. Copying FilesCopying files can be done with the cp command. The cp command takes as arguments a list of files to copy (the source) followed by the destination (where the files are to be copied to). Source lists containing more than one file must be followed by a destination directory. When copying a single file, the destination argument can be a directory to copy the file into, or the name of the new file to create. Consider the command "cp hello.txt world.txt goodbye.txt /tmp". This command will copy the three text files specified (assuming they exist and are in the current directory) to the standard Unix temporary directory /tmp. After the command finishes the /tmp directory will contain copies of the three text files. Now consider the command "cp hello.txt none.txt". After the command finishes, the file "none.txt" in the current directory will exist and will be an exact duplicate of the hello.txt file. Moving FilesMoving or renaming files from the Unix Shell is done with the mv command. Mv behaves a lot like copy. When moving multiple files the last argument on the command line specifies the name of the destination directory. When moving a single file the last (second) command line argument can either be the directory into which the file will be moved or the name the file will be given. For example "mv hello.txt world.txt goodbye.txt /tmp" will move the three text files into the /tmp directory. After the command executes the /tmp directory will contain the three files and the current directory will no longer contain the files. The command "mv hello.txt none.txt" will rename the file hello.txt to none.txt. Nothing else besides the name of the file will change. Removing FilesDeleting a file from the Unix shell is done with the "rm" command. To delete a file, type rm followed by the file name to delete. If you want to delete multiple files then type the names of all the files to delete. For example to delete the three text files used in the copying example type "rm hello.txt world.txt goodbye.txt". It is important to remember that Unix does not normally have an undelete function. Once a file is deleted it is gone forever. Directories can also be deleted with the rm command. To delete a directory(and everything in that directory) use the –r (for recursive) option. If you wish to delete the directory /tmp/demo and everything in it, the command "rm -r /tmp/demo" will work. SummaryThe Unix shell should not be feared or loathed. Shells are just pieces of software like any other components of the operating system. Programs can be started from the shell, as can file management tasks. This article dealt with running simple shell commands and performing file management. The next step in mastering the Unix Shell is learning how to connect programs together through the shell. Until then, if you have any comments or suggestions, let me know here. » Next Tutorial Coming Soon!
|