简体   繁体   English

在递归结构中查找特定的文件扩展名并提取它们直到没有剩余

[英]Find specific file extensions in recursive structure and extract them until no left

I have a recursive multiple folder structures.我有一个递归的多个文件夹结构。 Inside of there are multiple zip, img files.里面有多个zip,img文件。 But when I extracted it, new zip,img files come out again.但是当我提取它时,新的 zip,img 文件又出来了。 I want to write a loop for this.我想为此写一个循环。

Scan whole recursive folders, find multiple zip files and extract them.扫描整个递归文件夹,找到多个 zip 文件并解压。 After extraction scan folders again if system found new zip files, extract it again and again continue until no left zip file in the folders.再次提取扫描文件夹后,如果系统发现新的 zip 文件,则一次又一次地提取它,直到文件夹中没有剩余的 zip 文件。

sample.tar.gz
       1.img
           Thefolder
                 samplefolder
                         t.img
       2.img
           samplefolder2
                 different.gz
       3.img
       4.img

There are more folders also under other img files.其他 img 文件下还有更多文件夹。

I tried to write for loop for this but couldn't work it.我试图为此编写 for 循环,但无法正常工作。 After extraction 1.img script getting error.提取 1.img 脚本后出现错误。

Error Output:错误 Output:

ID = 277952352

Everything is Ok

Folders: 3
Files: 1
Size:       345424152
Compressed: 671024244
+ rm -f /home/sample/1.img
+ recursiveExtract /home/sample/Thefolder
+ for file in "$path"/*
+ '[' -d /home/sample/Thefolder ']'
+ recursiveExtract /home/sample/Thefolder
Segmentation fault

The code:编码:

#!/bin/bash
set -x


target_file=$1
path="$(realpath "$1")"
recursiveExtract () { # $1=directory
      for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursiveExtract "$file"
        elif [ -f "$file" -a "${file##*.}" = 'img' ]; then
            7z x $file -o$path -r # variation 2
            rm -f "$file" # comment this if you want to keep the zip files.
            recursiveExtract "${file%.img}"
        fi
        b=$(ls $path | grep img | wc -l)
        if [[ "$b" -eq 0 ]]; then
            break
        fi
      done
      for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursiveExtract "$file"
        elif [ -f "$file" -a "${file##*.}" = 'tar' ]; then
            7z x $file -o$path -r # variation 2
            rm -f "$file" # comment this if you want to keep the zip files.
            recursiveExtract "${file%.tar}"
        fi
        c=$(ls $path | grep tar | wc -l)
        if [[ "$c" -eq 0 ]]; then
            break
        fi
      done
      for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursiveExtract "$file"
        elif [ -f "$file" -a "${file##*.}" = 'gz' ]; then
            7z x $file -o$path -r # variation 2
            rm -f "$file" # comment this if you want to keep the zip files.
            recursiveExtract "${file%.gz}"
        fi
        d=$(ls $path | grep gz | wc -l)
        if [[ "$d" -eq 0 ]]; then
            break
        fi
      done
}
recursiveExtract "$1"

Latest version of my code:我的代码的最新版本:

#!/bin/bash
# set -x

recursiveExtract () { # $1=directory
      path="$(realpath "$1")"
      echo "GZ------------------"
      for file in "$path"/*; do
        if [ -f "$file" -a "${file##*.}" = 'gz' ]; then
            echo "File:" $file
            echo "Path:" $path
            # 7z e "${file%.gz}" -o"$file" # variation 1
            7z x $file -o$path -r -aou # variation 2
            rm -f "$file" # comment this if you want to keep the zip files.
            recursiveExtract "${file%.gz}"
        fi
        # d=$(ls $path | grep gz | wc -l)
        d=$(find $path -type f -name "*.gz" | wc -l)
        echo "WC GZ-----------------:" $d
        if [[ "$d" -eq 0 ]]; then
            break
        fi
      done
      echo "IMG------------------"
      for file in "$path"/*; do
        if [ -f "$file" -a "${file##*.}" = 'img' ]; then
            echo "File:" $file
            echo "Path:" $path
            # 7z e "${file%.img}" -o"$file" # variation 1
            7z x $file -o$path -r -aou # variation 2
            rm -f "$file" # comment this if you want to keep the zip files.
            recursiveExtract "${file%.img}"
        fi
        # b=$(ls $path | grep img | wc -l)
        b=$(find $path -type f -name "*.img" | wc -l)
        echo "WC IMG-----------------:" $b
        if [[ "$b" -eq 0 ]]; then
            break
        fi
      done
}
while true
do
    d=$(find $1 -type f -name "*.gz" | wc -l)
    b=$(find $1 -type f -name "*.img" | wc -l)
    if [[ "$d" -eq 0 ]] && [[ "$b" -eq 0 ]]; then
        break
    else
        recursiveExtract "$1"
    fi
done

This shoud do the job这应该做的工作

#!/bin/bash
find ./ -name "*.zip*" > current_zips.txt
while [[ `wc -l "current_zips.txt" | cut -d' ' -f1` > 0 ]]; do
    find ./ -name "*.zip*" -exec unzip {} \;
    while IFS= read file_; do rm $file_; done < "current_zips.txt" # for file_ in $(cat current_zips.txt);do rm $file_;done
    find ./ -name "*.zip*" > current_zips.txt
done

Use the following script to generate test data使用以下脚本生成测试数据

#!/bin/bash
#create some data
echo "data 0" > file0.txt
mkdir folder1 folder2
echo "data 1" > folder1/file1.txt
echo "data 2" > folder2/file1.txt
#zip data
zip file0.zip file0.txt
zip -r folder1.zip folder1
zip -r folder2.zip folder2
zip -r data.zip *.zip
#delete original data
rm -rf file* folder*

I think you want something like this:我想你想要这样的东西:

recursiveExtract.bash recursiveExtract.bash

#!/bin/bash

# efi,gz,img,tar

recursiveExtract () { # $1=file/directory
    local path="$(realpath "$1")" # This variable must be local.
    for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursiveExtract "$file"
        elif [ -f "$file" -a "${file##*.}" = 'tgz' ]; then # file.tgz == file.tar.gz
            local anotherFile="${file%.*}"'.tar'
            echo 'Extracting: '"$file"
            7z x "$file" -o"${file%/*}" -aoa -bd -bso0
            7z x "$anotherFile" -o"${anotherFile%.*}" -aoa -bd -bso0
            #######################
            # -aoa   == Overwrite output files       #
            # -bd     == Don't show progress bar  #
            # -bso0 == Silent                                    #
            #######################
            rm -f "$anotherFile" # DO NOT COMMENT THIS LINE.
            rm -f "$file" # Comment this if you want to keep the archieves.
            recursiveExtract "${anotherFile%.*}"
        elif [ -f "$file" -a "${file##*.}" = 'gz' ]; then
            local anotherFile="${file%.*}"
            if [ -n "$anotherFile" -a \( "${anotherFile##*.}" = 'tar' -o "${anotherFile##*.}" = 'img' \) ]; then # file.tar.gz, file.img.gz
                echo 'Extracting: '"$file"
                7z x "$file" -o"${file%/*}" -aoa -bd -bso0
                7z x "$anotherFile" -o"${anotherFile%.*}" -aoa -bd -bso0
                rm -f "$anotherFile" # DO NOT COMMENT THIS LINE.
                rm -f "$file" # Comment this if you want to keep the archieves.
                recursiveExtract "${anotherFile%.*}"
            else # gz
                echo 'Extracting: '"$file"
                7z x "$file" -o"${file%.*}" -aoa -bd -bso0
                rm -f "$file" # Comment this if you want to keep the archieves.
                recursiveExtract "{file%.*}"
            fi
        elif [ -f "$file" -a \( "${file##*.}" = 'img' -o "${file##*.}" = 'tar' \) ]; then # file.img or file.tar
            echo 'Extracting: '"$file"
            7z x "$file" -o"${file%.*}" -aoa -bd -bso0
            rm -f "$file" # Comment this if you want to keep the archieves.
            recursiveExtract "${file%.*}"
        fi
    done
}
recursiveExtract "$1"

Give the script executable permission with chmod +x recursiveExtract.bash then you can run it like:使用chmod +x recursiveExtract.bash授予脚本可执行权限,然后您可以像这样运行它:

$ ./recursiveExtract.bash <directory/file>

In your case, maybe like this:在你的情况下,可能是这样的:

$ ./recursiveExtract.bash 'sample.img.gz'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM