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.

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).

mkdir

To create a new directory, use the mkdir command. The format 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

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.

cp

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:

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] work
cp: overwrite work/ch2? n

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 . and .. will put the copy in the working directory or its parent. For example:

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

puts the copies into the working directory.

Problem checklist

The system says something along the lines of “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 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.

rcp

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.

ftp

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 hostname

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

Command

Description

put filename

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.

mput filenames

Copies the named files (you can use wildcards) from local to remote.

get filename

Copies the file filename from the remote computer to your local computer. If you give a second argument, the local copy will have that name.

mget filenames

Copies the named files (you can use wildcards) from remote to local.

cd pathname

Changes the working directory on the remote machine to pathname (ftp usually starts at your home directory on the remote machine).

lcd pathname

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.

dir

Lists the remote directory (like ls -l).

binary

Tells ftp to copy the following file(s) without translation. This preserves pictures, sound, or other data.

ascii

Transfers plain text files, translating data if needed.

quit

Ends the ftp session and takes you back to a shell prompt.

Here’s an example. Carol uses ftp to copy the file todo from her work subdirectory on her account on the remote computer rhino:

% ls
afile   ch2    somefile
% ftp rhino
Connected to rhino.zoo.com.
Name (rhino:carol): csmith
Password:
ftp> cd work
ftp> dir
total 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> get todo
ftp> quit
% ls
afile   ch2    somefile    todo

We’ve covered the most basic ftp commands here. Entering help at an ftp> prompt gives a list of all commands; entering help followed by an ftp command name gives a one-line summary of that command.

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.

mv

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.

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.

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!)

% 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 (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.

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.

rm

The rm command removes files. The syntax is simple:

rm filename(s)

rm removes the named files, as the following examples show:

% 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 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.

rmdir

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:

rmdir dirname(s)

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:

  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.

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 command rm .[a-zA-Z] .??* is good for cleaning up hidden files.

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 summary.txt into our current directory (.) by entering:

% mcopy a:summary.txt .
Copying summary.txt
%

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.

Get Learning the UNIX Operating System, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.