Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of painless File extraction on Linux

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you on the Linux painless file extraction example analysis, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

They come in many formats, from .gz to .tbz2, and these files are named in some different ways. Of course, you can remember all the commands that extract files from the archive and their options, but you can also save all your experiences to scripts without worrying about the details.

In this article, we combine a series of extraction commands into a script that invokes the appropriate command to extract the contents of the file based on the document name. The script starts with some commands to verify that a file name has been provided as a parameter, or to ask the person running the script to provide a file name.

#! / bin/bash if [$#-eq 0]; then echo-n "filename >" read filename else filename=$1 fi if [!-f "$filename"]; then echo "No such file: $filename" exit $? Fi

Do you understand? If no parameters are provided, the script prompts for a file name and uses it if it exists. It then verifies that the file actually exists. If not, the script exits.

The next step is to use the case statement of bash to invoke the appropriate extract command based on the name of the archive file. For some of these file types (for example, .bz2), you can also use commands other than tar, but for each file naming convention, we include only one extract command. Therefore, this is a case statement with various archive file names:

Case $filename in * .tar) tar xf $filename;; * .tar.bz2) tar xjf $filename;; * .tbz) tar xjf $filename;; * .tbz2) tar xjf $filename;; * .tgz) tar xzf $filename;; * .tar.gz) tar xzf $filename;; * .gz) gunzip $filename;; * .bz2) bunzip2 $filename;; * .zip) unzip $filename ; * .Z) uncompress $filename;; *) echo "No extract option for $filename" esac

If the file extension provided to the script does not match the extension known to the script, the message "No extract option for $filename" is issued. If any of the archive types you use are missing, simply add them along with the desired extraction command.

Add the bash header to the top of the script to make it executable, and then you're ready to start.

#! / bin/bash if [$#-eq 0]; then echo-n "filename >" read filename else filename=$1 fi if [!-f "$filename"]; then echo "No such file: $filename" exit $? Fi case $filename in * .tar) tar xf $filename;; * .tar.bz2) tar xjf $filename;; * .tbz) tar xjf $filename;; * .tbz2) tar xjf $filename;; * .tgz) tar xzf $filename;; * .tar.gz) tar xzf $filename;; * .gz) gunzip $filename;; * .bz2) bunzip2 $filename * .zip) unzip $filename;; * .Z) uncompress $filename;; * .rar) rar x $filename;; *)

If you want the script to display when extracting the file, add the detailed option (- v) to each command parameter string:

#! / bin/bash if [$#-eq 0]; then echo-n "filename >" read filename else filename=$1 fi if [!-f "$filename"]; then echo "No such file: $filename" exit $? Fi case $filename in * .tar) tar xvf $filename;; * .tar.bz2) tar xvjf $filename;; * .tbz) tar xvjf $filename;; * .tbz2) tar xvjf $filename;; * .tgz) tar xvzf $filename;; * .tar.gz) tar xvzf $filename;; * .gz) gunzip-v $filename;; * .bz2) bunzip2-v $filename * .zip) unzip-v $filename;; * .Z) uncompress-v $filename;; *) echo "No extract option for $filename" esac is all the contents of the article "sample Analysis of painless File extraction on Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report