tar cpf for the most recent file in directory with progress

1.2k Views Asked by At

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.

2

There are 2 best solutions below

3
On BEST ANSWER

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
2
On

You can pipe it:

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

and then, create a compressed format

gzip recent.tar

or an older package extension

gzip -c recent.tar > recent.tgz

Please note that -r option in tar is used to append file in your package