tar.texi

来自「gnu tar 源码包。 tar 软件是 Unix 系统下的一个打包软件」· TEXI 代码 · 共 1,767 行 · 第 1/5 页

TEXI
1,767
字号
@file{collection.tar}.This section will proceed slowly, detailing how to use @option{--create}in @code{verbose} mode, and showing examples using both short and longforms.  In the rest of the tutorial, and in the examples in the nextchapter, we will proceed at a slightly quicker pace.  This sectionmoves more slowly to allow beginning users to understand how@command{tar} works.@menu* prepare for examples::* Creating the archive::* create verbose::* short create::* create dir::@end menu@node prepare for examples@subsection Preparing a Practice Directory for ExamplesTo follow along with this and future examples, create a new directorycalled @file{practice} containing files called @file{blues}, @file{folk}and @file{jazz}.  The files can contain any information you like:ideally, they should contain information which relates to their names,and be of different lengths.  Our examples assume that @file{practice}is a subdirectory of your home directory.Now @command{cd} to the directory named @file{practice}; @file{practice}is now your @dfn{working directory}.  (@emph{Please note}: Althoughthe full file name of this directory is@file{/@var{homedir}/practice}, in our examples we will refer tothis directory as @file{practice}; the @var{homedir} is presumed.In general, you should check that the files to be archived exist whereyou think they do (in the working directory) by running @command{ls}.Because you just created the directory and the files and have changed tothat directory, you probably don't need to do that this time.It is very important to make sure there isn't already a file in theworking directory with the archive name you intend to use (in this case,@samp{collection.tar}), or that you don't care about its contents.Whenever you use @samp{create}, @command{tar} will erase the currentcontents of the file named by @option{--file=@var{archive-name}} (@option{-f @var{archive-name}}) if it exists.  @command{tar}will not tell you if you are about to overwrite an archive unless youspecify an option which does this (@pxref{backup}, for theinformation on how to do so).  To add files to an existing archive,you need to use a different option, such as @option{--append} (@option{-r}); see@ref{append} for information on how to do this.@node Creating the archive@subsection Creating the Archive@xopindex{create, introduced}To place the files @file{blues}, @file{folk}, and @file{jazz} into anarchive named @file{collection.tar}, use the following command:@smallexample$ @kbd{tar --create --file=collection.tar blues folk jazz}@end smallexampleThe order of the arguments is not very important, @emph{when using longoption forms}.  You could also say:@smallexample$ @kbd{tar blues --create folk --file=collection.tar jazz}@end smallexample@noindentHowever, you can see that this order is harder to understand; this iswhy we will list the arguments in the order that makes the commandseasiest to understand (and we encourage you to do the same when you use@command{tar}, to avoid errors).Note that the sequence@option{--file=@-collection.tar} is considered to be @emph{one} argument.If you substituted any other string of characters for@kbd{collection.tar},  then that string would become the name of thearchive file you create.The order of the options becomes more important when you begin to useshort forms.  With short forms, if you type commands in the wrong order(even if you type them correctly in all other ways), you may end up withresults you don't expect.  For this reason, it is a good idea to getinto the habit of typing options in the order that makes inherent sense.@xref{short create}, for more information on this.In this example, you type the command as shown above: @option{--create}is the operation which creates the new archive(@file{collection.tar}), and @option{--file} is the option which letsyou give it the name you chose.  The files, @file{blues}, @file{folk},and @file{jazz}, are now members of the archive, @file{collection.tar}(they are @dfn{file name arguments} to the @option{--create} operation.@xref{Choosing}, for the detailed discussion on these.) Now that they arein the archive, they are called @emph{archive members}, not files.(@pxref{Definitions,members}).When you create an archive, you @emph{must} specify which files youwant placed in the archive.  If you do not specify any archivemembers, @GNUTAR{} will complain.If you now list the contents of the working directory (@command{ls}), you willfind the archive file listed as well as the files you saw previously:@smallexampleblues   folk   jazz   collection.tar@end smallexample@noindentCreating the archive @samp{collection.tar} did not destroy the copies ofthe files in the directory.Keep in mind that if you don't indicate an operation, @command{tar} will notrun and will prompt you for one.  If you don't name any files, @command{tar}will complain.  You must have write access to the working directory,or else you will not be able to create an archive in that directory.@emph{Caution}: Do not attempt to use @option{--create} (@option{-c}) to add files toan existing archive; it will delete the archive and write a new one.Use @option{--append} (@option{-r}) instead.  @xref{append}.@node create verbose@subsection Running @option{--create} with @option{--verbose}@xopindex{create, using with @option{--verbose}}@xopindex{verbose, using with @option{--create}}If you include the @option{--verbose} (@option{-v}) option on the command line,@command{tar} will list the files it is acting on as it is working.  Inverbose mode, the @code{create} example above would appear as:@smallexample$ @kbd{tar --create --verbose --file=collection.tar blues folk jazz}bluesfolkjazz@end smallexampleThis example is just like the example we showed which did not use@option{--verbose}, except that @command{tar} generated the remaining lines@iftex(note the different font styles).@end iftex@ifinfo.@end ifinfoIn the rest of the examples in this chapter, we will frequently use@code{verbose} mode so we can show actions or @command{tar} responses thatyou would otherwise not see, and which are important for you tounderstand.@node short create@subsection Short Forms with @samp{create}As we said before, the @option{--create} (@option{-c}) operation is one of the mostbasic uses of @command{tar}, and you will use it countless times.Eventually, you will probably want to use abbreviated (or ``short'')forms of options.  A full discussion of the three different forms thatoptions can take appears in @ref{Styles}; for now, here is what theprevious example (including the @option{--verbose} (@option{-v}) option) looks likeusing short option forms:@smallexample$ @kbd{tar -cvf collection.tar blues folk jazz}bluesfolkjazz@end smallexample@noindentAs you can see, the system responds the same no matter whether you uselong or short option forms.@FIXME{i don't like how this is worded:} One difference between usingshort and long option forms is that, although the exact placement ofarguments following options is no more specific when using short forms,it is easier to become confused and make a mistake when using shortforms.  For example, suppose you attempted the above example in thefollowing way:@smallexample$ @kbd{tar -cfv collection.tar blues folk jazz}@end smallexample@noindentIn this case, @command{tar} will make an archive file called @file{v},containing the files @file{blues}, @file{folk}, and @file{jazz}, becausethe @samp{v} is the closest ``file name'' to the @option{-f} option, andis thus taken to be the chosen archive file name.  @command{tar} will tryto add a file called @file{collection.tar} to the @file{v} archive file;if the file @file{collection.tar} did not already exist, @command{tar} willreport an error indicating that this file does not exist.  If the file@file{collection.tar} does already exist (e.g., from a previous commandyou may have run), then @command{tar} will add this file to the archive.Because the @option{-v} option did not get registered, @command{tar} will notrun under @samp{verbose} mode, and will not report its progress.The end result is that you may be quite confused about what happened,and possibly overwrite a file.  To illustrate this further, we will showyou how an example we showed previously would look using short forms.This example,@smallexample$ @kbd{tar blues --create folk --file=collection.tar jazz}@end smallexample@noindentis confusing as it is.  When shown using short forms, however, itbecomes much more so:@smallexample$ @kbd{tar blues -c folk -f collection.tar jazz}@end smallexample@noindentIt would be very easy to put the wrong string of charactersimmediately following the @option{-f}, but doing that could sacrificevaluable data.For this reason, we recommend that you pay very careful attention tothe order of options and placement of file and archive names,especially when using short option forms.  Not having the option namewritten out mnemonically can affect how well you remember which optiondoes what, and therefore where different names have to be placed.@node create dir@subsection Archiving Directories@cindex Archiving Directories@cindex Directories, ArchivingYou can archive a directory by specifying its directory name as afile name argument to @command{tar}.  The files in the directory will bearchived relative to the working directory, and the directory will bere-created along with its contents when the archive is extracted.To archive a directory, first move to its superior directory.  If youhave followed the previous instructions in this tutorial, you shouldtype:@smallexample$ @kbd{cd ..}$@end smallexample@noindentThis will put you into the directory which contains @file{practice},i.e., your home directory.  Once in the superior directory, you canspecify the subdirectory, @file{practice}, as a file name argument.  Tostore @file{practice} in the new archive file @file{music.tar}, type:@smallexample$ @kbd{tar --create --verbose --file=music.tar practice}@end smallexample@noindent@command{tar} should output:@smallexamplepractice/practice/bluespractice/folkpractice/jazzpractice/collection.tar@end smallexampleNote that the archive thus created is not in the subdirectory@file{practice}, but rather in the current working directory---thedirectory from which @command{tar} was invoked.  Before trying to archive adirectory from its superior directory, you should make sure you havewrite access to the superior directory itself, not only the directoryyou are trying archive with @command{tar}.  For example, you will probablynot be able to store your home directory in an archive by invoking@command{tar} from the root directory; @xref{absolute}.  (Notealso that @file{collection.tar}, the original archive file, has itselfbeen archived.  @command{tar} will accept any file as a file to bearchived, regardless of its content.  When @file{music.tar} isextracted, the archive file @file{collection.tar} will be re-writteninto the file system).If you give @command{tar} a command such as@smallexample$ @kbd{tar --create --file=foo.tar .}@end smallexample@noindent@command{tar} will report @samp{tar: ./foo.tar is the archive; notdumped}.  This happens because @command{tar} creates the archive@file{foo.tar} in the current directory before putting any files intoit.  Then, when @command{tar} attempts to add all the files in thedirectory @file{.} to the archive, it notices that the file@file{./foo.tar} is the same as the archive @file{foo.tar}, and skipsit.  (It makes no sense to put an archive into itself.)  @GNUTAR{}will continue in this case, and create the archivenormally, except for the exclusion of that one file.  (@emph{Pleasenote:} Other implementations of @command{tar} may not be so clever;they will enter an infinite loop when this happens, so you should notdepend on this behavior unless you are certain you are running@GNUTAR{}.  In general, it is wise to always place the archive outsideof the directory being dumped.@node list@section How to List Archives@opindex listFrequently, you will find yourself wanting to determine exactly what aparticular archive contains.  You can use the @option{--list}(@option{-t}) operation to get the member names as they currentlyappear in the archive, as well as various attributes of the files atthe time they were archived.  For example, you can examine the archive@file{collection.tar} that you created in the last section with thecommand,@smallexample$ @kbd{tar --list --file=collection.tar}@end smallexample@noindentThe output of @command{tar} would then be:@smallexamplebluesfolkjazz@end smallexample@noindentThe archive @file{bfiles.tar} would list as follows:@smallexample./birdsbaboon./box@end smallexample@noindentBe sure to use a @option{--file=@var{archive-name}} (@option{-f@var{archive-name}}) option just as with @option{--create}(@option{-c}) to specify the name of the archive.@xopindex{list, using with @option{--verbose}}@xopindex{verbose, using with @option{--list}}If you use the @option{--verbose} (@option{-v}) option with@option{--list}, then @command{tar} will print out a listingreminiscent of @w{@samp{ls -l}}, showing owner, file size, and soforth.  This output is described in detail in @ref{verbose member listing}.If you had used @option{--verbose} (@option{-v}) mode, the exampleabove would look like:@smallexample$ @kbd{tar --list --verbose --file=collection.tar folk}-rw-r--r-- myself user 62 1990-05-23 10:55 folk@end smallexample

⌨️ 快捷键说明

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