Say, if I want to copy /etc/bin to a folder while maintaining the file attributes as well as parent directories. [The target folder should not just contain bin folder but /etc/bin.]

Is the possible to do this? Do zipping and unzipping using tar works?

  • tar always work. – David Dai Nov 23 '16 at 2:45
  • What about ownership? – Rinato Fenice Nov 23 '16 at 3:31

man tar:

   -P, --absolute-names
          Don't strip leading slashes from file names  when  creating  ar‐

   -p, --preserve-permissions, --same-permissions
          extract information about file permissions  (default  for  supe‐

          Enable extended attributes support.
mkdir target/etc
cp -Rp /etc/bin target/etc/

or (GNU only)

mkdir target/etc
cp -a /etc/bin target/etc/


mkdir target/etc
rsync -aAX /etc/bin target/etc/

or (classic unix)

find /etc/bin | cpio -pm target

or (classic unix, the other )

( cd / && tar -tf - etc/bin ) | (cd target && tar -xf - )

or (the POSIX replacement that few people seem to have heard of)

mkdir target/etc
pax -rw -pe etc/bin target/etc/

All of these preserve traditional Unix metadata (permissions, timestamps¹, ownership if run as root². Depending on your system, they may or may not preserve additional metadata such as ACL and extended attributes (some of them may require additional options, e.g. --acls --xattrs with GNU tar).

¹ Except for the ctime which can't be copied, and for directory atimes for some methods. ² Only root can preserve ownership.

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.