Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I like zip, it's simple, but recently I came with the need of only zipping the file owned by one user in a directory.

I read the manual and I can't find any option to for example do things like:

zip /tmp/file.zip -own antoine -r /usr/something/* 

Does I miss the option or maybe it's possible with other archive standard format (I'm open to suggestion though I work on AIX and might miss some options or binary)

share|improve this question
    
If the filenames and directory names do not have spaces you can use the output of find -user antoine as arguments to zip. zip unfortunately does not have an option to read NUL separated filenames, and so doesn't handle files with spaces and newlines in the name very well if provided from another command. Alternative is to write a python script, it has a good zip module and there special filenames are easily handled. –  Anthon Mar 14 at 15:46
    
@anthon, I think that might feet my actual situation as no file get a space in there name –  Kiwy Mar 14 at 15:48

4 Answers 4

up vote 11 down vote accepted

You can try to find the relevant files with find:

find /usr/something -maxdepth 1 -user antoine

You can then use -exec to create a zip file from the results of find:

find /usr/something -maxdepth 1 -user antoine -exec zip /tmp/file.zip {} +

leave out the maxdepth if you want to recurse.

share|improve this answer
    
-own ? :D are you sure –  Kiwy Mar 14 at 15:53
    
@Kiwy trying to do two things at a time, updated –  Anthon Mar 14 at 15:55
    
well thanks it work but I will wait if anybody came with the posixy miraculous solution out of nowhere before accepting the answer :-) –  Kiwy Mar 14 at 15:57
    
@Kiwy afaik there is no POSIX complyant Python :p –  Anthon Mar 14 at 16:03
    
@Kiwy, There's no POSIX zip. The POSIX equivalent of -maxdepth 1 is \( -name . -o -prune \) though here chances are you want -mindepth 1 -maxdepth 1, that is ! -name . -prune. –  Stéphane Chazelas Mar 14 at 22:09

Using only features available on AIX or other POSIX-and-hardly-more systems:

find /usr/something/* /usr/something/.[!.]* /usr/something/..?* \
     -prune -type f -user antoine -exec zip -r /tmp/file.zip {} +

find recurses into subdirectories. To avoid that, -prune tells it to not descend into the directories it encounters. That's no good if I run find /usr/something -type d -prune -o … because then /usr/something would be skipped. So I run find on all the entries in that directory, except . and ...

An easier idiom is to switch to the directory and treat the starting directory . specially. Here, it would change the resulting archive, which would have paths of the form ./foo instead of usr/something/foo. This technique relies on the fact that -name . only matches the starting directory, every other encountered file will have its own name.

cd /usr/something &&
find . ! -name . -prune -type f -user antoine -exec …

Note that zip may be executed more than once if the list of file names is too long for a single invocation. This is ok because zip -r replaces existing files in the archive; if you use another archiver, take care not to invoke it in a mode where it would create a new archive (e.g. don't use tar -c).

share|improve this answer
    
You can do find /usr/something/. ! -name . -prune ... you don't need to cd into it or pass the whole file list to find using shell wildcards. –  Stéphane Chazelas Mar 17 at 9:30

With zsh:

zip mine.zip ./*(.U)

to zip my (regular) files.

zip antoine.zip ./*(.u:antoine:)

to zip antoine's

share|improve this answer
1  
If you don't use zsh, but you happen to have it installed (OS X provides a copy), you can use zsh -c 'zip ...'. –  Blacklight Shining Mar 14 at 23:38
    
no luck no zsh installed at all, thought the solution is very nice, i should take a look at this zsh thingy –  Kiwy Mar 17 at 9:01

How about

find /usr/something -maxdepth 1 -user antoine | zip /tmp/file.zip -@
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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