I have a symlink with these permissions:
lrwxrwxrwx 1 myuser myuser 38 Aug 18 00:36 npm -> ../lib/node_modules/npm/bin/npm-cli.js*
The symlink is located in a .tar.gz archive. Now when I unpack the tar.gz archive using maven the symlink is no longer valid. I’m therefore trying to reconstruct the symlink. First I create the symlink using ln but how do I set the same permissions as the original symlink?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can make a new symlink and move it to the location of the old link.
ln -s <new_location> npm2 mv -f npm2 npm
That will preserve the link ownership. Alternatively, you can use chown to set the link’s ownership manually.
chown -h myuser:myuser npm
On most systems, symlink permissions don’t matter. When using the symlink, the permissions of the components of symlink’s target will be checked. However, on some systems they do matter. MacOS requires read permission on the link for readlink, and NetBSD’s symperm mount option forces link permissions checks on read and traversal. On those systems (and their relatives, including FreeBSD and OpenBSD) there is a equivalent -h option to chmod.
chmod -h 777 npm
Method 2
When you try to use chmod to set the link’s permissions, the actually you do is to set the permissions of the link’s target.The link’s permissions are meaningless.
Method 3
When you have a link like:
link -> foo/bar
and want to change it to:
link -> new/target
There are two cases to consider:
-
foo/baris not a directory or doesn’t exist or you don’t have search access tofoo. Thenln -s new/target link
will fail because
linkalready exists, but you can overcome that by using the standard:ln -fs new/target link
-
foo/baris a directory (and you have search permission tofooto be able to determine thatfoo/baris a directory). In that case, when you do:ln -s new/target link
or
ln -fs new/target link
That’s understood as creating a new
targetsymlink inside thelinkdirectory (linkis a directory because it’s a symlink to thefoo/bardirectory). So you’ll actually create a:foo/bar/target -> new/target
To overcome that, GNU
lnhas a-Toption for the link name to always be considered as link name, and not as a directory to create the link(s) in. So, with GNUln:ln -fsT new/target link
will work. Like before, it will remove the original
linksymlink and create it anew withnew/targetas the target (and the process’ euid and egid as the owner).GNU
lnalso has a-noption. It works like-Texcept whenlinkis actually a real directory in which case it will still create the symlink inside that directory (instead of failing with an error).Portably, your best option is to remove the link first and then recreate it:
rm -f link && ln -s new/target link
On most systems, permissions on symlinks are ignored and generally fixed to rwxrwxrwx.
On systems where symlink permissions matter (like OS/X where you need read permission to a symlink to be able to resolve its target), there’s generally a way to change them (chmod -h on OS/X).
Ownership, while like above not relevant for access to the file pointed to by the symlink on most systems, can have some other relevance wrt the t bit of the parent directory or quotas…) and there’s a standard command to change it:
chown -h user[:group] the-link chgrp -h group the-link
Method 4
If you really need to change symlinks permission (usually meaningless, as written in other answers), I was successful with using -R option of chown:
chown -R myuser:mygroup link
If -R was not used, the permissions were not changed.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0