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

📄 filearchiv.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!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.57"><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"><TABLEWIDTH="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">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 12. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="communications.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="FILEARCHIV">12.5. File and Archiving Commands</A></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="AEN8586"HREF="#FTN.AEN8586">[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	      tar has been patched to accept various compression	      filters, such as <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 (<BCLASS="COMMAND">$PWD</B>).		<ANAME="AEN8596"HREF="#FTN.AEN8596">[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="EMPHASIS">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">-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="EMPHASIS">gzipped</I> tar	      archive. When archiving important files, make multiple	      backups.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">shar</B></DT><DD><P>Shell archiving 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 Internet 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><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="EMPHASIS">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="EMPHASIS">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><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.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX48"></A><P><B>Example 12-27. Using <BCLASS="COMMAND">cpio</B> to move a directory tree</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Copying a directory tree using 'cpio.'   4&nbsp;   5&nbsp;# Advantages of using 'cpio':   6&nbsp;#   Speed of copying. It's faster than 'tar' with pipes.   7&nbsp;#   Well suited for copying special files (named pipes, etc.)   8&nbsp;#+  that 'cp' may choke on.   9&nbsp;  10&nbsp;ARGS=2  11&nbsp;E_BADARGS=65  12&nbsp;  13&nbsp;if [ $# -ne "$ARGS" ]  14&nbsp;then  15&nbsp;  echo "Usage: `basename $0` source destination"  16&nbsp;  exit $E_BADARGS  17&nbsp;fi    18&nbsp;  19&nbsp;source=$1  20&nbsp;destination=$2  21&nbsp;  22&nbsp;find "$source" -depth | cpio -admvp "$destination"  23&nbsp;#               ^^^^^         ^^^^^  24&nbsp;# Read the 'find' and 'cpio' man page to decipher these options.  25&nbsp;  26&nbsp;  27&nbsp;# Exercise:  28&nbsp;# --------  29&nbsp;  30&nbsp;#  Add code to check the exit status ($?) of the 'find | cpio' pipe  31&nbsp;#+ and output appropriate error messages if anything went wrong.  32&nbsp;  33&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><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 12-28. Unpacking an <ICLASS="EMPHASIS">rpm</I> archive</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# de-rpm.sh: Unpack an 'rpm' archive   3&nbsp;   4&nbsp;: ${1?"Usage: `basename $0` target-file"}   5&nbsp;# Must specify 'rpm' archive name as an argument.   6&nbsp;   7&nbsp;   8&nbsp;TEMPFILE=$$.cpio                         # Tempfile with "unique" name.   9&nbsp;                                         # $$ is process ID of script.  10&nbsp;  11&nbsp;rpm2cpio &#60; $1 &#62; $TEMPFILE                # Converts rpm archive into cpio archive.  12&nbsp;cpio --make-directories -F $TEMPFILE -i  # Unpacks cpio archive.  13&nbsp;rm -f $TEMPFILE                          # Deletes cpio archive.  14&nbsp;  15&nbsp;exit 0  16&nbsp;  17&nbsp;#  Exercise:  18&nbsp;#  Add check for whether 1) "target-file" exists and  19&nbsp;#+                       2) it is really an rpm archive.  20&nbsp;#  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><DL><DT><ANAME="GZIPREF"></A><BCLASS="COMMAND">gzip</B></DT><DD><P>The standard GNU/UNIX compression utility, replacing	      the inferior and proprietary	      <BCLASS="COMMAND">compress</B>. The corresponding decompression	      command is <BCLASS="COMMAND">gunzip</B>, which is the equivalent of	      <BCLASS="COMMAND">gzip -d</B>.</P><P>The <BCLASS="COMMAND">zcat</B> filter decompresses a	      <ICLASS="EMPHASIS">gzipped</I> file to	      <TTCLASS="FILENAME">stdout</TT>, as possible input to a pipe or	      redirection. This is, in effect, a <BCLASS="COMMAND">cat</B>	      command that works on compressed files (including files	      processed with the older <BCLASS="COMMAND">compress</B>	      utility). The <BCLASS="COMMAND">zcat</B> command is equivalent to	      <BCLASS="COMMAND">gzip -dc</B>.</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>On some commercial UNIX systems, <BCLASS="COMMAND">zcat</B>	      is a synonym for <BCLASS="COMMAND">uncompress -c</B>,	      and will not work on <ICLASS="EMPHASIS">gzipped</I>	      files.</P></TD></TR></TABLE></DIV><P>See also <AHREF="comparison-ops.html#EX14">Example 7-7</A>.</P></DD><DT><ANAME="BZIPREF"></A><BCLASS="COMMAND">bzip2</B></DT><DD><P>An alternate compression utility, usually more efficient	      (but slower) than <BCLASS="COMMAND">gzip</B>, especially on	      large files. The corresponding decompression command is	      <BCLASS="COMMAND">bunzip2</B>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Newer versions of <AHREF="filearchiv.html#TARREF">tar</A> have been patched with	      <BCLASS="COMMAND">bzip2</B> support.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">compress</B>, <BCLASS="COMMAND">uncompress</B></DT><DD><P>This is an older, proprietary compression	      utility found in commercial UNIX distributions. The	      more efficient <BCLASS="COMMAND">gzip</B> has largely	      replaced it. Linux distributions generally include a	      <BCLASS="COMMAND">compress</B> workalike for compatibility,	      although <BCLASS="COMMAND">gunzip</B> can unarchive files	      treated with <B

⌨️ 快捷键说明

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