1

Using Raspbian on a Raspberry Pi, I want to back up the /etc files, but not all of them. I'd like to exclude /etc/alternatives, so I used this command:

sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* –x /etc/alternatives/*

Why doesn't this work? It proceeds to include the contents of /etc/alternatives just like all the other folders in /etc.

I tried these variations, and they didn't work either

sudo zip -r –x /etc/alternatives/* @ /home/pi/backup/$timestamp/etcfilesall.zip /etc/*
sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* –x /etc/alternatives
sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* –x '/etc/alternatives'
sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* –x '/etc/alternatives/*'

Under the -r option for recursion, the zip -h2 help says "Use -i and -x with either to include or exclude paths", so it seems like what I'm trying to do should be supported.

2 Answers 2

3

The problem is that the "-" before the "x" (for the option "-x") is not really an ASCII minus sign in OP's question. Instead, it is the Unicode u+2013 (looks the same, but does not act the same).

If the command is typed in as expected, it works as expected for OP's last example (corrected here):

sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* -x '/etc/alternatives/*'

Likely this was a problem resulting from cut/paste from some website.

Further reading:

2
  • That was it, Thomas. Really good catch. I just deleted the character that was before the x and re-typed the normal ASCII minus sign. Now it works. In the font that putty uses, they all look the same. I wish putty and/or nano would highlight non-ascii characters! Maybe a different color or reverse font?
    – tim11g
    Feb 13, 2016 at 20:12
  • Perhaps. In vile, I moved my cursor to the suspect and pressed ^G to see its value. Feb 13, 2016 at 20:16
0

From man zip page:

zip -r foo foo -x \*.o

which will include the contents of foo in foo.zip while excluding all the files that end in .o. The backslash avoids the shell filename substitution, so that the name matching is performed by zip at all directory levels.

You have to make sure that your glob * is expanded not by shell, but by zip utility itself, otherwise it will only affect files directly in /etc/alternatives directory and won't target subdirectories. This can be done by adding backslash in front of * or quoting it:

sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip /etc/* –x /etc/alternatives/\*

However, it seems that you have already tried a similar way - it is not clear to me why it didn't work for you, while it should. Still, there are other ways to achieve similar effect, like using additional tools to exclude files you do not want before you call zip utility:

sudo zip -r /home/pi/backup/$timestamp/etcfilesall.zip $(sudo find /etc | grep -v ^/etc/alternatives)
2
  • OP did quote the expression (a good answer would point out why he was unsuccessful). Feb 13, 2016 at 19:22
  • You are right. Still, for your answer to be complete, you probably should mention that the the last command OP tried is the correct one, since argument to '-x' still have to be escaped. Feb 13, 2016 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .