2

I'm actually working on a Windows OS for development purposes. I have some config files which i want to deploy on a linux based system. The structure of the zip generated on Windows is the same as the target.

When i unzip my archive in the linux system, the unzip command change the owner of original files with the user who make the unzip (root).

Before unzip :

[root@supermachine /]# ll /myRootFolder -R
/myRootFolder:
total 4
-rw-r--r-- 1 superman superman  5 21 août  10:18 file.TXT
drwxr-xr-x 2 superman superman 25 21 août  10:17 mySubFolder

/myRootFolder/mySubFolder:
total 4
-rw-r--r-- 1 superman superman 4 21 août  10:17 subFile.TXT

After unzip

[root@supermachine /]# unzip myRootFolder.zip
Archive:  myRootFolder.zip
replace myRootFolder/file.TXT? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: myRootFolder/file.TXT
replace myRootFolder/mySubFolder/subFile.TXT? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: myRootFolder/mySubFolder/subFile.TXT

[root@supermachine /]# ll /myRootFolder -R
/myRootFolder:
total 4
-rw-r--r-- 1 root     root      5 21 août  10:18 file.TXT
drwxr-xr-x 2 superman superman 25 21 août  10:21 mySubFolder

/myRootFolder/mySubFolder:
total 4
-rw-r--r-- 1 root root 4 21 août  10:17 subFile.TXT

For information, unzip version is :

[root@supermachine /]$ unzip -v
UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Latest sources and executables are at ftp://ftp.info-zip.org/pub/infozip/ ;
see ftp://ftp.info-zip.org/pub/infozip/UnZip.html for other sites.

Compiled with gcc 4.8.5 20150623 (Red Hat 4.8.5-26) for Unix (Linux ELF) on Jan 10 2018.

UnZip special compilation options:
        COPYRIGHT_CLEAN (PKZIP 0.9x unreducing method not supported)
        SET_DIR_ATTRIB
        SYMLINKS (symbolic links supported, if RTL and file system permit)
        TIMESTAMP
        UNIXBACKUP
        USE_EF_UT_TIME
        USE_UNSHRINK (PKZIP/Zip 1.x unshrinking method supported)
        USE_DEFLATE64 (PKZIP 4.x Deflate64(tm) supported)
        UNICODE_SUPPORT [wide-chars, char coding: UTF-8] (handle UTF-8 paths)
        MBCS-support (multibyte character support, MB_CUR_MAX = 6)
        LARGE_FILE_SUPPORT (large files over 2 GiB supported)
        ZIP64_SUPPORT (archives using Zip64 for large files supported)
        USE_BZIP2 (PKZIP 4.6+, using bzip2 lib version 1.0.6, 6-Sept-2010)
        VMS_TEXT_CONV
        [decryption, version 2.11 of 05 Jan 2007]

UnZip and ZipInfo environment options:
           UNZIP:  [none]
        UNZIPOPT:  [none]
         ZIPINFO:  [none]
      ZIPINFOOPT:  [none]

Is there an option on zip command to preserve original owner of crushed files ? I read the manual but i see anything related to these case.

Note : If there is a solution with other kind of archive, it can also resolve my issue (i can generate other archive formats).

6
  • How should a filesystem like EXT4 for example deal with NTFS permissions in this case? This won't work because of the differences between Linux and Windows.
    – Panki
    Aug 21, 2019 at 8:49
  • 1
    I don't think that zip stores ownership details. If there is only one owner, then you can change owner before unzipping. Aug 21, 2019 at 8:50
  • Top tip: If you can stop using Microsoft's Windows. If you can't then set up a Gnu/Linux in virtual box. Aug 21, 2019 at 8:51
  • 1
    tar will preserve ownership
    – muru
    Aug 21, 2019 at 9:17
  • Which version of unzip are you using? Please include the output of unzip --version. (I think the answer will still be "no, there's no such option", but can only be sure by systematically going through every option supported by that exact version.)
    – JigglyNaga
    Aug 21, 2019 at 13:33

2 Answers 2

4

You could try the su command to run the unzip as the user that you want to own the files:

su -c "unzip myRootFolder.zip" superman 

su allows you to run a command as another user, or temporarily become that user within the current login session. Run

man su

for a complete description of the command.

0

unzip does not have a function to do precisely what you're describing.

When it asked you

replace myRootFolder/file.TXT? [y]es, [n]o, [A]ll, [N]one, [r]ename:

and you responded y, "replace" meant that it deleted the original file before extracting the new copy. The original file is gone, including all its metadata (owner, permissions, timestamps).

Using backup files

An option that does part of what you need would be the -B option, to keep a backup of all overwritten files. After unzip -B, you should see (in the ll / ls output) that as well as the update file, there's the original file with a ~ at the end of its name:

-rw-r--r-- 1 superman superman  5 21 août  10:18 file.TXT~
-rw-r--r-- 1 root     root      5 21 août  10:18 file.TXT

From there, you need to copy each backup file's ownership to the new file with chown, then remove the backup files afterwards.

You could do this with eg. find -exec:

find myRootFolder -name "*~" -exec bash -c 'bak="{}";
  new="${bak%\~}";
  chown --reference "$bak" "$new" ; rm "$bak"' \;

but this will cause problems if you already had filenames ending with ~, before any backups were created.

Alternatively, you could list the zip contents (using the zipinfo command included with Info-ZIP), and for each one, check whether a backup even exists before doing the chown...rm.

zipinfo -1 myRootFolder.zip | grep -v '$/' | while read new ; do
  bak="${new}~"
  test -e "$bak" || continue
  chown --reference "$bak" "$new"
  rm "$bak"
done
3
  • This is going to give bad results if some of the files (on the file system, or in the zip) already have a "~" at the end. There are probably other edge cases I've missed, too.
    – JigglyNaga
    Aug 21, 2019 at 15:31
  • Thanks for our response @JigglyNaga. I think your solution is very close to what I want. We can execute a script shell after unziping to make chown command only on files which have a backup in the same directory. A cleaner solution is also with a shell script which list files in the zip archive and make the chown command only on files extracted from this archive.
    – Florian G
    Aug 23, 2019 at 6:04
  • @FlorianG Edited to add a script that does exactly that.
    – JigglyNaga
    Aug 23, 2019 at 9:34

You must log in to answer this question.

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