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, 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.
4.4.1 Creating Directories
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 command. The format is:
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:
4.4.2 Copying Files
If you're about to edit a file, you may want to save a copy of it first. Doing that makes it easy to get back the original version.
The cp command 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:
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:
You can also use the form:
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:
Or, you could use wildcards and let the shell find all the appropriate files. This time, let's add the -i option for safety:
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 .
The shorthand forms
puts the copies into the working directory.
22.214.171.124 Problem checklist
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 : 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
from your home
directory on the computer named
Put the copies into your working directory (
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:
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, Getting Started ), 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.
Here's an example. Carol uses ftp to copy the file todo from her work subdirectory on her account on the remote computer rhino :
We've covered the most basic
commands here. Entering
4.4.3 Renaming and Moving Files
You may need to change a filename. To rename a file, use the mv (move) command. The mv command can also move a file from one directory to another.
The mv command has the same syntax as the cp command:
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.
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.
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 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!)
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:
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, Where to Go from Here ) or read the chapter about them in O'Reilly's UNIX Power Tools .
4.4.5 Removing Files and Directories
You may have finished working on a file or directory and see no need to keep it, or the contents may be obsolete. Periodically removing unwanted files and directories will free storage space.
The rm command removes files. The syntax is simple:
rm removes the named files, as the following examples show:
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.
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:
126.96.36.199 Problem checklist
4.4.6 Files on Other Operating Systems
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
into our current directory
Your system administrator should be able to tell you whether other filesystems are mounted, whether you have utilities like MTOOLS, and how to use them.