Change symbolic link in linux

But it’s not changing. I’m logged in as root. The current user/group is set to root:root. What went wrong?

Which operating system do you use?Acoording to the manaul page,-h option takes affect only on systems that can change the ownership of symbolic link.

Anything that ends with / is a directory. You mean mysymbolic , which is the symbolic link, not mysymbolic/ which is probably the directory it points to.

10 Answers 10

I was putting a slash in the end of target:

chown -h myuser:mygroup mysymbolic/ 

just removed the slash in the end and works. Here’s the correct way:

 chown -h myuser:mygroup mysymbolic 

I can’t believe after 4 years, I have bumped into my past self suffereing the same problem, the missin ‘-h’!

I’ve tried this myself and it works for me. If you have the -h it changes the owner of the symbolic link, but if you dont then it changes the owner of the file itself and not the link.

But it doesnt seem to work of the symbolic link is linked to a directory

For what it’s worth, the man page on OS X is a lot clearer on the -h option than the one on (Arch) Linux. “-h If the file is a symbolic link, change the user ID and/or the group ID of the link itself.” vs. “-h, —no-dereference affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)”

I was unable to chown a directory even with -h but using the full path worked.

# ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # chown -h deploy:deploy apps # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # chown -h deploy:deploy apps/ # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # pwd /var/www/html # chown -h deploy:deploy /var/www/html/apps # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 deploy deploy 32 Dec 30 09:02 apps -> /u/apps/ 

Is the target a file or a directory?

If it is a directory then try -H (upper case H)

Sorry for the thread necromancy, but I’d like to point out that the correct syntax is with the lowercase ‘h’.

chown -h myuser:mygroup [without trailing slash] 

should be enough and work!

Recreate that link by myuser at myuser’s home, and mv this link to the target location by sudo.

Читайте также:  Astra linux включить флешки

For example: (as myuser), ln -s somedir/ linkname (will be a broken link if somedir/ doesn’t exist in user’s directory)

Then, sudo mv linkname targetlocation (will become a valid link provided targetlocation/somedir/ exists)

Your answer is without detail and hard to fully understand. Please consider revising your answer to provide more detail.

I had a similar problem. For me, I could not chmod the symbolic link even as root regardless how I called chmod. To add confusion to this, nautilus was showing the owner/group as nothing. The owner was just blank. So I tried to change the symbolic link using nautilus running as root since chmod wasn’t working and nautilus crashed!!

But I think I figured out the problem. The directory the symbolic link was pointing to had different permissions than the symbolic link. So I chmod’ed the target directory (using -h) to my user/group name. Then chmod’ed the symbolic link to the same and it worked! And viewing the symbolic link’s details in nautilus (with root permissions) now no longer crashes.

So for others having a similar problem, check the permissions of the target directory/file and make sure it is compatible with the permissions you are setting the symbolic link to.

Источник

Dillion Megida

Dillion Megida

Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link

A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.

Some people call symlinks «soft links» – a type of link in Linux/UNIX systems – as opposed to «hard links.»

Soft links are similar to shortcuts, and can point to another file or directory in any file system.

Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.

Let’s look at the steps involved in creating and removing a symlink. We’ll also see what broken links are, and how to delete them.

The syntax for creating a symlink is:

ln is the link command. The -s flag specifies that the link should be soft. -s can also be entered as -symbolic .

By default, ln command creates hard links. The next argument is path to the file (or folder) that you want to link. (That is, the file or folder you want to create a shortcut for.)

And the last argument is the path to link itself (the shortcut).

ln -s /home/james/transactions.txt trans.txt 

After running this command, you will be able to access the /home/james/transactions.txt with trans.txt . Any modification to trans.txt will also be reflected in the original file.

Note that this command above would create the link file trans.txt in your current directory. You can as well create a linked file in a folder link this:

ln -s /home/james/transactions.txt my-stuffs/trans.txt 

There must be a directory already called «my-stuffs» in your current directory – if not the command will throw an error.

This would create a symlinked folder called ‘james’ which would contain the contents of /home/james . Any changes to this linked folder will also affect the original folder.

Читайте также:  Astra linux управление сетью

Before you’d want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.

Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L (‘l’), it means the file/folder is a symlink.

You’d also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.

There are two methods to remove a symlink:

This deletes the symlink if the process is successful.

Even if the symlink is in the form of a folder, do not append ‘/’, because Linux will assume it’s a directory and unlink can’t delete directories.

As we’ve seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.

Note that trying to do rm james/ would result an error, because Linux will assume ‘james/’ is a directory, which would require other options like r and f . But that’s not what we want. A symlink may be a folder, but we are only concerned with the name.

The main benefit of rm over unlink is that you can remove multiple symlinks at once, like you can with files.

Broken links occur when the file or folder that a symlink points to changes path or is deleted.

For example, if ‘transactions.txt’ moves from /home/james to /home/james/personal , the ‘trans.txt’ link becomes broken. Every attempt to access to the file will result in a ‘No such file or directory’ error. This is because the link has no contents of its own.

When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:

This will list all broken symlinks in the james directory – from files to directories to sub-directories.

Passing the -delete option will delete them like so:

find /home/james -xtype l -delete 

Wrapping up

Symbolic link are an interesting feature of Linux and UNIX systems.

You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Читайте также:  Astra linux postgresql репозиторий

Источник

Let me show my problem through screen shots. This is my first terminal. I changed the link from another terminal say terminal2. My problem is highlighted in this screen shots. Here the ls returns content of test1 folder after I changed the link into test2.

But I changed the link to test2, even after ls returns the files in test1. Is this behavior expected?

I don’t see the command where you change the link to test2 (which would be something like ln -sf test2 test ). readlink doesn’t make links.

4 Answers 4

The thing that is confusing you is that the pwd builtin doesn’t actually tell you the current working directory — it tells you the path that you used to get to the current working directory.

If you want the actual current working directory, you need the -P flag:

which will give you something like /home/darkknight/test1 .

That second call to readlink doesn’t do what you think

darkknight@localhost:~/test$ readlink \`pwd`/home/darkknight/test2` 

The description for readlink in the man page is

Display value of a symbolic link on standard output

readlink is a read-only operation. You will also find the value of $? to be non-zero after this, meaning this has failed because test2 is not a symlink.

Replace it with another call to ln -s -f (where -f stands for force) .

darkknight@localhost:~$ ln -s -f test2 test 

Not all distros honor -f so you may have to remove the symlink first:

darkknight@localhost:~$ rm test; ln -s test1 test 

All evidence suggests you have not changed the symlink. At 1st blush your question still implies you believe readlink changes the symlink. There is no need to use more than one terminal. I suggest you replace readlink with ls -l which more clearly displays the situation.

I don’t think readlink changes the symlink. It is used here to show the link is changed. And the issue happens if changes symlink from another terminal (no matter whether it is needed or not).

What you describe is the expected behavior.

$ mkdir test1 $ mkdir test2 $ touch test1/t1 $ touch test2/t2 $ ln -s test1 test $ cd test $ ls t1 $ rm ../test $ ln -s test2 ../test $ readlink ../test test2 $ ls t1 $ 

Simply imagine that when you cd test you actually enter test1 . You can then change the symbolic link test to whatever you want -and even delete it!- you are still in test1 .

$ pwd test $ readlink `pwd` test2 $ 

For sure, readlink returns test2 , as it goes and read the current test link that has been changed to test2 . However, at the time you cd into test , the link was to test1 . And you are still in the test1 directory.

Of course, if now you change to the test directory, you’ll be in test2 .

One last thing, to clarify. Directory test does not exist. You cannot enter nor be in this directory. Whenever you cd test , you enter the directory the test symbolic link currently points to (here test1 ).

After you’ve entered the test1 directory, you can change the test symbolic link to whatever you want, you’ll still be in test1 .

Источник

Оцените статью
Adblock
detector