Merge pull request #2460 from theo-l/master

translating
This commit is contained in:
Xingyu.Wang
2015-03-02 13:48:59 +08:00
2 changed files with 191 additions and 187 deletions

View File

@@ -1,187 +0,0 @@
10 quick tar command examples to create/extract archives in Linux
================================================================================
### Tar command on Linux ###
The tar (tape archive) command is a frequently used command on linux that allows you to store files into an archive.
The commonly seen file extensions are .tar.gz and .tar.bz2 which is a tar archive further compressed using gzip or bzip algorithms respectively.
In this tutorial we shall take a look at simple examples of using the tar command to do daily jobs of creating and extracting archives on linux desktops or servers.
### Using the tar command ###
The tar command is available by default on most linux systems and you do not need to install it separately.
> With tar there are 2 compression formats, gzip and bzip. The "z" option specifies gzip and "j" option specifies bzip. It is also possible to create uncompressed archives.
#### 1. Extract a tar.gz archive ####
Well, the more common use is to extract tar archives. The following command shall extract the files out a tar.gz archive
$ tar -xvzf tarfile.tar.gz
Here is a quick explanation of the parameters used -
> x - Extract files
>
> v - verbose, print the file names as they are extracted one by one
>
> z - The file is a "gzipped" file
>
> f - Use the following tar archive for the operation
Those are some of the important options to memorise
**Extract tar.bz2/bzip archives**
Files with extension bz2 are compressed with the bzip algorithm and tar command can deal with them as well. Use the j option instead of the z option.
$ tar -xvjf archivefile.tar.bz2
#### 2. Extract files to a specific directory or path ####
To extract out the files to a specific directory, specify the path using the "-C" option. Note that its a capital C.
$ tar -xvzf abc.tar.gz -C /opt/folder/
However first make sure that the destination directory exists, since tar is not going to create the directory for you and will fail if it does not exist.
#### 3. Extract a single file ####
To extract a single file out of an archive just add the file name after the command like this
$ tar -xz -f abc.tar.gz "./new/abc.txt"
More than once file can be specified in the above command like this
$ tar -xv -f abc.tar.gz "./new/cde.txt" "./new/abc.txt"
#### 4. Extract multiple files using wildcards ####
Wildcards can be used to extract out a bunch of files matching the given wildcards. For example all files with ".txt" extension.
$ tar -xv -f abc.tar.gz --wildcards "*.txt"
#### 5. List and search contents of the tar archive ####
If you want to just list out the contents of the tar archive and not extract them, use the "-t" option. The following command prints the contents of a gzipped tar archive,
$ tar -tz -f abc.tar.gz
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/abc.txt
...
Pipe the output to grep to search a file or less command to browse the list. Using the "v" verbose option shall print additional details about each file.
For tar.bz2/bzip files use the "j" option
Use the above command in combination with the grep command to search the archive. Simple!
$ tar -tvz -f abc.tar.gz | grep abc.txt
-rw-rw-r-- enlightened/enlightened 0 2015-01-13 11:40 ./new/abc.txt
#### 6. Create a tar/tar.gz archive ####
Now that we have learnt how to extract existing tar archives, its time to start creating new ones. The tar command can be told to put selected files in an archive or an entire directory. Here are some examples.
The following command creates a tar archive using a directory, adding all files in it and sub directories as well.
$ tar -cvf abc.tar ./new/
./new/
./new/cde.txt
./new/abc.txt
The above example does not create a compressed archive. Just a plain archive, that puts multiple files together without any real compression.
In order to compress, use the "z" or "j" option for gzip or bzip respectively.
$ tar -cvzf abc.tar.gz ./new/
> The extension of the file name does not really matter. "tar.gz" and tgz are common extensions for files compressed with gzip. ".tar.bz2" and ".tbz" are commonly used extensions for bzip compressed files.
#### 7. Ask confirmation before adding files ####
A useful option is "w" which makes tar ask for confirmation for every file before adding it to the archive. This can be sometimes useful.
Only those files would be added which are given a yes answer. If you do not enter anything, the default answer would be a "No".
# Add specific files
$ tar -czw -f abc.tar.gz ./new/*
add ./new/abc.txt?y
add ./new/cde.txt?y
add ./new/newfile.txt?n
add ./new/subdir?y
add ./new/subdir/in.txt?n
# Now list the files added
$ tar -t -f abc.tar.gz
./new/abc.txt
./new/cde.txt
./new/subdir/
#### 8. Add files to existing archives ####
The r option can be used to add files to existing archives, without having to create new ones. Here is a quick example
$ tar -rv -f abc.tar abc.txt
> Files cannot be added to compressed archives (gz or bzip). Files can only be added to plain tar archives.
#### 9. Add files to compressed archives (tar.gz/tar.bz2) ####
Its already mentioned that its not possible to add files to compressed archives. However it can still be done with a simple trick. Use the gunzip command to uncompress the archive, add file to archive and compress it again.
$ gunzip archive.tar.gz
$ tar -rf archive.tar ./path/to/file
$ gzip archive.tar
For bzip files use the bzip2 and bunzip2 commands respectively.
#### 10. Backup with tar ####
A real scenario is to backup directories at regular intervals. The tar command can be scheduled to take such backups via cron. Here is an example -
$ tar -cvz -f archive-$(date +%Y%m%d).tar.gz ./new/
Run the above command via cron and it would keep creating backup files with names like -
'archive-20150218.tar.gz'.
Ofcourse make sure that the disk space is not overflown with larger and larger archives.
#### 11. Verify archive files while creation ####
The "W" option can be used to verify the files after creating archives. Here is a quick example.
$ tar -cvW -f abc.tar ./new/
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/newfile.txt
./new/abc.txt
Verify ./new/
Verify ./new/cde.txt
Verify ./new/subdir/
Verify ./new/subdir/in.txt
Verify ./new/newfile.txt
Verify ./new/abc.txt
Note that the verification cannot be done on compressed archives. It works only with uncompressed tar archives.
Thats all for now. For more check out the man page for tar command, with "man tar".
--------------------------------------------------------------------------------
via: http://www.binarytides.com/linux-tar-command/
作者:[Silver Moon][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:https://plus.google.com/117145272367995638274/posts

View File

@@ -0,0 +1,191 @@
linux中创建和解压文档的10个快速tar命令样例
================================================================================
### linux中的tar命令###
tar磁带归档命令是linux系统中被经常用来将文件存入到一个归档文件中的命令。
常见的文件扩展包括:.tar.gz 和 .tar.bz2, 分别表示通过gzip或bzip算法进一步压缩的磁带归档文件扩展。
在该教程中我们会窥探一下在linux桌面或服务器版本中使用tar命令来处理一些日常创建和解压归档文件的工作样例。
### 使用tar命令###
tar命令在大部分linux系统默认情况下都是可用的所以你不用单独安装该软件。
> tar命令具有两个压缩格式gzip和bzip该命令的“z”选项用来指定gzip“j”选项用来指定bzip。同时也可哟用来创建非压缩归档文件。
#### 1.解压一个tar.gz归档 ####
一般常见的用法是用来解压归档文件下面的命令将会把文件从一个tar.gz归档文件中解压出来。
$ tar -xvzf tarfile.tar.gz
这里对这些参数做一个简单解释-
> x - 解压文件
> v - 繁琐,在解压每个文件时打印出文件的名称。
> z - 该文件是一个使用 gzip压缩的文件。
> f - 使用接下来的tar归档来进行操作。
这些就是一些需要记住的重要选项。
**解压 tar.bz2/bzip 归档文件 **
具有bz2扩展名的文件是使用bzip算法进行压缩的但是tar命令也可以对其进行处理但是是通过使用“j”选项来替换“z”选项。
$ tar -xvjf archivefile.tar.bz2
#### 2.将文件解压到一个指定的目录或路径 ####
为了将文件解压到一个指定的目录中,使用“-C”选项来指定路径此处的“C”是大写“C”。
$ tar -xvzf abc.tar.gz -C /opt/folder/
然后首先需要确认目标目录是否存在毕竟tar命令并不会为你创建目录所以如果目标目录不存在的情况下该命令会失败。
####3. 解压出单个文件 ####
为了从一个归档文件中解压出单个文件,只需要将文件名按照以下方式将其放置在命令后面。
$ tar -xz -f abc.tar.gz "./new/abc.txt"
在上述命令中,可以按照以下方式来指定多个文件。
$ tar -xv -f abc.tar.gz "./new/cde.txt" "./new/abc.txt"
#### 4.使用通配符来解压多个文件 ####
通配符可以用来解压于给定通配符匹配的一批文件,例如所有以".txt"作为扩展名的文件。
$ tar -xv -f abc.tar.gz --wildcards "*.txt"
#### 5. 列出并检索tar归档文件中的内容 ####
如果你仅仅想要列出而不是解压tar归档文件的中的内容使用“-t”选项 下面的命令用来打印一个使用gzip压缩过的tar归档文件中的内容。
$ tar -tz -f abc.tar.gz
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/abc.txt
...
将输出通过管道定向到grep来搜索一个文件或者定向到less命令来浏览内容列表。 使用"v"繁琐选项将会打印出每个文件的额外详细信息。
对于 tar.bz2/bzip文件需要使用"j"选项。
结合上述的命令和grep命令来检索归档文件如下所示。简单吧
$ tar -tvz -f abc.tar.gz | grep abc.txt
-rw-rw-r-- enlightened/enlightened 0 2015-01-13 11:40 ./new/abc.txt
#### 6.创建一个tar/tar.gz归档文件 ####
现在我们已经学过了如何解压一个tar归档文件是时候开始创建一个新的tar归档文件了。tar命令可以用来将所选的文件或整个目录放入到一个归档文件中以下是相应的样例。
下面的命令使用一个目录来创建一个tar归档文件它会将该目录中所有的文件和子目录都加入到归档文件中。
$ tar -cvf abc.tar ./new/
./new/
./new/cde.txt
./new/abc.txt
上述命令不会创建一个压缩的的归档文件,只是一个普通的归档文件,只是将多个文件放入到一个归档文件中并没有真正地压缩每个文件。
为了使用压缩可以分别使用“z”或“j”选项进行gzip或bzip压缩算法。
$ tar -cvzf abc.tar.gz ./new/
> 文件的扩展名其实并不真正有什么影响。“tar.gz” 和tgz是gzip压缩算法压缩文件的常见扩展名。 “tar.bz2”和“tbz”是bzip压缩算法压缩文件的常见扩展名。
#### 7. 在添加文件之前进行确认 ####
一个有用的选项是“w”该选项使得tar命令在添加每个文件到归档文件之前来让用户进行确认有时候这会很有用。
使用该选项时只有用户输入yes时的文件才会被加入到归档文件中如果你输入任何东西默认的回答是一个“No”。
# 添加指定文件
$ tar -czw -f abc.tar.gz ./new/*
add ./new/abc.txt?y
add ./new/cde.txt?y
add ./new/newfile.txt?n
add ./new/subdir?y
add ./new/subdir/in.txt?n
#现在列出所有被加入的文件
$ tar -t -f abc.tar.gz
./new/abc.txt
./new/cde.txt
./new/subdir/
#### 8. 加入文件到存在的归档文件中 ####
“r”选项可以被用来将文件加入到已存在的归档文件中而不用创建一个新的归档文件下面是一个简单的样例
$ tar -rv -f abc.tar abc.txt
> 文件并不能加入到已压缩的归档文件中gz 或 bzip。文件只能被加入到普通的归档文件中。
#### 9. 将文件加入到压缩的归档文件中tar.gz/tar.bz2) ####
之前已经提到了不可能将文件加入到已压缩的归档文件中然和依然可以通过简单的一些把戏来完成。使用gunzip命令来解压缩归档文件然后将文件加入到归档文件中后重新进行压缩。
$ gunzip archive.tar.gz
$ tar -rf archive.tar ./path/to/file
$ gzip archive.tar
对于bzip文件分别使用bzip2和bunzip2。
#### 10.通过tar来进行备份 ####
一个真实的场景是在规则的间隔内来备份目录tar命令可以通过cron调度来实现这样的一个备份以下是一个样例 -
$ tar -cvz -f archive-$(date +%Y%m%d).tar.gz ./new/
使用cron来运行上述的命令会保持创建类似以下名称的备份文件 -
'archive-20150218.tar.gz'.
当然,需要确保日益增长的归档文件不会导致磁盘空间的溢出。
#### 11. 在创建归档文件是进行验证 ####
"W"选项可以用来在创建归档文件之后进行验证,以下是一个简单例子。
$ tar -cvW -f abc.tar ./new/
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/newfile.txt
./new/abc.txt
Verify ./new/
Verify ./new/cde.txt
Verify ./new/subdir/
Verify ./new/subdir/in.txt
Verify ./new/newfile.txt
Verify ./new/abc.txt
需要注意的是验证动作不能呢该在压缩过的归档文件上进行只能在非压缩的tar归档文件上执行。
现在就先到此为止可以通过“man tar”命令来查看tar命令的的手册。
--------------------------------------------------------------------------------
via: http://www.binarytides.com/linux-tar-command/
作者:[Silver Moon][a]
译者:[theo-l](https://github.com/theo-l)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:https://plus.google.com/117145272367995638274/posts