October 24, 2014
 
 
RSSRSS feed

The Unix Shell - Part One - page 2

Introduction and Basic Concepts

  • June 22, 1999
  • By Steve Singer
File Management

Many shell scripts involve copying, moving, or removing files. All of these tasks can easily be handled from a standard shell.

Listing Files

The 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 Files

Copying 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 Files

Moving 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 Files

Deleting 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.

Summary

The 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!
Sitemap | Contact Us