3

What is the difference between -r and -R in zip command.

Obviously, I have googled about it.

Also, I have referred this in quest of finding the difference, but didn't got clarification.

Can anyone from community help me for this?

3
  • 4
    "Obviously, I have googled about it." and what you found?
    – Braiam
    Aug 4, 2014 at 13:21
  • 1
    zip is ported from DOS, so it has its own wildcard substitution implementation. -r is full recurse, including .. if that would be matched. -R appears to be a less brain-dead option doing the same but starting from the current directory and never ascending.
    – MattBianco
    Aug 4, 2014 at 13:39
  • 1
    @Braiam: Do you want me to give you links of all the results i have got when googled. Nobody has time to ask the question just for fun. This is SE - Unix & Linux, not funny blogs or facebook and i know that for sure. Aug 5, 2014 at 4:09

3 Answers 3

9

From what is likely to be in your man pages:

-r
--recurse-paths 
Travel the directory structure recursively

-R
--recurse-patterns 
Travel the directory structure recursively starting at the current directory

Loosely speaking, zip -r is used when you want to zip files under a specific directory, and zip -R when you want to zip files under a specific directory and where those files match a pattern defined after the -R flag, as you can see in the examples provided in that page. Also, -R starts in the current directory by default.


Examples:

zip -r foo foo1 foo2
First zips up foo1 and then foo2, going down each directory.

zip -R foo "*.c"
In this case, all the files matching *.c in the tree starting at the current 
directory are stored into a zip archive named foo.zip. Note that *.c will 
match file.c, a/file.c and a/b/.c. More than one pattern can be listed as 
separate arguments.
2
  • -R has nothing to do with how the files are stored inside the zip, but with what files are.
    – Braiam
    Aug 4, 2014 at 13:51
  • @jim: Thanks a lot jim. This is exactly what i was looking for. I am novice to Linux. So, your clear examples helped me lot to understand the difference between two. Aug 5, 2014 at 4:17
5

Both are recursive, that much you should know, but -R works with patterns instead of whole trees. For example:

zip -R music "*.mp3"

It will from the current directory match all files ending with .mp3 and zip them maintaining the structure:

➜  src  zip -R amr "*.css"
  adding: AMR/css/jquery.treeview.css (deflated 76%)
  adding: AMR/css/importexport.css (deflated 43%)
  adding: AMR/css/amr_style.css (deflated 82%)
  adding: AMR/css/backsite.css (deflated 49%)
➜  AMR git:(develop) ✗ zip -R amr "*.css"
  adding: css/jquery.treeview.css (deflated 76%)
  adding: css/importexport.css (deflated 43%)
  adding: css/amr_style.css (deflated 82%)
  adding: css/backsite.css (deflated 49%)

-r allows this if you use it in conjunction of -i or -x.

1
  • It would have been great if you have provided examples of both to point out the difference between the two. I am novice for Linux. Aug 5, 2014 at 4:14
4

zip -r expects a path (a filename works, but defeats the point of adding the -r)

zip -R expects a pattern.

For example, zip -r stuff.zip stuff* will recursively compress all directories whose name begins with stuff, starting from the current directory. The * is expanded by the shell to all file/directories that begin with stuff. If you were to quote the * then zip would simply look for a file/directory called stuff* and compress that file/folder if it existed.

On the other hand, zip -R stuff.zip "stuff*" will compress all files under the current directory whose names begin with stuff. Note that the pattern is quoted so that the shell doesn't expand it. If you were to remove the quotes, the shell would expand it before it got to zip and therefore zip would try to find and compress a file called stuff.

If you have a directory called test in the current working directory that has a file within it called stuff.txt then this will be added to the zip in the second example as the filename matches the pattern regardless of the name of the containing directory. The file would not be picked up by the first example as it only compresses directories that begin with stuff.

You must log in to answer this question.

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