start page | rating of books | rating of authors | reviews | copyrights

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.1. Creating Directories with mkdir

It's handy to group related files in the same directory. If you were writing a spy novel, you probably wouldn't want your intriguing files mixed with restaurant listings. You could create two directories: one for all the chapters in your novel (spy, for example), and another for restaurants (boston.dine).

To create a new directory, use the mkdir program. The syntax is:

mkdir dirname(s)

dirname is the name of the new directory. To make several directories, put a space between each directory name. To continue our example, you would enter:

$ mkdir spy boston.dine

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.2.2. Problem checklist

The system says something like "cp: cannot copy file to itself."
If the copy is in the same directory as the original, the filenames must be different.

The system says something like "cp: filename: no such file or directory."
The system can't find the file you want to copy. Check for a typing mistake. If a file isn't in the working directory, be sure to use its pathname.

The system says something like "cp: permission denied."
You may not have permission to copy a file created by someone else or copy it into a directory that does not belong to you. Use ls -l to find the owner and the permissions for the file, or ls -ld to check the directory. If you feel that you should be able to copy a file, ask the file's owner or a system staff person to change its access modes.

4.4.2.3. Copying files across a network

The cp program works on whatever computer you're logged onto. But, unless your computer has a networked filesystem (see Section 3.1.3 in Chapter 3), you can't copy files to other computers with cp. To do this, see Section 6.4 in Chapter 6.

4.4.3. Renaming and Moving Files with mv

To rename a file, use mv (move). The mv program can also move a file from one directory to another.

The mv command has the same syntax as the cp command:

mv old new

old is the old name of the file and new is the new name. mv will write over existing files, which is handy for updating old versions of a file. If you don't want to overwrite an old file, be sure that the new name is unique. If your cp has an -i option for safety, your mv probably has one too.

$ mv chap1 intro
$

The previous example changed the file named chap1 to intro. If you list your files with ls, you will see that the filename chap1 has disappeared.

The mv command can also move a file from one directory to another. As with the cp command, if you want to keep the same filename, you only need to give mv the name of the destination directory.

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.5.1. rm

The rm program removes files. The syntax is simple:

rm filename(s)

rm removes the named files, as the following example shows:

$ ls
chap10       chap2       chap5    cold
chap1a.old   chap3.old   chap6    haha
chap1b       chap4       chap7    oldjunk
$ rm *.old chap10
$ ls
chap1b    chap4    chap6    cold    oldjunk
chap2     chap5    chap7    haha
$ rm c*
$ ls
haha    oldjunk
$

When you use wildcards with rm, be sure you're deleting the right files! If you accidentally remove a file you need, you can't recover it unless you have a copy in another directory or in the system backups.

WARNING: Do not enter rm * carelessly. It deletes all the files in your working directory.

Here's another easy mistake to make: you want to enter a command such as rm c* (remove all filenames starting with "c") but instead enter rm c * (remove the file named c and all files!).

It's good practice to list the files with ls before you remove them. Or, if you use rm's -i (interactive) option, rm asks you whether you want to remove each file.

4.4.5.2. rmdir

Just as you can create new directories, you can remove them with the rmdir program. As a precaution, rmdir won't let you delete directories that contain any files or subdirectories; the directory must first be empty. (The rm -r command removes a directory and everything in it. It can be dangerous for beginners, though.)

The syntax is:

rmdir dirname(s)

If a directory you try to remove does contain files, you get a message like "rmdir: dirname not empty".

To delete a directory that contains some files:

  1. Enter "cd dirname" to get into the directory you want to delete.

  2. Enter "rm *" to remove all files in that directory.

  3. Enter "cd .." to go to the parent directory.

  4. Enter "rmdir dirname" to remove the unwanted directory.

4.4.5.3. Problem checklist

I still get the message "dirname not empty" even after I've deleted all the files.
Use ls -a to check that there are no hidden files (names that start with a period) other than . and .. (the working directory and its parent). The following command is good for cleaning up hidden files (which aren't matched by a simple wildcard like *):

$ rm .[a-zA-Z] .??*

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.