Home | Hardware | Internet News |Web Hosting |IT Management |Network Storage
LinuxPlanet
Search 
  Power Search | Tips 

 Front Door
 Discussion
 LinuxEngine
 Opinions
 Reports
 Reviews
 Tutorials
 News
 Technology Jobs

 Browse by subject.
Free Newsletter

Java/Open Source Daily
Linux Today
More Free Newsletters

Be a Commerce Partner


















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

Print this article
Email this article
Related Items

•  The unofficial ex MS-DOS user and LINUX newbie FAQ


   LinuxPlanet / Tutorials







The Unix Shell - Part One
File Management and Summary

Steve Singer
Tuesday, June 22, 1999 12:12:27 PM

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!

« Back: Introduction and Basic Concepts

Skip Ahead

1 Introduction and Basic Concepts
2 File Management and Summary





Linux is a trademark of Linus Torvalds.


internet.com home | search | help! | about us

Jupiter Online Media

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers