📄 filearchiv.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>File and Archiving Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="Text Processing Commands"HREF="textproc.html"><LINKREL="NEXT"TITLE="Communications Commands"HREF="communications.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="textproc.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="communications.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="FILEARCHIV"></A>15.5. File and Archiving Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAARCHIVING1"></A>Archiving</B></P><DL><DT><ANAME="TARREF"></A><BCLASS="COMMAND">tar</B></DT><DD><P>The standard UNIX archiving utility. <ANAME="AEN10910"HREF="#FTN.AEN10910">[1]</A> Originally a <ICLASS="WORDASWORD">Tape ARchiving</I> program, it has developed into a general purpose package that can handle all manner of archiving with all types of destination devices, ranging from tape drives to regular files to even <TTCLASS="FILENAME">stdout</TT> (see <AHREF="special-chars.html#EX58">Example 3-4</A>). GNU <ICLASS="FIRSTTERM">tar</I> has been patched to accept various compression filters, for example: <BCLASS="COMMAND">tar czvf archive_name.tar.gz *</B>, which recursively archives and <AHREF="filearchiv.html#GZIPREF">gzips</A> all files in a directory tree except <AHREF="external.html#DOTFILESREF">dotfiles</A> in the current working directory (<AHREF="variables2.html#PWDREF">$PWD</A>). <ANAME="AEN10921"HREF="#FTN.AEN10921">[2]</A> </P><P>Some useful <BCLASS="COMMAND">tar</B> options: <OLTYPE="1"><LI><P><TTCLASS="OPTION">-c</TT> create (a new archive)</P></LI><LI><P><TTCLASS="OPTION">-x</TT> extract (files from existing archive)</P></LI><LI><P><TTCLASS="OPTION">--delete</TT> delete (files from existing archive)</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This option will not work on magnetic tape devices.</P></TD></TR></TABLE></DIV></LI><LI><P><TTCLASS="OPTION">-r</TT> append (files to existing archive)</P></LI><LI><P><TTCLASS="OPTION">-A</TT> append (<ICLASS="FIRSTTERM">tar</I> files to existing archive)</P></LI><LI><P><TTCLASS="OPTION">-t</TT> list (contents of existing archive)</P></LI><LI><P><TTCLASS="OPTION">-u</TT> update archive</P></LI><LI><P><TTCLASS="OPTION">-d</TT> compare archive with specified filesystem</P></LI><LI><P><TTCLASS="OPTION">--after-date</TT> only process files with a date stamp <SPANCLASS="emphasis"><ICLASS="EMPHASIS">after</I></SPAN> specified date</P></LI><LI><P><TTCLASS="OPTION">-z</TT> <AHREF="filearchiv.html#GZIPREF">gzip</A> the archive</P><P>(compress or uncompress, depending on whether combined with the <TTCLASS="OPTION">-c</TT> or <TTCLASS="OPTION">-x</TT>) option</P></LI><LI><P><TTCLASS="OPTION">-j</TT> <AHREF="filearchiv.html#BZIPREF">bzip2</A> the archive</P></LI></OL> </P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>It may be difficult to recover data from a corrupted <ICLASS="FIRSTTERM">gzipped</I> tar archive. When archiving important files, make multiple backups.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="SHARREF"></A><BCLASS="COMMAND">shar</B></DT><DD><P><ICLASS="FIRSTTERM">Shell archiving</I> utility. The files in a shell archive are concatenated without compression, and the resultant archive is essentially a shell script, complete with <SPANCLASS="TOKEN">#!/bin/sh</SPAN> header, and containing all the necessary unarchiving commands. <ICLASS="FIRSTTERM">Shar archives</I> still show up in Usenet newsgroups, but otherwise <BCLASS="COMMAND">shar</B> has been pretty well replaced by <BCLASS="COMMAND">tar</B>/<BCLASS="COMMAND">gzip</B>. The <BCLASS="COMMAND">unshar</B> command unpacks <ICLASS="FIRSTTERM">shar</I> archives.</P></DD><DT><ANAME="ARREF"></A><BCLASS="COMMAND">ar</B></DT><DD><P>Creation and manipulation utility for archives, mainly used for binary object file libraries.</P></DD><DT><ANAME="RPMREF"></A><BCLASS="COMMAND">rpm</B></DT><DD><P>The <ICLASS="FIRSTTERM">Red Hat Package Manager</I>, or <BCLASS="COMMAND">rpm</B> utility provides a wrapper for source or binary archives. It includes commands for installing and checking the integrity of packages, among other things.</P><P>A simple <BCLASS="COMMAND">rpm -i package_name.rpm</B> usually suffices to install a package, though there are many more options available.</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><TTCLASS="USERINPUT"><B>rpm -qf</B></TT> identifies which package a file originates from.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rpm -qf /bin/ls</B></TT> <TTCLASS="COMPUTEROUTPUT">coreutils-5.2.1-31</TT> </PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><TTCLASS="USERINPUT"><B>rpm -qa</B></TT> gives a complete list of all installed <ICLASS="FIRSTTERM">rpm</I> packages on a given system. An <TTCLASS="USERINPUT"><B>rpm -qa package_name</B></TT> lists only the package(s) corresponding to <TTCLASS="FILENAME">package_name</TT>.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rpm -qa</B></TT> <TTCLASS="COMPUTEROUTPUT">redhat-logos-1.1.3-1 glibc-2.2.4-13 cracklib-2.7-12 dosfstools-2.7-1 gdbm-1.8.0-10 ksymoops-2.4.1-1 mktemp-1.5-11 perl-5.6.0-17 reiserfs-utils-3.x.0j-2 ...</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rpm -qa docbook-utils</B></TT> <TTCLASS="COMPUTEROUTPUT">docbook-utils-0.6.9-2</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rpm -qa docbook | grep docbook</B></TT> <TTCLASS="COMPUTEROUTPUT">docbook-dtd31-sgml-1.0-10 docbook-style-dsssl-1.64-3 docbook-dtd30-sgml-1.0-10 docbook-dtd40-sgml-1.0-11 docbook-utils-pdf-0.6.9-2 docbook-dtd41-sgml-1.0-10 docbook-utils-0.6.9-2</TT> </PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV></DD><DT><ANAME="CPIOREF"></A><BCLASS="COMMAND">cpio</B></DT><DD><P>This specialized archiving copy command (<BCLASS="COMMAND">c</B>o<BCLASS="COMMAND">p</B>y <BCLASS="COMMAND">i</B>nput and <BCLASS="COMMAND">o</B>utput) is rarely seen any more, having been supplanted by <BCLASS="COMMAND">tar</B>/<BCLASS="COMMAND">gzip</B>. It still has its uses, such as moving a directory tree. With an appropriate block size (for copying) specified, it can be appreciably faster than <BCLASS="COMMAND">tar</B>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX48"></A><P><B>Example 15-29. Using <ICLASS="FIRSTTERM">cpio</I> to move a directory tree</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Copying a directory tree using cpio. 4 5 # Advantages of using 'cpio': 6 # Speed of copying. It's faster than 'tar' with pipes. 7 # Well suited for copying special files (named pipes, etc.) 8 #+ that 'cp' may choke on. 9 10 ARGS=2 11 E_BADARGS=65 12 13 if [ $# -ne "$ARGS" ] 14 then 15 echo "Usage: `basename $0` source destination" 16 exit $E_BADARGS 17 fi 18 19 source="$1" 20 destination="$2" 21 22 ################################################################### 23 find "$source" -depth | cpio -admvp "$destination" 24 # ^^^^^ ^^^^^ 25 # Read the 'find' and 'cpio' info pages to decipher these options. 26 # The above works only relative to $PWD (current directory) . . . 27 #+ full pathnames are specified. 28 ################################################################### 29 30 31 # Exercise: 32 # -------- 33 34 # Add code to check the exit status ($?) of the 'find | cpio' pipe 35 #+ and output appropriate error messages if anything went wrong. 36 37 exit $?</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="RPM2CPIOREF"></A><BCLASS="COMMAND">rpm2cpio</B></DT><DD><P>This command extracts a <BCLASS="COMMAND">cpio</B> archive from an <AHREF="filearchiv.html#RPMREF">rpm</A> one.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DERPM"></A><P><B>Example 15-30. Unpacking an <ICLASS="FIRSTTERM">rpm</I> archive</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # de-rpm.sh: Unpack an 'rpm' archive 3 4 : ${1?"Usage: `basename $0` target-file"} 5 # Must specify 'rpm' archive name as an argument. 6 7 8 TEMPFILE=$$.cpio # Tempfile with "unique" name. 9 # $$ is process ID of script. 10 11 rpm2cpio < $1 > $TEMPFILE # Converts rpm archive into 12 #+ cpio archive. 13 cpio --make-directories -F $TEMPFILE -i # Unpacks cpio archive. 14 rm -f $TEMPFILE # Deletes cpio archive. 15 16 exit 0 17 18 # Exercise: 19 # Add check for whether 1) "target-file" exists and 20 #+ 2) it is an rpm archive. 21 # Hint: Parse output of 'file' command.</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FACOMPRESSION1"></A>Compression</B></P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -