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, rename files to distinguish different versions of a file, or give several names to the same file. You may want to create new directories each time you start working on 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 sections below explain how to make and remove directories and files.
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 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 will replace 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 a -i (interactive) option that will query the user 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 Chapter 3) as well as simple filenames. For example, let’s say your working directory was /users/carol (from the filesystem diagrams in Chapter 3). To copy three files called ch1, ch2, and ch3 from /users/john to a subdirectory called work (that’s /users/carol/work), by entering:
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] workcp: overwrite work/ch2?
There was already a file named ch2 in the work directory. When cp asked, I answered n to prevent copying ch2. Answering y would overwrite the old ch2.
cp ../john/ch[1-3] .
puts the copies into the working directory.
If the copy is in the same directory as the original, the filenames must be different.
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.
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 have permission to copy a file whose access is denied to you, ask the file’s owner or the system administrator to change the access modes for the file.
Some versions of UNIX have an rcp (remote copy) command for copying files between two computers. In general, you must have accounts on both computers. The syntax of rcp is like cp, but rcp also lets you add the remote hostname to the start of a file or directory pathname. The syntax of each argument is:
hostname : pathname
hostname : is needed only for remote files. You can copy from a remote computer to the local computer, from the local to a remote, or between two remote computers.
For example, let’s copy the files named report.may and report.june
from your home
directory on the computer named giraffe.
Put the copies into your working directory (
.) on the
machine you’re logged in to now:
rcp giraffe:report.may giraffe:report.june .
To use wildcards in the remote filenames, put quotation marks (”name“) around each remote name. For example, to copy all files from your food/lunch subdirectory on your giraffe account into your working directory on the local account, enter:
rcp "giraffe:food/lunch/*" .
Unlike cp, most versions of rcp do not have a -i safety option. Also, even if your system has rcp, your system administrator may not want you to use it for system security reasons. Another command, ftp, is more flexible and secure than rcp.
The command ftp (file transfer protocol) is a flexible way to copy files between two computers. (Some systems have a friendlier version of ftp named ncftp.) Both computers don’t need to be running UNIX, though they do need to be connected by a network (like the Internet) that ftp can use. To start ftp, give the hostname of the remote computer:
ftp will prompt for your username and password on the remote computer. This is something like a remote login (see Chapter 1), but ftp doesn’t start your usual shell. Instead, ftp prints its own prompt and uses a special set of commands for transferring files. Table 4.1 lists the most important ftp commands.
Table 4-1. Some ftp Commands
Copies the file filename from your local computer to the remote computer. If you give a second argument, the remote copy will have that name.
Changes ftp’s working directory on the local machine to pathname (ftp starts at your working directory on the local computer). Note that the ftp lcd command changes only ftp’s working directory. After you quit ftp, your shell’s working directory will not have changed.
Lists the remote directory (like ls -l).
Transfers plain text files, translating data if needed.
Here’s an example. Carol uses ftp to copy the file todo from her work subdirectory on her account on the remote computer rhino:
lsafile ch2 somefile %
ftp rhinoConnected to rhino.zoo.com. Name (rhino:carol):
dirtotal 3 -rw-r--r-- 1 csmith mgmt 47 Feb 5 1997 for.ed -rw-r--r-- 1 csmith mgmt 264 Oct 11 12:18 message -rw-r--r-- 1 csmith mgmt 724 Nov 20 14:53 todo ftp>
lsafile ch2 somefile todo
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 a -i option for safety, your mv probably has one too.
mv chap1 intro%
The previous example changed the name of the file chap1 to intro. If you list your files with ls, you will see that the filename chap1 has disappeared.
If your account has lots of files, organizing those files into subdirectories can help you find the files later. Sometimes you may not remember which subdirectory has a file. The find command can search for files in many ways; we’ll look at two of them.
Change to your home directory so find will start its search there. Then carefully enter one of the two find commands below. (The syntax is strange and ugly—but find does the job!)
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 (home) directory and all its subdirectories for files (type f) whose names start with chap. (find understands wildcards in filenames.) 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 command. 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 that have alpha in their names. (If you get a lot of output, add a pipe to more or pg—see 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 7) or read the chapter about them in O’Reilly’s UNIX Power Tools.
rm removes the named files, as the following examples show:
lschap10 chap2 chap5 cold chap1a.old chap3.old chap6 haha chap1b chap4 chap7 oldjunk %
rm *.old chap10%
lschap1b chap4 chap6 cold oldjunk chap2 chap5 chap7 haha %
lshaha 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.
Here’s another easy mistake to make: You want to enter a command like 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 will ask you whether you want to remove each file.
Just as you can create new directories, you can also remove them with the rmdir command. As a precaution, the rmdir command will not 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:
If a directory you try to remove does contain files, you will get a message like “rmdir: dirname not empty”.
To delete a directory that contains some files:
Enter cd dirname to get into the directory you want to delete.
Enter rm * to remove all files in that directory.
Enter cd .. to go to the parent directory.
Enter rmdir dirname to remove the unwanted directory.
Use ls -a to check that there are no hidden files (names that
start with a period) other than
working directory and its parent). The command rm .[a-zA-Z] .??* is good for cleaning up hidden files.
You read above about ftp, a program for transferring files across a network—possibly to non-UNIX operating systems. Your system may also be able to run operating systems other than UNIX. For instance, many Linux systems can also run MS-DOS and Windows 95. If yours does, you can probably use those files from your Linux account.
If the DOS or 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 DOS file C:\WORD\REPORT.DOC through the pathname /dosc/word/report.doc.
Your Linux (or other) system may also have the MTOOLS utilities.
These give you DOS-like commands that interoperate with the UNIX-like system.
For example, we can put a Windows 95 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 %