Search code examples
bashfor-looprar

Bash - Archive multiple subfolders from mounted fs to local folder


I thought my idea was easy but since i've spend 4h now, I thought I could need help.

I want to create rar-archives from a music library that is stored on a mounted fs, with each archive having the subfolders name out of the library but I want the archives being directly created on the local system.

Both of the following solutions leaded to created archives in the library but not on my local fs.

 for folder in /mnt/storage01/lib/audiobook/entertainment*/; do rar a -m5 -v100m -r "${folder%/}.rar" "$folder"; done

or

find /mnt/storage01/lib/audiobook/entertainment/. -mindepth 1 -maxdepth 1 -exec rar a -m5 -v100m -r -w {}.rar {} \;

adding

 ... -v100m -r -w "../home/{}.rar" {} \;     

at the end unfortunately leaded just to one archive named "home.rar" where all subfolders got collected. At least in my local fs..

What am I overseeing?


Solution

  • Use find with read/while :

        # -- sample code (replace echo output with actual command) --
        save_folder="/tmp/"
        find /tmp/testing/ -maxdepth 1  -type d | while read fpath; do
            echo "rar a -m5 -v100m -r \"${save_folder}$(basename ${fpath%.*}).rar\" \"$fpath\""     
        done
    

    output (just echoing) :

    rar a -m5 -v100m -r "/tmp/14.rar" "/tmp/testing/14"
    rar a -m5 -v100m -r "/tmp/80.rar" "/tmp/testing/80"
    rar a -m5 -v100m -r "/tmp/13.rar" "/tmp/testing/13"
    rar a -m5 -v100m -r "/tmp/16.rar" "/tmp/testing/16"
    rar a -m5 -v100m -r "/tmp/2.rar" "/tmp/testing/2"
    rar a -m5 -v100m -r "/tmp/100.rar" "/tmp/testing/100"
    rar a -m5 -v100m -r "/tmp/17.rar" "/tmp/testing/17"