4

I would like to archive all files (to one .tar.gz file) in a directory when they are older than X days.

I have this one liner:

find /home/xml/ -maxdepth 1 -mtime +14 -type f -exec sh -c \ 'tar -czvPf /home/xml/archive/archive_$(date +%F).tar.gz $0' {} \;

When I run this command, I see correct files selected in this directory, but in the archive is only the last file. Is there any way to get all files into one tar.gz archive?

One more problem after @Alex answer: still many files are missing, check the screenshot.

enter image description here Maybe the colons (:) are causing the problem?

Adrian
  • 2,576
  • 9
  • 49
  • 97

1 Answers1

6

-exec runs the command for each file selected, so it's writing a tar with one file in it and then overwriting it for every source file, which explains why you're only getting the last one. You can use find to generate the list of files you want and then pipe that through xargs to pass the list as if they were parameters to your tar command:

find /home/xml/ -maxdepth 1 -mtime +14 -type f | xargs tar -czvPf /home/xml/archive/archive_$(date +%F).tar.gz

File names with colons work fine for me:

% dd if=/dev/urandom of=one:1 count=1
% dd if=/dev/urandom of=two:2 count=1
% dd if=/dev/urandom of=three:3 count=1
% dd if=/dev/urandom of=four:4 count=1
% dd if=/dev/urandom of=five:5 count=1
% find . -type f | xargs tar cvf foo.tar
    ./five:5
    ./four:4
    ./two:2
    ./three:3
    ./one:1
% tar tvf foo.tar
    -rw------- alex/alex       512 2017-07-03 21:08 ./five:5
    -rw------- alex/alex       512 2017-07-03 21:08 ./four:4
    -rw------- alex/alex       512 2017-07-03 21:08 ./two:2
    -rw------- alex/alex       512 2017-07-03 21:08 ./three:3
    -rw------- alex/alex       512 2017-07-03 21:08 ./one:1
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Nice find, thank you! Works like a charm. – Adrian Jul 03 '17 at 18:53
  • I thought that it's ok, but it's not. Could you please check the edited question? – Adrian Jul 03 '17 at 19:26
  • Run the `find` command by itself and make sure the files you want are selected. Then put the `v` option on your `tar` command when creating the file and make sure the files you want are listed as they're going into the tar. Then run `tar tv` to list the contents when you're done. What step are the files missing from? – Alex Howansky Jul 03 '17 at 21:15
  • While I run tar -czvPf, I see all files are listed when it's creating the archive. One very interesting thing: ~/home/xml/$ find ./ ---> working ~/home/xml$ find /home/xml/ ---> not working. – Adrian Jul 03 '17 at 22:10
  • Ok, some hours later I've found the problem. I think I reached the upper limit of the xargs maximum command length. I added xargs -s 2092413 and now it seem to be work fine. – Adrian Jul 03 '17 at 22:53