home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning UnixSearch this book

4.4. Managing Your Files

The tree structure of the Unix filesystem makes it easy to organize your files. After you make and edit some files, you may want to copy or move files from one directory to another, or rename files to distinguish different versions of a file. You may want to create new directories each time you start a different project.

A directory tree can get cluttered with old files you don't need. If you don't need a file or a directory, delete it to free storage space on the disk. The following sections explain how to make and remove directories and files.

4.4.2. Copying Files

If you're about to edit a file, you may want to save a copy first. That makes it easy to get back the original version.

4.4.2.1. cp

The cp program can put a copy of a file into the same directory or into another directory. cp doesn't affect the original file, so it's a good way to keep an identical backup of a file.

To copy a file, use the command:

cp old new

where old is a pathname to the original file and new is the pathname you want for the copy. For example, to copy the /etc/passwd file into a file called password in your working directory, you would enter:

$ cp /etc/passwd password
$

You can also use the form:

cp old olddir

This puts a copy of the original file old into an existing directory olddir. The copy will have the same filename as the original.

If there's already a file with the same name as the copy, cp replaces the old file with your new copy. This is handy when you want to replace an old copy with a newer version, but it can cause trouble if you accidentally overwrite a copy you wanted to keep. To be safe, use ls to list the directory before you make a copy there. Also, many versions of cp have an -i (interactive) option that asks you before overwriting an existing file.

You can copy more than one file at a time to a single directory by listing the pathname of each file you want copied, with the destination directory at the end of the command line. You can use relative or absolute pathnames (see Section 3.1 in Chapter 3) as well as simple filenames. For example, let's say your working directory is /users/carol (from the filesystem diagram in Figure 3-1). To copy three files called ch1, ch2, and ch3 from /users/john to a subdirectory called work (that's /users/carol/work), enter:

$ cp ../john/ch1 ../john/ch2 ../john/ch3 work

Or, you could use wildcards and let the shell find all the appropriate files. This time, let's add the -i option for safety:

$ cp -i ../john/ch[1-3] work
cp: overwrite work/ch2? n

There is already a file named ch2 in the work directory. When cp asks, answer n to prevent copying ch2. Answering y would overwrite the old ch2.

As you saw in Section 3.1.5.2 in Chapter 3, the shorthand form . puts the copy in the working directory, and .. puts it in the parent directory. For example, the following puts the copies into the working directory:

$ cp ../john/ch[1-3] .

cp can also copy entire directory trees. Use the option -R, for "recursive." There are two arguments after the option: the pathname of the top-level directory you want to copy from, and the pathname of the place where you want the top level of the copy to be. As an example, let's say that a new employee, Asha, has joined John and Carol. She needs a copy of John's work directory in her own home directory. See the filesystem diagram in Figure 3-1. Her home directory is /users/asha. If Asha's own work directory doesn't exist yet (important!), she could type the following commands:

$ cd /users
$ cp -R john/work asha/work

Or, from her home directory, she could have typed "cp -R ../john/work work". Either way, she'd now have a new subdirectory /users/asha/work with a copy of all files and subdirectories from /users/john/work.

WARNING: If you give cp -R the wrong pathnames, it can copy a directory tree into itself--running forever until your filesystem fills up!

If the copy seems to be taking a long time, stop cp with CTRL-Z, then explore the filesystem (ls -RF is handy for this). If all's okay, you can resume the copying by putting the cp job in the background (with bg) so it can finish its slow work. Otherwise, kill cp and do some cleanup--probably with rm -r, which we mention in Section 4.4.5.2 later in this chapter. (See Section 7.1 and Section 7.3 in Chapter 7.)

4.4.4. Finding Files

If your account has lots of files, organizing them into subdirectories can help you find the files later. Sometimes you may not remember which subdirectory has a file. The find program can search for files in many ways; we'll look at two.

Change to your home directory so find will start its search there. Then carefully enter one of the following two find commands. (The syntax is strange and ugly--but find does the job!)

$ cd
$ find . -type f -name "chap*" -print
./chap2
./old/chap10b
$ find . -type f -mtime -2 -print
./work/to_do

The first command looked in your working directory (.) and all its subdirectories for files (-type f) whose names start with chap. (find understands wildcards in filenames. Be sure to put quotes around any filename pattern with a wildcard in it, as we did in the example.) The second command looked for all files that have been created or modified in the last two days (-mtime -2). The relative pathnames that find finds start with a dot (./), the name of the working directory, which you can ignore.

Linux systems, and some others, have the GNU locate program. If it's been set up and maintained on your system, you can use locate to search part or all of a filesystem for a file with a certain name. For instance, if you're looking for a file named alpha-test, alphatest, or something like that, try this:

$ locate alpha
/users/alan/alpha3
/usr/local/projects/mega/alphatest

You'll get the absolute pathnames of files and directories with alpha in their names. (If you get a lot of output, add a pipe to less--see Section 5.2.3 in Chapter 5.) locate may or may not list protected, private files; its listings usually also aren't completely up to date. To learn much more about find and locate, read your online documentation (see Chapter 8) or read the chapter about them in Unix Power Tools (O'Reilly).

4.4.5. Removing Files and Directories

You may have finished work on a file or directory and see no need to keep it, or the contents may be obsolete. Periodically removing unwanted files and directories frees storage space.

4.4.6. Files on Other Operating Systems

Chapter 6 includes Section 6.4, which explains ways to transfer files across a network--possibly to nonUnix operating systems. Your system may also be able to run operating systems other than Unix. For instance, many Linux systems can also run Microsoft Windows. If yours does, you can probably use those files from your Linux account without needing to boot and run Windows.

If the Windows filesystem is mounted with your other filesystems, you'll be able to use its files by typing a Unix-like pathname. For instance, from our PC under Linux, we can access the Windows file C:\WORD\REPORT.DOC through the pathname /winc/word/report.doc.

Your Linux (or other) system may also have the MTOOLS utilities. These give you Windows-like (actually, DOS-like) programs that interoperate with the Unix-like system. For example, we can put a Windows floppy disk in the A: drive and then copy a file named summary.txt into our current directory (.) by entering:

$ mcopy a:summary.txt .
Copying summary.txt
$

The mcopy -t option translates the end-of-line characters in plain-text files from the Windows format to the Unix format or vice versa. In general, don't use -t unless you're sure that you need to translate end-of-line characters. A local expert should be able to tell you about translation, whether other filesystems are mounted or can be mounted, whether you have utilities like MTOOLS, and how to use them.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.