【问题标题】:Find and Unrar all files查找并解压缩所有文件
【发布时间】:2018-09-23 15:10:04
【问题描述】:

您好,我正在编写一个脚本,该脚本使用 SFTP 从远程站点同步内容,然后提取所有档案。但是,我正在努力将提取的文件放到正确的(源)目录中。

我从 for 循环开始,但认为这可能更有效。

list=`find $local_dir -type f -name *.rar`
for line in $list; do
DEST=${line%/*}
unrar x -o- $line $DEST
done

要求:

1. Find all RAR files
2. Extract all rar files to the directory each rar is located

/sync/testfile.rar --> /sync/testfile.file
/sync/testdir/testfile1.rar --> sync/testdir/testfile1.file
/sync/testdir/testfile2.rar --> sync/testdir/testfile2.file

这是我当前的命令。在我使用的循环中,我专门调用了目的地,但不知道如何在一行中做到这一点。

find $local_dir -type f -name '*.rar' -exec unrar x -o- {} \;

我也尝试过unrar xe -o-,但这给了我相同的结果,将内容提取到运行脚本的目录。

【问题讨论】:

    标签: bash find exec unrar


    【解决方案1】:

    也许:

    find $local_dir -type f -name '*.rar' -exec sh -c 'echo unrar x -o- {} $(dirname {})' \;
    

    我在unrar 前面加上了echo,这样就可以检查什么 命令是要做的。例如,对于像这样的目录结构 这个:

    $ tree
    .
    ├── a.rar
    ├── b
    │   └── z.rar
    └── b.rar
    
    1 directory, 3 files
    

    它会打印:

    $ find $local_dir -type f -name '*.rar' -exec sh -c 'echo unrar x -o- "{}" "$(dirname {})"' \;
    unrar x -o- ./b.rar .
    unrar x -o- ./a.rar .
    unrar x -o- ./b/z.rar ./b
    

    【讨论】:

      【解决方案2】:

      基本上在您的示例中 “-exec” 应该是“-execdir”(未测试 unrar 开关)

      从上面同步的文件夹中执行: folderAboveSync$ find -name '*.rar' -execdir unrar e {} \;

      http://man7.org/linux/man-pages/man1/find.1.html

      【讨论】:

        猜你喜欢
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 1970-01-01
        • 2012-01-29
        • 2017-03-27
        相关资源
        最近更新 更多