mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-28 23:20:10 +08:00
commit
7cd9394b3c
@ -7,16 +7,14 @@
|
||||
[#]: via: (https://www.networkworld.com/article/3564265/painless-file-extraction-on-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
Painless file extraction on Linux
|
||||
Linux 上无痛文件解压
|
||||
======
|
||||
|
||||
Thinkstock
|
||||
从 Linux 系统的存档中解压文件要比拔牙要麻烦得多,但有时看起来更麻烦。在本文中,我们将探讨如何轻松地从几乎任何可能在 Linux 系统中使用的存档中解压文件。
|
||||
|
||||
Extracting files from archives in Linux systems is considerably less painful than tooth extraction, but sometimes seems more complicated. In this post, we will take a look at how you can easily extract files from just about any kind of archive you’re likely to run into on a Linux system.
|
||||
它们有很多格式,从 .gz 到 .tbz2,这些文件的命名方式有一些变化。当然,你可以记住所有可用于从存档中解压文件的命令以及它们的选项,但是你也可以将所有经验保存到脚本中,而不必担心细节。
|
||||
|
||||
There’s a pile of them – everything from .gz to .tbz2 files with some variations for how those files are named. Sure, you can memorize all of the various commands available for extracting files from archives along with the options they offer, but you can also just deposit all that know-how into a script and stop worrying about the details.
|
||||
|
||||
In this post, we assemble a series of extraction commands into a script that calls the proper command to extract the content of file archives depending on the archive file names. The script starts with some commands to verify that a file name has been provided as an argument or ask that the person running the script provide one.
|
||||
在本文中,我们将一系列解压命令组合成一个脚本,它会调用适当的命令根据文档名解压文件的内容。该脚本以一些命令开头来验证文件名是否已经提供作为参数,或要求运行脚本的人提供文件名。
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
@ -34,9 +32,9 @@ if [ ! -f "$filename" ]; then
|
||||
fi
|
||||
```
|
||||
|
||||
Got that? The script prompts for a file name if no arguments were offered and uses the argument provided if there is one. It then verifies that the file actually exists. If not, the script exits.
|
||||
了解了么?如果未提供任何参数,脚本将提示输入文件名,如果存在则使用它。然后,它验证文件是否实际存在。如果不是,那么脚本退出。
|
||||
|
||||
The next step is to use a bash case statement to call the appropriate extraction command for the archive depending on its name. For some of these file types (e.g., .bz2), other commands than tar would also work, but we only include one extraction command for each file naming convention. So, here’s the case statement with the various archive file names.
|
||||
下一步是使用 bash case 语句根据存档文件的名称为存档文件调用适当的解压命令。对于其中某些文件类型(例如 .bz2),也可以使用除 tar 之外的其他命令,但是对于每种文件命名约定,我们仅包含一个解压命令。因此,这是带有各种存档文件名的 case 语句。
|
||||
|
||||
```
|
||||
case $filename in
|
||||
@ -54,9 +52,9 @@ The next step is to use a bash case statement to call the appropriate extraction
|
||||
esac
|
||||
```
|
||||
|
||||
If the file provided to the script has a file extension that doesn’t match any of those known to the script, the “No extract option for $filename” message will be issued. If any archive types that you use are missing, just add them along with the required extraction commands.
|
||||
如果提供给脚本的文件扩展名与脚本已知的扩展名都不匹配,那么会发出 “No extract option for $filename” 的消息。如果缺少你使用的任何存档类型,只需将它们与所需的提取命令一起添加即可。
|
||||
|
||||
Add the bash header to the top of the script, make it executable and you should be ready to go.
|
||||
将 bash 头添加到脚本顶部,使其可执行,然后就可以开始了。
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
@ -88,7 +86,7 @@ case $filename in
|
||||
*)
|
||||
```
|
||||
|
||||
If you want the script to display the contents of the archive as they are being extracted, add the verbose option (-v) to each string of command arguments:
|
||||
如果你希望脚本在解压文件时显示内容,请将详细选项(-v)添加到每个命令参数字符串中:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
@ -120,11 +118,11 @@ case $filename in
|
||||
esac
|
||||
```
|
||||
|
||||
### Wrap-up
|
||||
### 总结
|
||||
|
||||
While it’s certainly possible to create an alias for each extraction command you’re likely to use, it’s easier to let a script provide the command for each file type you encounter than having to stop and work out each of the commands and options yourself.
|
||||
虽然可以为每个可能用到的解压命令创建别名,但是让脚本为遇到的每种文件类型提供命令要比自己停下来编写每个命令和选项容易。
|
||||
|
||||
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
|
||||
加入 [Facebook][1] 和 [LinkedIn][2] 上的 Network World 社区,评论热门主题。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -132,7 +130,7 @@ via: https://www.networkworld.com/article/3564265/painless-file-extraction-on-li
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user