TIL tar keeps permissions of the files and directories archived if possible.

That was a "fun" debugging session...

124 points · 21 comments · view on lemmy.world

21 Comments

cmnybo@discuss.tchncs.de · 63 pts · 349d

That's what an archiving program is supposed to do.

Lemmchen@feddit.org · 34 pts · 349d (2 replies)

That's a feature, not a bug. It's an archive after all.

syklemil@discuss.tchncs.de · 11 pts · 349d

It's even a tape archiving tool. Just pretty much nobody uses it in the original way any more.

Very much one of those "if it ain't broke, don't replace it" tools.

lorty@lemmy.ml · 4 pts · 349d

I just assumed that whenever it would be expanded that the contents would have the default permissions for that user.

It's actually a cool feature I just feel dumb for how long it took me to realize this was the issue.

tetris11@lemmy.ml · 22 pts · 349d

Just bear in mind that uid 1001 on one machine is not generally uid 1001 on another, and that if you copy the tar off machine you're more than likely giving permission to somebody other than the intended target

vk6flab@lemmy.radio · 20 pts · 349d (1 reply)

You can even archive extended attributes with the ‘--xattrs flag.

MonkderVierte@lemmy.zip · 4 pts · 349d

Good to know if you use xattrs as a tagging system.

frongt@lemmy.zip · 19 pts · 349d (4 replies)

I've never heard of an archiving program that didn't.

slock@lemmy.world · 11 pts · 349d (2 replies)

IIRC zip can, but doesn't by default. Source : wasted 8 hours trying to figure out why something didn't work anymore (had to switch from tar to zip because stupid security system wouldn't let me transfer the tar file)

Zykino@programming.dev · 7 pts · 349d (1 reply)
tar -czf <folder>
mv folder.{tar.gz,zip}
InnerScientist@lemmy.world · 4 pts · 349d

Just use archive.pdf

lorty@lemmy.ml · 3 pts · 349d

I thought that, since users would be different between machines, there's no point in keeping this kind of information.

IsoKiero@sopuli.xyz · 18 pts · 349d (1 reply)

Since no one has yet mentioned, by default if you're running tar as a non-root user it extracts files with owner/umask of the current user and if you run it as root (or superuser) it'll preserve ownership and permissions. From tar man page:

--no-same-owner

Extract files as yourself (default for ordinary users).

--no-same-permissions

Apply the user's umask when extracting permissions from the archive (default for ordinary users).

As mentioned, with root the defaults are to keep UID/permissions as they are in the archive. (--preserve-permissions and --same-owner).

TheMadBeagle@lemmy.ml · 3 pts · 349d

Really good callout! Thanks for adding that

monovergent@lemmy.ml · 12 pts · 349d (1 reply)

Learned to make use of this the hard way when transferring a directory over a FAT32 USB drive messed up the permissions.

ki9@lemmy.gf4.pw · 2 pts · 348d

I uses to use 7z for my archives but it dropped the permissions. Now I tar it first into a .tar.7z aka .t7z when I want maximum compression.

HiddenLayer555@lemmy.ml · 10 pts · 348d

Worse, it preserves "special" files like the ones in /dev or /var which aren't removable by anyone other than root. Love extracting a system file backup in my file server as a regular user in order to get just a few files out of it, and promptly not being able to fully delete it afterward without SSHing into the server and using sudo.

I don't get how a regular user can even create files like that. Sounds like a security vulnerability.

daggermoon@lemmy.world · 4 pts · 349d (2 replies)

Yeah I did notice that. Never really had a use for such a feature though.

ki9@lemmy.gf4.pw · 2 pts · 348d (1 reply)

Well having the wrong permissions in system files will mess up your system. So when you restore from backup and want the backed-up permissions too.

daggermoon@lemmy.world · 1 pts · 348d

Never thought of using .tar folders for that. Makes sense.

PseudoSpock@lemmy.dbzer0.com · 4 pts · 347d

For those wishing they could do that with zip and 7zip, you can add a permissions file to the archive that you first make with getfacl, and then on the target after extracting said archive, restore permissions and ownership with setfacl from that file.

Handy for certain support organizations that insist on a zip but that later want the permissions. (Looking at you SAP and IBM).