1

Solaris 11

I want to find all files greater that 31 days, tar them and compress the tar file.

I'd like to do this on one line. I'd rather use pbzip2 than gzip!!!

Here is what I tried as a prototype: find /tmp -mtime +31 -type f -name "arch*" -exec sh -c 'tar -czvPfE /tmp/old_files_$(date +%F).tar.gz $0' {} \;

Problem with this is it creates a tar.gz for each file over-writing the file before it (I want all the files in only one tar ball). The "r" doesn't work unless the tar exists. And the "z" calls gzip not pbzip2

UPDATE: @DopeGhoti Thank you. First, my bad for not checking the man page for the "j" option. However, this almost worked. root@myhost:/tmp# find /tmp -mtime +31 -type f -name "arch*"| wc -l =207 root@myhost:/tmp# find /tmp -mtime +31 -type f -name "arch*"| xargs tar -cjf /tmp/old_files_$(date +%F).tbz root@myhost:/tmp# l|tail -rw------- 1 root root 1.6K Nov 30 15:59 old_files_2017-11-30.tbz root@myhost:/tmp# bunzip2 old_files_2017-11-30.tbz root@myhost:/tmp# tar tf old_files_2017-11-30.tar|wc -l =15

  • Yes, I could do this line by line in a script, but I was hoping to make an alias that I can run anytime I am under space pressure on the system. – Marinaio Nov 30 '17 at 15:47
  • find [...] | xargs tar -cjf "/path/to/tarball-$(date +%F).tbz"? – DopeGhoti Nov 30 '17 at 15:51
1

Assuming the file names don't contain newline characters, POSIXly (except for pbzip2 obviously), which takes the list of files to archive on stdin by default (and also writes the archive on stdout by default)

find /tmp -mtime +31 -type f -name "arch*" | pax -w | pbzip2 > file.tar.bz2

Or to use Solaris tar so you can keep your P/E options:

find /tmp -mtime +31 -type f -name "arch*" |
  tar cvPEf - -I /dev/stdin | pbzip2 > file.tar.bz2

-I /dev/stdin to take the file list from stdin, f - to output the archive on stdout, which is then compressed by pbzip2 like with pax.

  • Mind explaining the mechanism behind these two? "pax -w" and "tar cvPEf - -I /dev/stdin" Just curious how this works... – Marinaio Nov 30 '17 at 16:32
  • @Marinaio, see edit. – Stéphane Chazelas Nov 30 '17 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.