5.2.1 Using Files: A Tutorial

To use your system, you’ll have to know how to create, move, rename,

and delete files and directories. This section describes how to do

so with the standard Debian commands.

The best way to learn is to try things. As long as you aren’t root

(and haven’t yet created any important personal files), you cannot

mess up too seriously. Jump in—type each of these commands at the

prompt and press Enter.

pwd

One directory is always considered the _current working directory_

for the shell you’re using. You can view this directory with the pwd

command, which stands for Print Working Directory. pwd prints the

name of the directory you’re working in—probably /home/yourname.

ls

ls stands for “list,” as in “list files.” When you type ls, the

system displays a list of all the files in your current working

directory. If you’ve just installed Debian, your home directory may

well be empty. If your working directory is empty, ls produces no

output, because there are no files to list.

cd /

cd means “change directory.” In this case, you’ve asked to change to

the root directory.

pwd

This verifies that you’re working in the root directory.

ls

Lets you see what’s in /.

cd

Typing cd with no arguments selects your home directory— /home/

yourname —as the current working directory. Try pwd to verify this.

Before continuing, you should know that there are actually two

different kinds of filenames. Some of them begin with /, the root

directory, such as

/etc/profile. These are called _absolute_ filenames because they

/refer

to the same file no matter what your current directory is. The other

kind of filename is _relative_.

Two directory names are used _only_ in relative filenames: . and ...

The directory . refers to the current directory, and .. is the

parent directory. These are “shortcut” directories. They exist in

_every_ directory. Even the root directory has a parent

directory—it’s its own parent!

So filenames that include . or .. are _relative_, because their

meaning depends on the current directory. If I’m in /usr/bin and

type ../etc, I’m referring to /usr/etc. If I’m in /var and type

../etc, I’m referring to /etc. Note that a filename without the root

directory at the front implicitly has ./ at the front. So you can

type local/bin, or ./local/bin and it means the same thing.

A final handy tip: The tilde ~ is equivalent to your home directory.

So typing cd ~ is the same as typing cd with no arguments. Also, you

can type things like cd ~/practice/mysubdirectory to change to the

directory

/home/yourname/practice/mysubdirectory. In a similar way, ~myuser is

equivalent to the home directory of the user “myuser,” which is

probably something like /home/myuser; so ~myuser/docs/debian.ps is

equivalent to

/home/myuser/doc/debian.ps.

/

/

/

Here are some more file commands to try out, now that you know about

relative filenames. cd to your home directory before you begin.

mkdir practice

In your home directory, make a directory called practice. You’ll use

this directory to try out some other commands. You might type ls to

verify that your new directory exists.

cd practice

Changes the directory to practice.

mkdir mysubdirectory

Creates a subdirectory of practice.

cp /etc/profile .

cp is short for “copy.” /etc/profile is just a random file on your

system, don’t worry about what it is for now. We’ve copied it to .

(recall that . just means “the directory I’m in now,” or the current

working directory). So this creates a copy of /etc/profile and puts

it in your practice directory. Try typing ls to verify that there’s

indeed a file called profile in your working directory, alongside

the new mysubdirectory.

more profile

This lets you view the contents of the file profile. more is used to

view the contents of text files. It’s called more because it shows

one screenful of the file at a time, and you press the space bar to

see more. more will exit when you get to the end of the file, or

when you press q (quit).

more /etc/profile

Verifies that the original looks just like the copy you made.

mv profile mysubdirectory

mv stands for “move.” You’ve moved the file profile from the current

directory into the subdirectory you created earlier.

ls

Verifies that profile is no longer in the current directory.

ls mysubdirectory

Verifies that profile has moved to mysubdirectory.

cd mysubdirectory

Changes to the subdirectory.

mv profile myprofile

Note that unlike some operating systems, there is no difference

between moving a file and renaming it. Thus there’s no separate

rename command. Note that the second argument to mv can be a

directory to move the file or directory into, or it can be a new

filename. cp works the same way.

As usual, you can type ls to see the result of mv.

mv myprofile ..

Just as . means “the directory I’m in now,” .. means “parent of the

current directory,” in this case the practice directory you created

earlier. Use ls to verify that that’s where myprofile is now.

cd ..

Changes directories to the parent directory—in this case practice,

where you just put myprofile.

rm myprofile

rm means “remove,” so this deletes myprofile. Be careful! Deleting a

file on a GNU/Linux system is _permanent_—there is no undelete. If

you rm it, it’s _gone, forever_. Be careful! To repeat, deleting a

file on a GNU/Linux system is _permanent_—there is no undelete. If

you rm it, it’s _gone, forever_.

rmdir mysubdirectory

rmdir is just like rm, only it’s for directories. Notice that rmdir

only works on empty directories. If the directory contains files,

you must delete those files first, or alternatively you can use rm

-r in place of rmdir.

cd ..

This moves out of the current directory, and into its parent

directory. Now you can type the following:

rmdir practice

This will delete the last remnants of your practice session.

So now you know how to create, copy, move, rename, and delete files

and directories. You also learned some shortcuts, like typing simply

cd to jump to your home directory, and how . and .. refer to the

current directory and its parent, respectively. You should also

remember the concept of the _root directory_, or /, and the alias ~

for your home directory.

Debian GNU/Linux : Guide to Installation and Usage · 第 82 / 193 题