Manipulating Files with Unix

This tutorial document assumes you've already read the first document in this series entitled "Navigating Directories with UNIX".

Contents


Introduction

UNIX's ability to manipulate files is one of its strengths. This short tutorial is a brief introduction to some of the more important commands in this area.


Viewing files

One way to view files is with the more command (you could also use your favorite editor). For example, type
   # view the file "readme.txt":
   % more readme.txt
to view the file "readme.txt". The more utility displays one page of text at a time on your screen. At the more prompt
   --more--(7%)
press the spacebar to display the next page, or press the question mark (?) to see a list of options and commands. (The "7%" means you've already viewed 7% of the text file.)

The more command is especially useful at the end of a pipe. For instance, the command

   # concatenate three files and view a page at a time:
   % cat file1 file2 file3 | more
concatenates three files and pipes the output through more.


Contents

Moving and copying files

Use the mv and cp commands to move and copy files, respectively. The syntax of the two commands is similar. We give examples of the mv command first.

The mv command is used to move one or more files to a different location (no new files are created in the process). For example, type

   # move file "sidebar.gif" to "tmp" directory:
   % mv sidebar.gif tmp
to move the file "sidebar.gif" from the current directory to the "tmp" directory. (By the way, the command
   % mv tmp/sidebar.gif .
reverses the previous operation, since the dot "." refers to the current directory.) Multiple files are moved by listing them on the command line. For example, the command
   # move files to the parent directory:
   % mv *.bin *.hqx ..
moves all files with extension "bin" or "hqx in the current directory to the parent directory.

The mv command is also used to rename files. For instance, the command

   # rename file "README.TXT" to "readme.txt":
   % mv README.TXT readme.txt
renames the file "README.TXT" to "readme.txt". Directory names are changed similarly.

Entire directories may be moved with the mv command. For example, the command

   # move directory "tmp/Alpha" to a new location:
   % mv tmp/Alpha /usr/local/archives/public/users/trscavo
essentially prunes "tmp/Alpha" and grafts it onto the target directory.

The cp command is almost identical to the mv command. The only difference is that duplicate copies of the source files are created. To copy an entire directory tree from one location to another, use the "-r" option:

   # recursively copy "tmp/Alpha" to a new location:
   % cp -r tmp/Alpha /usr/local/archives/public/users/trscavo
Thus the following two commands
   cp -r dirname1 dirname2
   rm -r dirname1
are identical to
   mv dirname1 dirname2
but since it's not possible to move files across file systems, the latter is not always possible.

See the next section for more information about the rm command.


Contents

Removing files

Removing (or deleting) files is easy. In fact, it's so easy, you need to be careful. Be forewarned that if you delete a file by mistake, there's no way of getting it back. (Of course, your system administrator may be able to help, but don't count on it.)

To remove a single file, for example, type

   # remove file "strings.html":
   % rm strings.html
To remove a bunch of files, type
   # remove all files with suffix "html":
   % rm *.html
but be careful! If you type
   # remove all files in the current directory BY MISTAKE!:
   % rm * .html
by mistake, you've just wiped out an entire directory! This can be avoided by typing the "-i" option:
   # remove all files with suffix "html" interactively:
   % rm -i *.html
which prompts you before deleting a file. Of course, this takes longer, but it's safer. Indeed, system administrators often make "-i" a default option.

To remove all files in a certain directory, as well as all files in all subdirectories, use the -r option:

   # recursively remove files and directories:
   % rm -r tmp/*
But beware! You could mistakenly wipe out your entire account if you're not careful!


Contents

Command summary

   # view filename one page at a time:
   % more filename
   # move filename to dirname:
   % mv filename dirname
   # rename filename1 to filename2:
   % mv filename1 filename2
   # move dirname1 to dirname2:
   % mv dirname1 dirname2
   # copy filename to dirname:
   % cp filename dirname
   # copy filename1 to filename2:
   % cp filename1 filename2
   # recursively copy dirname1 to dirname2:
   % cp -r dirname1 dirname2
   # remove file filename:
   % rm filename
   # remove file filename interactively:
   % rm -i filename
   # remove all files in the current directory:
   % rm *
   # prune the directory tree at subdirectory dirname:
   % rm -r dirname
The next in this series of tutorial documents is entitled "Manipulating Directories with UNIX".