Search code examples
androidlinuxbashterminalbusybox

How to exclude folders in DU and TAR commands


I use command like this: tar cf - /folder-with-big-files -P | pv -s $(du -sb /folder-with-big-files | awk '{print $1}') > big-files.tar.gz from THIS site. It works very good but I need to exclude 2 folders in TAR and DU command and I have a problem with this. Perhaps there is a probelm with the order?

I tried this:

tar cf --exclude={"/data/media/0/!Temp/!Test/bellota","/data/media/0/!Temp/!Test/Xiaomi"} "$mainLocation" - "$mainLocation" -P | pv -s $(du -sb --exclude={"/data/media/0/!Temp/!Test/bellota","/data/media/0/!Temp/!Test/Xiaomi"} "$mainLocation" "$mainLocation" | awk '{print $1}') > "$tarArchiveLocationWithName"

but this command doesn't works.

My primary code from script:

#!/bin/bash

mainLocation='/data/media/0/!Temp/!Test'
tarArchiveLocationWithName="/data/media/0/!Temp/!backup-$(date +%Y-%m-%d_%H-%M-%S).tar"

tar cf - "$mainLocation" -P | pv -s $(du -sb "$mainLocation" | awk '{print $1}') > "$tarArchiveLocationWithName"

Solution

  • With GNU tar and du from GNU coreutils with absolute paths:

    #!/bin/bash
    
    mainLocation='/data/media/0/!Temp/!Test'
    ex1='/data/media/0/!Temp/!Test/bellota'
    ex2='/data/media/0/!Temp/!Test/Xiaomi'
    tarArchiveLocationWithName='/tmp/big-files.tar'
    
    # get size in bytes without excludes
    size=$(du -sb --exclude="$ex1" --exclude="$ex2" "$mainLocation" | awk '$0=$1')
    
    # create tar without excludes
    tar -C / --exclude="$ex1" --exclude="$ex2" -c -P "$mainLocation" | pv -s "$size" > "$tarArchiveLocationWithName"