Search code examples
bashprogress-bartargnu-findutils

tar cpf for the most recent file in directory with progress


I'm looking for the easiest way to tar the most recent file in directory. The command below locates the correct file, but I don't know how to tar it from the output:

find /home/user -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "

And also I must output the progress.


Solution

  • To create a tar file with GNU tar append:

    | xargs tar --checkpoint=1024 --checkpoint-action=ttyout='%u KB approximately written\r' -cf file.tar
    

    Update: GNU tar with progress bar (with pv):

    find /home/user -type f -printf '%T@ %s %p\n' | sort -n | tail -1 | while read t s p; do tar -cf - "$p" | pv -s "$s" > file.tar; done
    

    Output (example):

    400MB 0:00:22 [74.2MB/s] [========================>          ] 77% ETA 0:00:15