⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx03.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
$ cd journal

$ mkdir 94</PRE>

<H3 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=4><B>Working with Files</B>

<BR></FONT></A></CENTER></H3>

<P>Now that you know how to create, list, and view files, create directories, and move around the UNIX file tree, it's time to learn how to copy, rename, and remove files.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Copying Files with </B><B><I>cp</I></B>

<BR></FONT></A></CENTER></H4>

<P>To copy one or more files, you use the cp command. You might want to use cp to make a backup copy of a file before you edit it, or to copy a file from a friend's directory into your own.

<BR></P>

<P>Suppose that you want to edit a letter but also keep the first draft in case you later decide that you like it best. You could enter the following:

<BR></P>

<PRE>$ cd letters

$ ls

andrea zach

$ cp andrea andrea.back

$ ls

andrea andrea.back zach</PRE>

<P>(When it works, cp prints no output, following the UNIX tradition that &quot;no news is good news.&quot;)

<BR></P>

<P>Now you have two identical files: the original andrea file and a new filenamed andrea.back. The first file that you give to cp is sometimes called the target, and the second the destination. The destination can be a file (as in the preceding example) or 

a directory. For instance, you might decide to create a subdirectory of letters in which to keep backups of all your correspondence:

<BR></P>

<PRE>$ cd letters

$ mkdir backups

$ ls

andrea backups zach

$ cp andrea backups

$ ls backups

andrea</PRE>

<P>Note that the destination of the cp command is simply backups, not backups/andrea. When you copy a file into a directory, cp creates the new file with the same name as the original unless you specify something else. To give the file a different name, 
enter it as follows:

<BR></P>

<PRE>$ cp andrea backups/andrea.0

$ ls backups

andrea.0</PRE>

<P>As you can see, ls works differently when you give it a directory rather than a file as its command-line argument. When you enter ls <I>some_file</I>, ls prints that file's name if the file exists; otherwise, the command prints the following error 
message:

<BR></P>

<PRE><I>some</I>_file: No such file or directory</PRE>

<P>If you enter ls <I>some_dir</I>, ls prints the names of any files in <I>some_dir</I>; otherwise, the command prints nothing. If the directory doesn't exist, ls prints the following error message:

<BR></P>

<PRE><I>some_dir</I>: No such file or directory</PRE>

<P>You can also use cp to copy several files at once. If plan to edit both of your letters and want to save drafts of both, you could enter the following:

<BR></P>

<PRE>$ cd letters

$ ls

andrea backups zach

$ cp andrea zach backups

$ ls backups

andrea zach</PRE>

<P>When copying more than one file at a time, you must specify an existing directory as the destination. Suppose that you enter the following:

<BR></P>

<PRE>$ cd letters

$ ls

andrea    backups    zach

$ cp andrea zach both

cp: both not found</PRE>

<P>The cp command expects its last argument to be an existing directory, and prints an error message when it can't find the directory.

<BR></P>

<P>If what you want is to catenate two files into a third, use cat and shell redirection:

<BR></P>

<PRE>$ cat andrea zach &gt; both</PRE>

<P>You can also use the directory names dot and dot-dot as the destination in cp commands. Suppose that a colleague has left some files named data1 and data2 in the system temporary directory /tmp so that you can copy them to your home directory. You could 

enter the following:

<BR></P>

<PRE>$ cd

$ cp /tmp/data1 .

$ cp /tmp/data2 .

$ ls

data1 data2</PRE>

<P>Alternatively, because the destination is dot, a directory, you can copy both files at once:

<BR></P>

<PRE>$ cp /tmp/data1 /tmp/data2 .

$ ls

data1 data2</PRE>

<P>To copy the files to the parent directory of your CWD, use dot-dot rather than dot.

<BR></P>

<P>By default, cp silently overwrites (destroys) existing files. In the preceding example, if you already have a filenamed data1 and you type <B>cp </B><B>/tmp/data1 .</B>, you lose your copy of data1 forever, replacing it with /tmp/data1. You can use cp's 

-i (interactive) option to avoid accidental overwrites:

<BR></P>

<PRE>$ cp -i /tmp/data1 .

cp: overwrite./data1(y/n)?</PRE>

<P>When you use the -i option, cp asks whether you want to overwrite existing files. If you do, type <B>y</B>; if you don't, type <B>n</B>. If you're accident-prone or nervous, and your shell enables you to do so, you may want to create an alias that 
always uses cp -i (see Chapters 12 and 13, &quot;Korn Shell&quot; and &quot;C Shell&quot;).

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>Moving Files with </B><B><I>mv</I></B>

<BR></FONT></A></CENTER></H4>

<P>The mv command moves files from one place to another. Because each UNIX file has a unique pathname derived from its location in the file tree, moving a file is equivalent to renaming it: you change the pathname. The simplest use of mv is to rename a 
file in the current directory. Suppose that you've finally grown tired of typing <B>cat recipe-for-linguini</B> and want to give your fingers a rest. Instead, you can enter the following:

<BR></P>

<PRE>$ mv recipe-for-linguini linguini</PRE>

<P>There is an important difference between cp and mv: cp leaves the original file in its place, but mv removes it. Suppose that you enter the following command:

<BR></P>

<PRE>$ mv linguini /tmp</PRE>

<P>This command removes the copy of linguini in your CWD. So, if you want to retain your original file, use cp instead of mv.

<BR></P>

<P>Like cp, mv can handle multiple files if the destination is a directory. If your journal is to be a long-term project, you may want to put the monthly files in subdirectories that are organized by the year. Enter the following commands:

<BR></P>

<PRE>$ cd journal

$ <B>ls</B>

Apr_93 Dec_93 Jan_93 Jun_93 May_93 Oct_93

Aug_93 Feb_93 Jul_93 Mar_93 Nov_93 Sep_93

$ mkdir 93

$ mv *_93 93

$ ls

93

$ ls 93

Apr_93 Dec_93 Jan_93 Jun_93 May_93 Oct_93

Aug_93 Feb_93 Jul_93 Mar_93 Nov_93 Sep_93</PRE>

<P>Note that, by default, ls sorts filenames in dictionary order down columns. Often, such sorting is not what you want. The following tip suggests ways that you can work around this problem. Also note that mv, like other UNIX commands, enables you to use 

shell wild cards such as *.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>You can work around ls's default sorting order by prefixing filenames with punctuation (but not hyphens or plus signs), digits, or capitalization. For instance, if you want to sort the files of month 
names in their natural order, prefix them with 00, 01, and so on:

<BR>

<BR>$ cd journal/93

<BR>$ <B>ls</B>

<BR>01_jan 03_mar 05_may 07_jul 09_sep 11_nov

<BR>02_feb 04_apr 06_jun 08_aug 10_oct 12_dec

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Like cp, mv silently overwrites existing files by default:

<BR></P>

<PRE>$ <B>ls</B>

borscht      strudel

$ mv borscht strudel

$ <B>ls</B>

strudel</PRE>

<P>This command replaces the original file strudel with the contents of bortsch, and strudel's original contents are lost forever. If you use mv's -i option, the mv command, like cp, asks you before overwriting files.

<BR></P>

<P>Also like cp, mv requires that you specify dot or dot-dot as the destination directory. In fact, this requirement is true of all UNIX commands that expect a directory argument.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I12" NAME="I12">

<FONT SIZE=3><B>Removing Files with </B><B><I>rm</I></B>

<BR></FONT></A></CENTER></H4>

<P>You can remove unwanted files with rm. This command takes as its arguments the names of one or more files, and removes those files&#151;forever. Unlike operating systems like DOS, which can sometimes recover deleted files, UNIX removes files once and 
forever. Your systems administrator may be able to recover a deleted file from a backup tape, but don't count on it. (Besides, systems administrators become noticeably cranky after a few such requests.) Be especially careful when using shell wild cards to 

remove files&#151;you may end up removing more than you intended.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>Shell wild-card expansions may be dangerous to your mental health, especially if you use them with commands like rm. If you're not sure which files will match the wild cards that you're using, first use 

echo to check. For instance, before entering <B>rm a*</B>, first enter <B>echo a*</B>. If the files that match a* are the ones that you expect, you can enter the rm command, confident that it will do only what you intend.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>To remove the file andrea.back, enter the following command:

<BR></P>

<PRE>$ rm andrea.back</PRE>

<P>Like cp and mv, rm prints no output when it works.

<BR></P>

<P>If you are satisfied with your edited letters and want to remove the backups to save disk space, you could enter the following:

<BR></P>

<PRE>$ cd letters/backups

$ ls

andrea zach

$ rm *

$ ls</PRE>

<P>Because you have removed all the files in the subdirectory backups, the second ls command prints nothing.

<BR></P>

<P>Like cp and mv, rm has an interactive option, -i. If you enter <B>rm -i *</B>, rm asks you whether you really want to remove each individual file. As before, you type <B>y</B> for yes and <B>n</B> for no. This option can be handy if you accidentally 
create a filename with a nonprinting character, such as a control character. Nonprinting characters don't appear in file listings, and they make the file hard to work with. If you want to remove the file, enter <B>rm -i *</B>; then type <B>y</B> for the 
file you that you want to remove, while typing <B>n</B> for the others. The -i option also comes in handy if you want to remove several files that have such dissimilar names that you cannot specify them with wild cards. Again, simply enter <B>rm -i *</B>, 

and then type <B>n</B> for each file that you don't want to remove.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I13" NAME="I13">

<FONT SIZE=4><B>Working with Directories</B>

<BR></FONT></A></CENTER></H3>

<P>A directory is simply a special kind of file. Some of the operations that work with files also work with directories. However, some operations are not possible, and others must be done differently for directories.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I14" NAME="I14">

<FONT SIZE=3><B>Creating Multiple Directories with </B><B><I>mkdir</I></B>

<BR></FONT></A></CENTER></H4>

<P>As mentioned in the section &quot;Creating Directories with mkdir,&quot; you make directories with the mkdir command. In that section, you created a single directory, journal. However, mkdir can also create multiple directories at once. For example, to 

create two directories named journal and recipes, enter the following command:

<BR></P>

<PRE>$ mkdir journal recipes</PRE>

<P>The mkdir command can even create a directory and its subdirectories if you use its -p option:

<BR></P>

<PRE>$ mkdir -p journal/94</PRE>

<H4 ALIGN="CENTER">

<CENTER><A ID="I15" NAME="I15">

<FONT SIZE=3><B>Removing a Directory with </B><B><I>rmdir</I></B>

<BR></FONT></A></CENTER></H4>

<P>To remove an empty directory, use rmdir. Suppose that you made a typing mistake while creating a directory and want to remove it so that you can create the right one. Enter these commands:

<BR></P>

<PRE>$ mkdir jornal

$ rmdir jornal

$ mkdir journal</PRE>

<P>The rmdir command removes only empty directories. If a directory still has files, you must remove them before using rmdir:

<BR></P>

<PRE>$ rmdir journal

rmdir: journal: Directory not empty

$ rm journal/*

$ rmdir journal</PRE>

<P>Actually, rm can remove directories if you use its -r (recursive) option. This option tells rm to descend the file tree below the directory, remove all the files and subdirectories below it, and finally remove the directory itself. So, before you use 
this option, be sure that you mean to remove all the files and the directory. If you decide that you'll never eat Creole-style cuisine again, you can remove those recipes by entering the following:

<BR></P>

<PRE>$ cd recipes

$ rm -r creole</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>The rm command is like a chainsaw: It's a good tool, but one with which you can saw off your leg if you're not careful. The -r option is particularly dangerous&#151;especially if you use it with shell 
wild cards&#151;because it lops off entire branches of the file tree. If you have a directory of precious files that you don't want to accidentally remove, create a filenamed -no-rm-star in the same directory by entering the following:

<BR>

<BR>$ echo just say no &gt; -no-rm-star

<BR>

<BR>Now suppose that this directory also has two precious files named p1 and p2. If you enter <B>rm *</B>, your shell expands the wild card and runs the rm command with the following arguments:

<BR>

<BR>rm -no-rm-star p1 p2

<BR>

<BR>Because rm doesn't have an option -no-rm-star, it prints an error message and quits without removing your precious files. Note, however, that this also makes it difficult for you to use wild cards with any UNIX commands in this subdirectory because the 

shell always expands filenames before passing them to commands.

<BR></NOTE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -