2

Hi I am working on a script that syncs content from a remote site using SFTP, then extracts any archives. However I am struggling to get the extracted files to the correct (source) directory.

I started with a for loop, but was thinking this could be more efficient.

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

Requirements:

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

Here is my current command. In the loop I used I specifically called out the destination, but am not sure how to do that here in one line.

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

I have also tried unrar xe -o- but that gives me the same result where the contents are extracted to the directory where the script was ran from.

nitrobass24
  • 371
  • 2
  • 8
  • 21

2 Answers2

0

Maybe:

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

I prepended unrar with echo so that it's possible to examine what command is going to do. For example, for the directory structure like this:

$ tree
.
├── a.rar
├── b
│   └── z.rar
└── b.rar

1 directory, 3 files

It would print:

$ 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
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0

Basically in your example "-exec" should be "-execdir" (unrar switches not tested)

Executed from the folder above sync: folderAboveSync$ find -name '*.rar' -execdir unrar e {} \;

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

TheArchitecta
  • 273
  • 2
  • 17