linux - 目录和所有子目录中的递归解压缩和删除

标签 linux bash shell ubuntu unrar

我正在尝试编写一个脚本,该脚本将抓取我的 Plex 媒体文件夹,找到任何 header “.r00”文件,将它们解压缩到它们自己的目录中,并在完成后将存档 zips 丢弃。我有两个选择,我一直在玩。将它们结合起来可以做我想做的事,但我想将所有这些都放在一个漂亮的小脚本中。

选项 1:

  • 此脚本打开“LinRAR”GUI,让我导航到特定目录,查找并提取该目录中的任何 .r00 文件,并成功删除所有存档 zip。

    while true; do
    if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
        if [[ ! -d $dir ]]; then
    echo "$dir: Wrong Directory" >&2
    else
    ( cd "$dir" && for f in *.r00; do [[ -f $f ]] || continue; rar e "$f" && rm "${f%00}"[0-9][0-9]; done )
    fi
    else
    echo "$bold Selection cancelled $bold_off" >&2
        exit 1
    fi
    zenity --title="What else...?" --question --text="More work to be done?" || break
    done
    

选项 2:

  • 此脚本 cd 到我的 Plex 文件夹,递归地查找任何 .r00 文件,提取到我的/home/user 文件夹,并且不删除存档 zip。

    (cd '/home/user/Plex');
     while [ "`find . -type f -name '*.r00' | wc -l`" -gt 0 ]; 
          do find -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;; 
    done
    

我想要一些东西,它采用第一个工作脚本,并将递归查找应用于/Plex 内的所有文件夹,而不是让我通过“LinRAR”GUI 一次只导航到一个文件夹。

最佳答案

无需使用cdfind 获取起始目录。
就是您传递给它的那个点 (.)。

还为查找和循环添加了另一个(更理智的)替代方案:

#!/bin/bash

while true; do
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong Directory" >&2
    else
        # Alternative 1 - a little more comfortable
        files="$(find "${dir}" -type f -name '*.r00')"
        for file in ${files}; do
            rar e "${file}" && rm "${file}"
        done

        # Alternative 2 - based on your original code
        while [ "`find "${dir}" -type f -name '*.r00' | wc -l`" -gt 0 ]; do
            find "${dir}" -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;;
        done
    fi
else
    echo "$bold Selection cancelled $bold_off" >&2
    exit 1
fi
zenity --title="What else...?" --question --text="More work to be done?" || break
done

根据评论,我运行了这段代码的一个小例子,它运行得非常好:

#!/bin/bash

if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong directory" >&2
    else
        find $dir -type f
    fi
else
    echo "cancelled"
fi

一个目录被成功选取并且它的所有文件都被打印出来。如果我在 zenity 中选择取消,那么它会打印“已取消”。

关于linux - 目录和所有子目录中的递归解压缩和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29731644/

相关文章:

linux - 如何计算 Bash 中字符串中的单词数

shell - 如何使用Pig/Hive从Weblog文件中的URL中提取字符串

regex - sed 编辑文件中的一行

android - 没有shell cp命令复制系统文件

linux - 使用文件查找文件夹位置 Linux Bash

linux - 由 :Ambari Upgradation java. sql.SQLSyntaxErrorException: Unknown table 'hostcomponentstate' in information_schema 引起

c++ - 适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现是否可行?

linux - 删除包含多个单词的行

c++ - 导入文件存在到项目 eclipse C++ Linux

Linux:从 XML 文件中删除具有从第 2 次出现开始的匹配字符串的行的命令