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.
Maybe:
I prepended
unrar
withecho
so that it's possible to examine what command is going to do. For example, for the directory structure like this:It would print: