-1

I want to make a script that can extract rar files recursively and append an extension to the extracting files.

The extension should be added during the process (so that other software doesn't see a recognised file extension and start its process until all files are extracted). Once all files are completed the extension should be removed.

Here is an example file structure...

/some/path/
    folder1/
        folder2/
            file1.rar
        folder3/
            file2.rar
            file3.rar
            folder4/
                file4.rar

I want this to turn to this...

/some/path/
    folder1/
        folder2/
            file1.rar
            file1.txt.extracting
        folder3/
            file2.rar
            file2.txt.extracting
            file3.rar
            file3.txt.extracting
            folder4/
                file4.rar
                file4.txt.extracting

Then once all are complete, to this...

/some/path/
    folder1/
        folder2/
            file1.rar
            file1.txt
        folder3/
            file2.rar
            file2.txt
            file3.rar
            file3.txt
            folder4/
                file4.rar
                file4.txt

I hope that makes sense. Is this possible?

Chris
  • 985
  • 2
  • 7
  • 17
  • Please add unique names to files and dirs so we can distinguish. – tshiono Jan 31 '20 at 09:56
  • 2
    Concerning the files names : I don't see any option in unrar that would help unpackage the files with a transformed name. You mention software that would process the files, do they process them wherever they are or only in specific paths? Because an easy solution would be to unpack your files in a temporary directory before moving them to their expected destination – Aaron Jan 31 '20 at 10:09
  • The software monitors a directory so a temp location could also work – Chris Jan 31 '20 at 10:11
  • Please add your attempt at doing this – tomgalpin Jan 31 '20 at 10:26

1 Answers1

0

The following should work :

cd $(mktemp -d)
find /some/path/ -name '*.rar' \
   -exec unrar e {} \; \
   -exec bash -c 'mv * $(dirname {})' \;
# if relevant, cd - to get back to the previous directory 

This will iterate over all the .rar files in /some/path and its children directories, extracting them in a temporary directory before copying the extracted content to the .rar file's directory.

Executing bash -c instead of mv directly is required for the subshell to be interpreted after the {} gets replaced by find.

Aaron
  • 24,009
  • 2
  • 33
  • 57