- 论坛徽章:
- 0
|
There are plenty of ways you can archive files in Unix, mostly with the help of third-party applications
There are plenty of ways you can archive files in Unix, mostly with the help of third-party applications.
The first one is "tar". The name comes from "tape archiver", as it was once used for writing files to tapes. It still offers that functionality, but is also quite useful when archiving files on disk.
!! Note -- tar does NOT use compression. It simply creates a single archive file from a number of files and/or directories.
** Basic archiving example -- the following command will create an archive named somefile.tar, which will contain a number of files (listed at the end of the command):
tar cvf somefile.tar file1 file2 file3
- "c" is for "create".
- "v" is for "verbose" -- shows the name of files as they are archived.
- "f" is for "file" -- this is a standard option.
** Basic de-archiving example:
tar xvf somefile.tar
- "x" is for "extract"
** List the files in an archive without extracting them:
tar tvf somefile.tar
- "t" is for "test" (if I am not mistaken).
!! Note -- the commands you just saw work recursively. You can apply the same commands to whole directory structures.
Another command is "gzip". It is simply a zip utility by the GNU project (
[color="#eee3ff"]http://www.gnu.org
). It comes standard on most Unix systems and performs file compression.
Usually, when you have to archive a large number of files/directories, you use tar first, and then gzip.
** Compressing a file. The following will produce a compressed file named somefile.tar.gz.
gzip somefile.tar
** Uncompressing a file. This will produce the original file, somefile.tar.
gzip -d somefile.tar.gz
** Uncompressing a file and pointing the output to the screen instead of a file. This is useful in a situation described below.
gzip -dc somefile.tar.gz
Finally, here are several useful examples of how to use tar and gzip together.
** Uncompress and untar an archive. This will leave the original *.tar.gz file in place, as well as will produce the end result:
gzip -dc somefile.tar.gz | tar xvf -
What we just did was unzip the file, point the output to the console, then pipe that output to the tar command. The "-" at the end is mandatory.
** Some Unix systems (such as Linux) offer a shorter version of the above command, but keep in mind that it is not supported everywhere:
tar xvfz somefile.tar.gz
Similarly, you can create an archive on Linux using the same logic:
tar cvfz somefile.tgz file1 file2 file3
"tgz" is another alternative for "tar.gz".
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10290/showart_48825.html |
|