0

I am trying to make a tar archive of all files in my /var directory. Naturally, I want to exclude the Unix sockets in there.

So I try a command that looks something like:

sudo find /var \! -type s -exec tar -rf ~/test/etc.tar {} \;

But this throws up the repeated error message

tar format cannot archive socket:

When I try to do a copy instead of tar, I get a similar error:

cp: ./var/run/devd.pipe is a socket (not copied).

Does anyone have an idea why find fails to exclude files of type socket? It is able to exclude, for instance, files of type directory..

  • TAR implementations typically do not try to achive file types they cannot archive with the selected archive format. So what is your problem? – schily Mar 21 '19 at 14:31
2

You will have to exclude directories as well. If not excluding directories, tar will get /var (and other directories) as argument, and will proceed to archive these with all contents (sockets or not). It will additionally add multiple copies of many of the files due to the same reason (/var includes /var/log/messages and so does /var/log).

find /var ! -type s ! -type d -exec tar -r -f archive.tar {} +

Or, if you just want regular files and symbolic links:

find /var '(' -type f -o -type l ')' -exec tar -r -f archive.tar {} +

I've chosen to use -exec ... {} + here instead of -exec ... {} ';' as we probably want to call tar as few times as possible with as many arguments as possible at a time.

  • Thanks for explaining it and the extra tip. Two followup questions: (why) don't you need to escape the exclamation marks? What does the option -o do? I don't seem to have it in my (BSD) man pages but only in the examples... – Yogesch Nov 26 '18 at 11:39
  • 1
    @Yogesch If you use a sh shell (like bash or ksh), then a lone ! is not special in any way and does not need to be quoted. The -o option means "OR" (-a, "AND", is the default between two tests). The find manual will mention these operators for sure (it does on OpenBSD). – Kusalananda Nov 26 '18 at 11:43
  • Okay. It isn't mentioned in the FreeBSD man pages but, I checked the again and there is an operators section which has -and and -or, not the abbreviated forms. Same thing I guess. Thanks a lot for helping out :-) – Yogesch Nov 26 '18 at 11:48
  • 1
    @Yogesch The equivalence is mentioned in the "STANDARDS" section of the FreeBSD manual. The short variant of these operators is the standard one. – Kusalananda Nov 26 '18 at 11:53
  • Yes, indeed it is. Never knew there was still useful information after the examples. should read the man page till the end... – Yogesch Nov 26 '18 at 11:58

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.