2

嗨,我正在编写一个脚本,该脚本使用 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-,但这给了我相同的结果,将内容提取到运行脚本的目录。

4

2 回答 2

0

也许:

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

我添加了前缀unrarecho以便可以检查命令将要执行的操作。例如,对于这样的目录结构:

$ 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
于 2018-09-23T15:42:11.927 回答
0

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

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

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

于 2019-08-28T08:17:23.427 回答