Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example Analysis of painless File extraction on Linux

Shulou Source: shulou.com Published: 2022-06-01 05:25:21 10月04日 Update

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!

Tags: File script command content file name archive parameter article example analysis appropriate type statement validation different not much just name name most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux Shulou Information Shulou Technology Microsoft