ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Linux: Symbolically Linking Files

    IT Discussion
    linux sam linux administration symlink
    4
    14
    3.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      A common task of any computer user is creating links to files. Even to Windows desktop users, this is a common chore. There are two types of links: softlinks (also know as symnbolic links, symlinks or, on Windows, shortcuts) and hardlinks. We are going to look at symlinks now and will delve into hardlinks later when we have learned more about how Linux files work "under the hood."

      In this lesson we will be learning to use the following command:

      • ln -s: The symlink command. "s" is for symbolic or soft.

      A symlink is actually just a file itself. All it contains is the path to the file to which it points. It works, more or less, identically to how shortcuts work on Windows.

      The symlink command is very straightforward and is simply the ln command with the "-s" flag followed by the path of the original file followed by the path to the desired link file.

      # cd /tmp
      # touch myfile
      # ln -s myfile mysymlink
      

      That is all that it takes.

      # ls -l
      total 0
      -rw-r--r-- 1 scott scott 0 Feb 10 04:49 myfile
      lrwxrwxrwx 1 scott scott 6 Feb 10 04:49 mysymlink -> myfile
      

      You can see in the output of the ls -l command that the symlink is designated with a rather obvious "->" array that shows how it points to the actual file. Very easy to read. If you have full colour on your console you should see the symlink in a light blue colour as well.

      You can just as easily make a symlink directly to a directory rather than to a normal file. This is actually incredibly common on Linux to do.

      # cd /tmp
      # ln -s /var/log mydir
      # cd mydir
      

      Notice here that you are indeed magically transported into the /var/log directory and can see its contents, but if you do a pwd command or notice your command problem that it shows you as being in /tmp/mydir. It is using the /tmp/mydir path that you just created to get you to the /var/log directory. So be aware that this can lead to some confusion, but is also extremely powerful.

      You can safely delete a symlink with just the rm command.

      # cd /tmp
      # rm mydir
      # rm mysymlink
      

      One of the things that is difficult with symlinks is knowing how any given operation is going to treat it. The rm command or the mv command will delete, move or rename the symlink itself, but the cp command will copy the file to which the symlink points rather than the symlink itself. For the most part these commands behave as expected, but not always and some commands have flags to alter if the command follows symlinks or not (especially important for certain types of operations, like those that determined used storage space!)

      iroalI 1 Reply Last reply Reply Quote 4
      • iroalI
        iroal @scottalanmiller
        last edited by

        @scottalanmiller

        First, Thanks a lot for these tutorials are really useful for me.

        I get the error "No such file or directory" when execute "cd my dir" in:

        cd /tmp
        ln -s mydir /var/log
        cd mydir

        If I go to /var/log I can see the Link file created before.

        scottalanmillerS 1 Reply Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller
          last edited by

          Should be "cd mydir" without the space. You can do an ls to see what you have stored there.

          1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller @iroal
            last edited by

            @iroal said:

            If I go to /var/log I can see the Link file created before.

            Hmmm... nothing should have been created in there. Which file appeared in there?

            1 Reply Last reply Reply Quote 0
            • iroalI
              iroal
              last edited by

              0_1455093508778_Clipboard02.jpg

              It's Centos 7, spanish. let my know if you need any test.

              1 Reply Last reply Reply Quote 0
              • scottalanmillerS
                scottalanmiller
                last edited by

                Ah yes, you have discovered a typo in my copying over from one system to the other. The linking command should have been...

                # ln -s /var/log mydir
                
                iroalI 1 Reply Last reply Reply Quote 1
                • iroalI
                  iroal @scottalanmiller
                  last edited by

                  @scottalanmiller

                  It works, thaks you.

                  scottalanmillerS 1 Reply Last reply Reply Quote 0
                  • scottalanmillerS
                    scottalanmiller @iroal
                    last edited by

                    @iroal said:

                    @scottalanmiller

                    It works, thaks you.

                    Thanks for being an early adopter and proof reading my code for me!

                    1 Reply Last reply Reply Quote 0
                    • alex.olynykA
                      alex.olynyk
                      last edited by

                      I created a soft link to /etc/firewalld/zones/public.xml and called it 'fw'

                      But 'fw' is only available from the root directory.

                      How can I make 'fw' available from anywhere on the system?

                      scottalanmillerS 1 Reply Last reply Reply Quote 0
                      • scottalanmillerS
                        scottalanmiller @alex.olynyk
                        last edited by

                        @alex.olynyk said:

                        I created a soft link to /etc/firewalld/zones/public.xml and called it 'fw'

                        But 'fw' is only available from the root directory.

                        How can I make 'fw' available from anywhere on the system?

                        Well a couple of options. But you are kind of thinking of fw as an alias not as a symbolic link.

                        If you wanted a symbolic link that was really handy, you could make it directly under the /. So...

                        ln -s /etc/firewalld/zones/public.xml /fw
                        

                        Then to look at that file you could do...

                        cat /fw
                        less /fw
                        

                        Like that.

                        But likely what you actually want is an alias. What do you want to do with that file? Edit it? If so, use your editor of choice (mine is vi and we will cover why in a later lesson) you could do this...

                        alias fw="vi /etc/firewalld/zones/public.xml"
                        

                        Then when you run fw it opens the file instantly for editing.

                        fw
                        
                        1 Reply Last reply Reply Quote 0
                        • BRRABillB
                          BRRABill
                          last edited by BRRABill

                          Not quite thinking what I was doing, I was trying to forward my /var/log to another folder without delete the original first. Duh.

                          Anyway, I ran this command:

                          ln -s /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs /var/log
                          

                          And what it did was put a folder called "xenserverlogs" into the /var/log folder

                          Why did it do that?

                          scottalanmillerS 1 Reply Last reply Reply Quote 0
                          • scottalanmillerS
                            scottalanmiller @BRRABill
                            last edited by

                            @BRRABill said in Linux: Symbolically Linking Files:

                            Not quite thinking what I was doing, I was trying to forward my /var/log to another folder without delete the original first. Duh.

                            Anyway, I ran this command:

                            ln -s /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs /var/log
                            

                            And what it did was put a folder called "xenserverlogs" into the /var/log folder

                            Why did it do that?

                            The format is ln -s existingfile linklocation

                            So you designated /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs as the existing file. And you told it to make a symbolic link in the /var/log directory. Since /var/log already existed and you didn't specify the name of the symbolic link it had no choice but to use the default which is the "same as the original" which was xenserverlogs. So it created a link from /var/log/xenserverlogs to the file /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs.

                            And be sure to never use a term like "forward" in conjunction with links, that's a very different concept and does not apply here. That will just confuse you.

                            BRRABillB 1 Reply Last reply Reply Quote 0
                            • BRRABillB
                              BRRABill @scottalanmiller
                              last edited by

                              @scottalanmiller said

                              So you designated /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs as the existing file. And you told it to make a symbolic link in the /var/log directory. Since /var/log already existed and you didn't specify the name of the symbolic link it had no choice but to use the default which is the "same as the original" which was xenserverlogs. So it created a link from /var/log/xenserverlogs to the file /run/sr-mount/da71e8e4-27b9-f51d-3390-93793b0a2bfd/xenserverlogs.

                              And be sure to never use a term like "forward" in conjunction with links, that's a very different concept and does not apply here. That will just confuse you.

                              So to confirm, if I had first removed the /var/log folder, that command would have worked as I wanted it to, correct?

                              scottalanmillerS 1 Reply Last reply Reply Quote 0
                              • scottalanmillerS
                                scottalanmiller @BRRABill
                                last edited by

                                @BRRABill said in Linux: Symbolically Linking Files:

                                So to confirm, if I had first removed the /var/log folder, that command would have worked as I wanted it to, correct?

                                Correct. Since /var/log is a mount point for you, though, that is not as simple as deleting or renaming. You have to stop it mounting first. Then you can rmdir /var/log. Then you can symlink.

                                BRRABillB 1 Reply Last reply Reply Quote 1
                                • 1 / 1
                                • First post
                                  Last post