24 Unix Hours in

advertisement
00 8143 fm.qxd
7/18/05
3:24 PM
Page i
Presented by:
Teach
Yourself
Unix
in
Dave Taylor
24
Hours
FOURTH EDITION
800 East 96th Street, Indianapolis, Indiana 46240 USA
00 8143 fm.qxd
7/18/05
3:24 PM
Page ii
Sams Teach Yourself Unix in 24 Hours, Fourth
Edition
Acquisitions Editor
Jenny Watson
Copyright © 2006 by Sams Publishing
Development Editor
Scott Meyers
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of
the information contained herein. Although every precaution has been
taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained herein.
International Standard Book Number: 0-672-32814-3
Managing Editor
Charlotte Clapp
Senior Project Editor
Matthew Purcell
Indexer
Erika Millen
Library of Congress Catalog Card Number: 2005902143
Printed in the United States of America
First Printing: August 2005
08
07
06
05 4
3
2
1
Trademarks
All terms mentioned in this book that are known to be trademarks or
service marks have been appropriately capitalized. Sams Publishing
cannot attest to the accuracy of this information. Use of a term in this
book should not be regarded as affecting the validity of any trademark
or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information
provided is on an “as is” basis. The author and the publisher shall have
neither liability nor responsibility to any person or entity with respect to
any loss or damages arising from the information contained in this
book
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in
quantity for bulk purchases or special sales. For more information,
please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsontechgroup.com
Proofreader
Leslie Joseph
Technical Editor
Timothy Boronczyk
Team Coordinator
Vanessa Evans
Book Designer
Gary Adair
Page Layout
Julie Parks
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 109
HOUR 6
Creating, Moving, Renaming,
and Deleting Files and
Directories
In this hour, you learn the basic Unix file-manipulation commands. These commands will
explain how to create directories with mkdir, remove directories with rmdir, use cp and mv
to move files about in the file system, and use rm to remove files. The rm command has its
dangers: You learn that there isn’t an “unremove” command in Unix, and you also learn
how to circumvent the possible dangers that lurk in the program.
Goals for This Hour
In this hour, you will learn how to
. Create new directories using mkdir
. Copy files to new locations using cp
. Move files to new locations using mv
. Rename files using mv
. Remove directories using rmdir
. Remove files using rm
. Minimize the danger of using the rm command
This hour introduces several tremendously powerful commands that enable you to create
a custom file-system hierarchy (or wreak unintentional havoc on your files). As you learn
these commands, you also learn hints and ideas on how best to use the Unix file system to
keep your files neat and organized.
07 8143 CH06.qxd
110
7/18/05
3:25 PM
Page 110
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
Manipulating the Unix File System
You know how to find out what files are where in the file system using ls, but there
are also various incredibly useful commands that let you manipulate and modify
files, directories, and the file system itself. That’s what we’ll focus on here.
Task 6.1: Creating New Directories Using mkdir
One important aspect of Unix that has been emphasized continually in this book is
that the Unix file system is hierarchical. The Unix file system includes directories
containing files and directories, each directory of which can contain yet more files
and directories. Your own home directory, however, probably doesn’t contain any
directories (except . and .. of course), which prevents you from exploiting what I
call the virtual file cabinet of the file system.
The command for creating directories is actually one of the least complex and most
mnemonic (for Unix, anyway) in this book: mkdir, called “make directory.”
By the
Way
Pronounce the mkdir command as “make dir.”
1. Move to your home directory and examine the files and directories there.
Here’s an example:
% cd
% ls -F
Archives/
InfoWorld/
LISTS
Mail/
News/
OWL/
PubAccessLists.Z
bin/
educ
mailing.lists.bitnet.Z
rumors.26Oct.Z
rumors.5Nov.Z
src/
2. To create a directory, specify what you’d like to name the directory and where
you’d like to locate it in the file system (the default location is your current
working directory):
% mkdir NEWDIR
% ls -F
Archives/
InfoWorld/
LISTS
Mail/
NEWDIR/
Watch
Out!
News/
OWL/
PubAccessLists.Z
bin/
educ
mailing.lists.bitnet.Z
rumors.26Oct.Z
rumors.5Nov.Z
src/
Although Unix is very flexible about file and directory names, as a general rule
you’ll want to avoid spaces, tabs, control characters, and the / character.
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 111
Manipulating the Unix File System
3. That’s all there is to it. You’ve created your first Unix directory, and you can
now list it with ls to see what it looks like:
% ls -ld NEWDIR
drwxrwx--- 2 taylor
% ls -la NEWDIR
total 2
drwxrwx--- 2 taylor
drwx------ 11 taylor
24 Nov
24 Nov
1024 Nov
5 10:48 NEWDIR/
5 10:48 ./
5 10:48 ../
Not surprisingly, the directory is empty other than the two default entries of .
(the directory itself) and .. (the parent directory, your home directory).
4. Look closely at the permissions of the directory. Remember that the permissions are a result of your umask setting. As you learned in the preceding hour,
changing the umask setting changes the default directory permissions. Then,
when you create a new directory, the new permissions will be in place:
% umask
07
% umask 0
% mkdir NEWDIR2
% ls -ld NEWDIR2
drwxrwxrwx 2 taylor
% umask 222
% mkdir NEWDIR3
% ls -ld NEWDIR3
dr-xr-xr-x 2 taylor
24 Nov
5 10:53 NEWDIR2/
24 Nov
5 10:54 NEWDIR3/
5. What happens if you try to create a directory with a name that has already
been used?
% mkdir NEWDIR
mkdir: NEWDIR: File exists
6. To create a directory someplace other than your current location, prefix the
new directory name with a location:
% mkdir /tmp/testme
% ls -l /tmp
-rwx------ 1 zhongqi
-rw------- 1 xujia
-rw-r--r-- 1 beast
-rw-rw---- 1 root
-rw------- 1 qsc
-rwx------ 1 steen
-rwx------ 1 techman
-rw-r--r-- 1 root
-rw------- 1 zhongqi
drwxrwx--- 2 taylor
-rw-r--r-- 1 aru
22724
95594
572
0
0
24953
3711
997536
163579
24
90
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
Nov
4
4
5
5
5
5
5
5
4
5
5
21:33
23:10
05:59
10:30
00:18
10:40
10:45
10:58
20:16
10:56
02:55
/tmp/a.out*
/tmp/active.10122
/tmp/anon1
/tmp/bar.report
/tmp/lh013813
/tmp/mbox.steen*
/tmp/mbox.techman*
/tmp/quotas
/tmp/sp500.1
testme/
/tmp/trouble21972
111
07 8143 CH06.qxd
112
7/18/05
3:25 PM
Page 112
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
Like other basic Unix utilities, most variations of Unix have no arguments for mkdir
so it is quite easy to use. Some variants offer one or more flags, most commonly -m
mode to specify permissions, and -p to have mkdir create any intermediate directo-
ries required. Keep two things in mind: You must have write permission to the current directory if you’re creating a new directory, and you should ensure that the
name of the directory is not the same as (or, to avoid confusion, similar to) a directory name that already exists.
Task 6.2: Copying Files to New Locations Using cp
One of the most basic operations in any system is moving files, the modern-office
computer equivalent of paper shuffling. On a computer, moving files is a simple
matter of using one or two commands: You can move a file to a different location,
or you can create a copy of the file and move the copy to a different location.
Both Windows and Macs have an interesting strategy for differentiating between
moving and copying. If you drag a file to another location on the same device (a
hard disk, for example), by default the computer moves the file to that location. If
you drag the file to a location on a different device (from a floppy to a hard disk, for
instance), the computer automatically copies the file, placing the new, identically
named copy on the new device while leaving the old copy intact.
Unix lacks this subtlety. Instead, Unix lets you choose which of the two operations
you’d like to perform. The two commands are typically succinct Unix mnemonics:
mv to move files, and cp to copy files. The mv command also serves the dual duty of
enabling you to rename files.
By the
Way
Pronounce cp as “sea pea.” When you talk about copying a file, however, say
“copy.” Similarly, pronounce mv as “em vee,” but when you speak of moving a file,
say “move.”
I find myself using cp more than mv because it offers a slightly safer way to organize
files: If I get confused and rename it such that it steps on another file (you’ll see
what I mean in a moment), I still have original copies of all the files.
1. The format of a cp command is to specify first the name of the file you want
to copy and then the new filename. Both names should be either relative filenames (that is, without a leading slash or other indication of the directory) or
absolute filenames. Start out by making a copy of your .login file, naming
the new copy login.copy:
% cp .login login.copy
% ls -ld .login login.copy
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 113
Manipulating the Unix File System
-rw-------rw-------
1 taylor
1 taylor
113
1858 Oct 12 21:20 .login
1858 Nov 5 12:08 login.copy
You can see that the new file is identical in size and permissions but that it
has a more recent creation date, which certainly makes sense.
2. What happens if you try to copy a directory?
% cp olddir newdir
cp: olddir: Is a directory (not copied).
Generally, Unix won’t permit you to use the cp command to copy directories.
I found that this command worked—sort of—on one machine I have used. The
system’s response to the cp command indicated that something peculiar was happening with the following message:
cp: .: Is a directory (copying as plain file)
But the system also created newdir as a regular, executable file. You may find
that your system reacts in this manner, but you probably do not have any use for
it. On the other hand, you might find that your version of cp includes the useful -R
command, which instructs it to recursively copy all files and directories below the
specified location.
3. The cp command is quite powerful, and it can copy many files at once if you
specify a directory as the destination rather than specifying a new filename.
Further, if you specify a directory destination, the program automatically will
create new files and assign them the same names as the original files.
First, you need to create a second file to work with:
% cp .profile profile.copy
Now try it yourself. Here is what I did:
% cp login.copy profile.copy NEWDIR
% ls -l NEWDIR
total 4
-rw------- 1 taylor
1178 Nov
-rw------- 1 taylor
1858 Nov
5 12:18 profile.copy
5 12:18 login.copy
You can use the cp command to copy an original file as a new file or to a specific
directory (the format being cp original-file new-file-or-directory), and you
can copy many files to a directory (cp list-of-files new-directory). With the
-R flag that many versions of cp offer, you can recursively copy all files and directo-
ries below the specified directory. Experiment with creating new directories using
By the
Way
07 8143 CH06.qxd
114
7/18/05
3:25 PM
Page 114
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
mkdir and copying the files into the new locations. Use ls to confirm that the origi-
nals aren’t removed as you go along.
On some Unix systems, a lowercase “r” gives you a recursive copy. You can check
with the man cp command, of course.
Task 6.3: Moving Files to New Locations Using mv
Whereas cp leaves the original file intact, making a sort of electronic equivalent of a
photocopy, mv functions like a more traditional desk: Papers are moved from one
location to another. Rather than creating multiple copies of the files you’re copying,
mv physically relocates them from the old directory to the new.
1. You use mv almost the same way that you use cp:
% ls -l login.copy
-rw------- 1 taylor
1858 Nov
% mv login.copy new.login
% ls -l login.copy new.login
login.copy not found
-rw------- 1 taylor
1858 Nov
5 12:08 login.copy
5 12:08 new.login
2. Also, you move a group of files together using mv almost the same way you do
it using cp:
% cd NEWDIR
% ls
profile.copy login.copy
% mv profile.copy login.copy ..
% ls -l
total 0
% ls –F ..
Archives/
OWL/
InfoWorld/
PubAccessLists.Z
LISTS
bin/
Mail/
profile.copy
NEWDIR/
educ
News/
login.copy
mailing.lists.bitnet.Z
new.login
rumors.26Oct.Z
rumors.5Nov.Z
src/
3. Because you can use mv to rename files or directories, you can relocate the
new directory NEWDIR. However, you cannot use mv to relocate the dot directory because you’re inside it:
% mv . new.dot
mv: .: rename: Invalid argument
4. Both mv and cp can be dangerous. Carefully consider the following example
before trying either mv or cp on your own computer:
% ls -l login.copy profile.copy
-rw------- 1 taylor
1178 Nov
-rw------- 1 taylor
1858 Nov
5 12:38 profile.copy
5 12:37 login.copy
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 115
Manipulating the Unix File System
% cp profile.copy login.copy
% ls -l login.copy profile.copy
-rw------- 1 taylor
1178 Nov
-rw------- 1 taylor
1178 Nov
115
5 12:38 profile.copy
5 12:38 login.copy
Without bothering to warn me, Unix copied the file profile.copy over the
existing file login.copy. Notice that after the cp operation occurred, both files
had the same size and modification dates.
The mv command will cause the same problem:
% ls -l profile.copy login.copy
-rw------- 1 taylor
1178 Nov
-rw------- 1 taylor
1858 Nov
% mv profile.copy login.copy
% ls -l profile.copy login.copy
profile.copy not found
-rw------- 1 taylor
1178 Nov
5 12:42 profile.copy
5 12:42 login.copy
5 12:42 login.copy
The good news is that you can set up Unix so it won’t overwrite files. The bad
news is that for some reason many systems don’t default to this behavior. If your
system is configured reasonably, when you try either of the two preceding dangerous examples, the system’s response is remove login.copy?. You can either
press the Y key to replace the old file or press Enter to change your mind. If your
system cannot be set up to respond this way, you can use the -i flag to both cp
and mv to avoid this problem. Later, you learn how to permanently fix this problem
with a shell alias.
Together, mv and cp are the dynamic duo of Unix file organization. These commands enable you to put the information you want where you want it, leaving
duplicates behind if desired.
Task 6.4: Renaming Files with mv
Both Windows and Macintosh systems have easy ways to rename files. In DOS, you
can use RENAME to accomplish the task. On the Mac, you can select the name under
the file icon and enter a new filename. On Windows, you right-click the icon and
choose Rename from the pop-up menu.
Unix has none of these options. To rename files, you use the mv command, which, in
essence, moves the old name to the new name. It’s a bit confusing, but it works.
1. Rename the file profile.copy with your own first name. Here’s an example:
% ls -l profile.copy
-rw------- 1 taylor
% mv profile.copy dave
1178 Nov
5 13:00 profile.copy
By the
Way
07 8143 CH06.qxd
116
7/18/05
3:25 PM
Page 116
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
% ls -l dave
-rw------- 1 taylor
1178 Nov
5 13:00 dave
2. Rename a directory too:
% ls -ld NEWDIR
drwxrwx--- 2 taylor
512 Nov
% mv NEWDIR New.Sample.Directory
% ls -ld New.Sample.Directory
drwxrwx--- 2 taylor
512 Nov
5 12:32 NEWDIR/
5 12:32 New.Sample.Directory/
3. Be careful! Just as moving files with cp and mv can carelessly overwrite existing files, renaming files using mv can overwrite existing files:
% mv dave login.copy
If you try to use mv to rename a directory with a name that already has been
assigned to a file, the command fails:
% mv New.Sample.Directory dave
mv: New.Sample.Directory: rename: Not a directory
The reverse situation works fine because the file is moved into the directory as
expected. It’s the subtlety of using the mv command to rename files.
4. If you assign a new directory a name that belongs to an existing directory,
some versions of mv will happily overwrite the existing directory and name the
new one as requested:
% mkdir testdir
% mv New.Sample.Directory testdir
Being able to rename files is another important part of building a useful Unix virtual file cabinet for you. Some major dangers are involved, however, so tread carefully
and always use ls in conjunction with cp and mv to ensure that in the process you
don’t overwrite or replace an existing file.
Task 6.5: Removing Directories with rmdir
Now that you can create directories with the mkdir command, it’s time to learn how
to remove directories using the rmdir command.
1. With rmdir, you can remove any directory for which you have appropriate
permissions:
% mkdir test
% ls -l test
total 0
% rmdir test
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 117
Manipulating the Unix File System
117
Note that the output of ls shows there are no files in the test directory.
2. The rmdir command removes only directories that are empty:
% mkdir test
% touch test/sample.file
% ls -l test
total 0
-rw-rw---- 1 taylor
0 Nov
% rmdir test
rmdir: test: Directory not empty
5 14:00 sample.file
To remove a directory, you must first remove all files therein using the rm command. In this example, test still has files in it. Note: some versions of rmdir
offer a -p flag which will recursively remove all directories and subdirectories
from right-to-left until it encounters a non-empty directory.
3. Permissions are important, too. Consider what happens when I try to remove
a directory that I don’t have permission to touch:
% rmdir /tmp
rmdir: /tmp: Permission denied
% ls -l /tmp
drwxrwxrwt 81 root
15872 Nov
5 14:07 /tmp
The permissions of the parent directory, rather than the directory you’re trying
to remove, are the important considerations.
There’s no way to restore a directory you’ve removed, so be careful and think
through what you’re doing. The good news is that, because with rmdir you can’t
remove a directory having anything in it (a second reason the attempt in the preceding example to remove /tmp would have failed), you’re reasonably safe from
major gaffes. You are not safe, however, with the next command, rm, because it will
remove anything.
Task 6.6: Removing Files Using rm
The rm command is the most dangerous command in Unix. Lacking any sort of
archival or restoration feature, the rm command removes files permanently. It’s like
throwing a document into a shredder instead of into a dustbin.
On some Unixes, there’s a command called shred, actually, that removes files but
also overwrites the spot on the disk where the file information was previously
stored. It’s even more ruthless than rm and should be used with extreme caution,
if at all.
Watch
Out!
07 8143 CH06.qxd
118
7/18/05
3:25 PM
Page 118
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
1. Removing a file using rm is easy. Here’s an example:
% ls -l login.copy
-rw------- 1 taylor
% rm login.copy
% ls -l login.copy
login.copy not found
1178 Nov
5 13:00 login.copy
If you decide that you removed the wrong file and actually wanted to keep the
login.copy file, it’s too late. You’re out of luck.
By the
Way
There’s no “recycle bin” or “trashcan” from which you can recover accidentally
deleted files in Unix. Be careful!
2. You can remove more than one file at a time by specifying each of the files to
the rm command:
% ls -F
Archives/
PubAccessLists.Z
InfoWorld/
bin/
LISTS
profile.copy
Mail/
educ
News/
login.copy
OWL/
mailing.lists.bitnet.Z
% rm profile.copy login.copy new.login
% ls -F
Archives/
OWL/
InfoWorld/
PubAccessLists.Z
LISTS
bin/
Mail/
educ
News/
mailing.lists.bitnet.Z
new.login
rumors.26Oct.Z
rumors.5Nov.Z
src/
test/
testdir/
rumors.26Oct.Z
rumors.5Nov.Z
src/
test/
testdir/
3. Fortunately, rm does have a command flag that to some degree helps avoid
accidental file removal. When you use the -i flag to rm (the i stands for interactive in this case), the system will ask you whether you’re sure you want to
remove the file:
% touch testme
% rm -i testme
rm: remove testme? n
% ls testme
testme
% rm -i testme
rm: remove testme? y
% ls testme
testme not found
Note that n is no and y is yes. Delete the file.
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 119
Manipulating the Unix File System
119
4. Another flag that is often useful for rm, but is very dangerous, is the -r flag
for recursive deletion of files (a recursive command repeatedly invokes itself).
When the -r flag to rm is used, Unix will remove any specified directory along
with all its contents:
% ls -ld test
drwxrwxrwx 3
total 1
-rw-rw---- 1
drwxrwx--- 2
; ls -lR test
taylor
512 Nov
5 15:32 test
taylor
taylor
0 Nov
512 Nov
5 15:32 alpha
5 15:32 test2
0 Nov
5 15:32 file1
test/test2:
total 0
-rw-rw---- 1 taylor
% rm -r test
% ls -ld test
test not found
Without any warning or indication that it was going to do something so drastic, entering rm -r test caused not just the test directory, but all files and
directories inside it as well, to be removed.
This latest example demonstrates that you can give several commands in a single
Unix command line. To do this, separate the commands with a semicolon. Instead
of giving the commands ls -ld test and ls -lR test on separate lines, I
opted for the more efficient ls -ld test; ls -lR test, which executes the
commands one after the other.
The Unix equivalent of the paper shredder, the rm command allows easy removal of
files. With the -r flag, you can even clean out an entire directory, even if it contains
subdirectories and files within those subdirectories. With the -f flag, any warning
messages or other permission issues will be ignored, making the command even
more dangerous. The one command you never want to run on a Unix system is rm
-rf /*, for what I hope are obvious reasons!
Again, remember that nothing can be retrieved after the fact, however, so use great
caution.
Task 6.7: Minimizing the Danger of the rm
Command
At this point, you might be wondering why I am making such a big deal of the rm
command and the fact that it does what it is advertised to do—that is, remove files.
The answer is that learning a bit of paranoia now can save you immense grief in
By the
Way
07 8143 CH06.qxd
120
7/18/05
3:25 PM
Page 120
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
the future. It can prevent you from destroying a file full of information you really
needed to save.
For Windows, commercial programs (Norton Utilities, for instance) exist that can
retrieve accidentally removed files. As with the Mac, the Windows trash can is a folder that you can open and extract files from within until it’s “emptied.” Programs
such as Symantec Utilities for the Macintosh can be used to recover files that have
been deleted by emptying the trash can.
Unix just doesn’t have that capability, though, and files that are removed are gone
forever.
The only exception is if you work on a Unix system that has an automatic, reliable
backup schedule. In such a case, you might be able to retrieve from a storage tape
an older version of your file (maybe).
That said, you can do a few things to lessen the danger of using rm and yet give
yourself the ability to remove unwanted files.
1. You can use a shorthand, a shell alias, to attach the -i flag automatically to
each use of rm. To do this, ascertain what type of login shell you’re running,
which you can do most easily by using the following command. (Don’t worry
about what it all does right now. You learn about the grep command a few
hours from now.)
% grep taylor /etc/passwd
taylor:?:19989:1412:Dave Taylor:/users/taylor:/bin/csh
The last word on the line is what’s important. The /etc/passwd file is one of
the database files Unix uses to track accounts. Each line in the file is called a
password entry or password file entry. On my password entry, you can see that
the login shell specified is /bin/csh. If you try this and you don’t have an
identical entry, you should have /bin/sh or /bin/ksh.
2. If your entry is /bin/csh, enter exactly what is shown here:
% echo “alias rm /bin/rm -I” >> ~/.profile
% source ~/.profile
Now rm includes the -i flag each time it’s used:
% touch testme
% rm testme
rm: remove testme? N
3. If your entry is /bin/ksh or /bin/bash, enter exactly what is shown here,
paying particular attention to the two different quotation-mark characters
used in the example:
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 121
Manipulating the Unix File System
121
$ echo ‘alias rm=”/bin/rm -I”’ >> ~/.profile
$ . ~/.profile
Now rm includes the -i flag each time it’s used.
One thing to pay special attention to is the difference between the single quote
(‘), the double quote (“), and the backquote (`). Unix interprets each differently,
although single and double quotes are often interchangeable. The backquotes,
also known as grave accents, are more unusual and delineate commands within
other commands.
4. If your entry is /bin/sh, you cannot program your system to include the -i
flag each time rm is used. The Bourne shell, as sh is known, is the original command shell of Unix. The Bourne shell lacks an alias feature, a feature that both
the Korn shell (ksh) and the C shell (csh) include. As a result, I recommend that
you change your login shell to one of these alternatives, if available.
To see what’s available, look in the /bin directory on your machine for the
specific shells:
% ls -lF /bin/*sh
-rwxr-xr-x 1 root
-r-xr-xr-x 1 root
-r-xr-xr-x 1 root
-r-xr-xr-x 1 root
-rwxr-xr-x 1 root
wheel
wheel
wheel
wheel
wheel
603488
348068
603488
348068
479120
3
3
3
3
3
Nov
Nov
Nov
Nov
Nov
22:35
22:35
22:35
22:35
22:35
/bin/bash*
/bin/csh*
/bin/sh*
/bin/tcsh*
/bin/zsh*
Most of the examples in this book focus on the Korn Shell because I think it’s
the easiest of the shells to use. To change your login shell you’ll need to email
your system administrator with the request, though some versions of Unix
offer the chsh (change shell) command for just this purpose. If yours does, use
man chsh to learn more about how to use it. Now you can go back to step 2
and set up a Korn shell alias. This will help you avoid mischief with the rm
command.
The best way to avoid trouble with any of these commands is to learn to be just a
bit paranoid about them. Before you remove a file, make sure it’s the one you want
to remove. Before you remove a directory, make doubly sure that it doesn’t contain
any files you might want. Before you rename a file or directory, double-check to see
whether renaming it is going to cause any trouble. Before you hit Enter, make sure
the command is exactly what you want to invoke.
Take your time with the commands you learned in this hour, and you should be
fine. Even in the worst case, you will hopefully have the safety net of a backup performed by a system administrator, but don’t rely on it.
By the
Way
07 8143 CH06.qxd
122
7/18/05
3:25 PM
Page 122
HOUR 6: Creating, Moving, Renaming, and Deleting Files and Directories
Summary
You now have completed six hours of Unix instruction, and you are armed with
enough commands to cause trouble and make Unix do what you want it to do. In
this hour, you learned the differences between cp and mv for moving files and how
to use mv to rename both files and directories. You also learned how to create directories with the mkdir command and how to remove them with the rmdir command.
And you learned about the rm command for removing files and directories, and how
to avoid getting into too much trouble with it.
Finally, if you were really paying attention, you learned how to identify which login
shell you’re using (csh, ksh, bash, or sh).
Workshop
The Workshop summarizes the key terms you learned and poses some questions
about the topics presented in this chapter. It also provides you with a preview of
what you will learn in the next hour.
Key Terms
recursive command
shell alias
A command that repeatedly invokes itself.
Most Unix shells have a convenient way for you to create abbreviations
for commonly used commands or series of commands, known as shell aliases. For
example, if I always found myself typing ls -F, an alias can let me type just ls
and have the shell automatically add the -F flag each time.
Exercises
1. What are the differences between cp and mv?
2. If you were installing a program from a floppy disk onto a hard disk, would
you use cp or mv?
3. If you know DOS, this question is for you. Although DOS has a RENAME command, it doesn’t have both COPY and MOVE. Which of these two do you think
DOS includes? Why?
4. Try using mkdir to create a directory. What happens and why?
5. You’ve noticed that both rmdir and rm -r can be used to remove directories.
Which is safer to use?
07 8143 CH06.qxd
7/18/05
3:25 PM
Page 123
Workshop
6. The rm command has another flag that wasn’t discussed in this hour. The -f
flag forces removal of files regardless of permission (assuming you’re the
owner, that is). In combination with the -r flag, this can be amazingly
destructive. Why?
Preview of the Next Hour
The seventh hour introduces the useful file command, which indicates the contents
of any file in the Unix file system. With file, you will explore various directories in
the Unix file system to see what the command reveals about different system and
personal files. Then, when you’ve found some files worth reading, you will learn
about cat, more, and pg, which are different ways of looking at the contents of a
file.
123
Download