2

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
faceless
  • 450
  • 4
  • 15

2 Answers2

1

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Dear sir, Your solution works for me. Thanks for that. However, i have 2 additional questions: 1- command execution prints the following message:tar: Removing leading `/' from member names. What is the meaning of the message? Does it remove the path of the file in some way? 2- Where i can learn more about the syntax of your command? – faceless May 24 '17 at 19:25
  • 1: [By default, GNU tar drops a leading `/' on input or output. There is an option that turns off this behavior.](https://www.gnu.org/software/tar/manual/html_node/absolute.html) 2: [More about Checkpoints](https://www.gnu.org/software/tar/manual/html_section/tar_26.html#checkpoints) – Cyrus May 24 '17 at 19:33
  • Thanks, max appreciated!! And finally do you happen to have an idea of how to add progress bar to this process? – faceless May 24 '17 at 19:43
0

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

Unwastable
  • 629
  • 4
  • 5
  • Hello,Thanks a lot for your assistance. The pipe command seems to work, but it creates 10Kb file, which, of course, cannot be used. How do i create the tar for the entire file? – faceless May 24 '17 at 19:14
  • This will tar the entire file, this command: `tar -rf` has no instruction to split the file. – Unwastable May 24 '17 at 19:57