Linux symbolic link to file

A link creates a reference to a file or a folder. Symbolic links are used in Linux for managing and collating files.

In this guide, learn how to use the ln command to create symbolic links in Linux.

Ln Command in Linux: How to Create Symbolic Links

  • A system running Linux
  • Access to a terminal window / command line (Activities >Search> type Terminal)
  • (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)

To use the ln command, open a terminal window and enter the command with the following format:

  • By default, the ln command creates a hard link.
  • Use the -s option to create a soft (symbolic) link.
  • The -f option will force the command to overwrite a file that already exists.
  • Source is the file or directory being linked to.
  • Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

For example, create a symbolic link with:

ln -s test_file.txt link_file.txt

This creates a symbolic link (link_file.txt) that points to the test_file.txt.

To verify whether the symlink has been created, use the ls command:

Creating a link using the ln command

A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:

ln -s /mnt/external_drive/stock_photos ~/stock_photos

This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.

Creating a link to a directory with the ln command

Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.

You might receive an error message as displayed in the image below:

example of Error message: link file already exists

The error message means that there’s already a file in the destination named link_file.txt. Use the -f option to force the system to overwrite the destination link:

ln -sf test_file.txt link_file.txt

Overwriting an existing link file

Note: Using the -f option will permanently delete the existing file.

If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:

rm link_file.txt unlink link_file.txt

Deleting link files using rm and unlink commands

The ln command can be used to create two different kinds of links:

Читайте также:  Include windows h in linux

A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.

Here are a few important aspects of a soft link:

  • If the symbolic link file is deleted, the original data remains.
  • If the original file is moved or deleted, the symbolic link won’t work.
  • A soft link can refer to a file on a different file system.
  • Soft links are often used to quickly access a frequently-used file without typing the whole location.

When a file is stored on a hard drive, several things happen:

  • The data is physically written to the disk.
  • A reference file, calledinode, is created to point to the location of the data.
  • A filename is created to refer to the inode data.

A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.

Here are a few important aspects of hard links:

  • If the original file is deleted, the file data can still be accessed through other hard links.
  • If the original file is moved, hard links still work.
  • A hard link can only refer to a file on the same file system.
  • The inode and file data are permanently deleted when the number of hard links is zero.

You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln command to create links and verify using the ls command.

Источник

Linux ln – How to Create a Symbolic Link in Linux [Example Bash Command]

A symlink (symbolic) is a type of file that points to other files or directories (folders) in Linux.

You can create a symlink (symbolic) by using the ln command in the command line.

Symbolic links are useful because they act as shortcuts to a file or directory.

In this article, I will go over how to use the ln command to create a symlink to a file or directory.

What is the difference between soft and hard links in Linux?

A soft link or symbolic link will point to the original file on your system. A hard link will create a copy of the file.

Soft links can point to other files or directories on a different file system, whereas hard links cannot.

You can find the command line using the Terminal application on Mac or using the Command Prompt on Windows.

Here is the basic syntax for creating a symlink to a file in your terminal.

ln -s existing_source_file optional_symbolic_link 

You use the ln command to create the links for the files and the -s option to specify that this will be a symbolic link. If you omit the -s option, then a hard link will be created instead.

Читайте также:  Server bsd or linux

The existing_source_file represents the file on your computer that you want to create the symbolic link for.

The optional_symbolic_link parameter is the name of the symbolic link you want to create. If omitted, then the system will create a new link for you in the current directory you are in.

Let’s take a look at an example to better understand how this works.

On my Desktop I have a file called example_fcc_file.txt .

Screen-Shot-2022-02-19-at-7.48.02-PM

I will need to first open up my terminal, and then make sure I am in the Desktop directory. I can run the command cd Desktop to navigate to my Desktop.

After running that command, you should see you are now in the Desktop.

jessicawilkins@Dedrias-MacBook-Pro-2 ~ % cd Desktop jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % 

I can then use the ln command to create a new symbolic link called fcc_link.txt .

ln -s example_fcc_file.txt fcc_link.txt

When you run that command in the terminal, you will notice that nothing was returned. That is because when the ln command is successful, there will be no output and it will return zero.

jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % ln -s example_fcc_file.txt fcc_link.txt jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % 

To check that your symbolic link was successful, you can use the ls command. The ls command will list information about files and the -l flag represents the symbolic link.

When you run that command, you should see this type of result in the terminal.

lrwxr-xr-x 1 jessicawilkins staff 20 Feb 19 19:56 fcc_link.txt -> example_fcc_file.txt 

The fcc_link.txt -> example_fcc_file.txt portion of the output shows you that the symbolic link is pointing to the file called example_fcc_file.txt .

You should also see that new symbolic link show up in your directory.

Screen-Shot-2022-02-19-at-8.11.09-PM

In this example, we want to create a symbolic link called my_music that will point to my Music folder in the home directory of my computer.

First, make sure you are in the home directory. You can run cd to get back to your home directory in the command line.

jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % cd jessicawilkins@Dedrias-MacBook-Pro-2 ~ % 

You can then use the ln command to create a symlink to the Music directory.

ln -s /Users/jessicawilkins/Music ~/my_music 

If successful, you should see it in the home directory.

Screen-Shot-2022-02-19-at-8.38.14-PM

To remove symlink you can either use the unlink or rm command.

If we wanted to remove the fcc_link.txt symlink we created earlier, then we can use either of these commands:

Читайте также:  Check space usage linux

Now we should see that the symlink was removed from our directory.

Screen-Shot-2022-02-19-at-8.47.30-PM

If we try to create a new symlink called fcc_link.txt , then it will result in an error because it is already being used and pointing to another file.

ln: fcc_link.txt: File exists 

You can overwrite this error by using the force ( -f ) option.

ln -sf example_fcc_file.txt fcc_link.txt

How to learn more about the ln command

If you want to learn more about the ln command, then you can read about it in the man pages (manual for using Linux commands).

Run man ln in your terminal and you should see the man pages for the ln command.

LN(1) BSD General Commands Manual LN(1) NAME link, ln -- make links SYNOPSIS ln [-Ffhinsv] source_file [target_file] ln [-Ffhinsv] source_file . target_dir link source_file target_file DESCRIPTION The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies''; instead, a link ``points'' to the original copy. There are two types of links; hard links and sym- bolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link. The options are as follows: -F If the target file already exists and is a directory, then remove it so that the link may occur. The -F option should be used with either -f or -i options. If none is specified, -f is implied. The -F option is a no-op unless -s option is specified. -h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f option, to replace a symlink which may point to a directory. -f If the target file already exists, then unlink it so that the link may occur. (The -f option overrides

Conclusion

A symlink (symbolic) is a type of file that points to other files or directories (folders) in Linux. You can create a symlink (symbolic) by using the ln command in the command line.

Symbolic links are useful because they act as shortcuts to a file or directory.

Here is the basic syntax for creating a symlink to a file using the terminal:

ln -s existing_source_file optional_symbolic_link

Here is the basic syntax for creating a symlink to a directory using the terminal:

ln -s path_to_existing_directory name_of_symbolic_link 

To remove symlink you can either use the unlink or rm command:

unlink name_of_symbolic_link

If you need to remove a symlink then you can use this command:

ln -sf path_to_existing_directory name_of_symbolic_link

I hoped you enjoyed this article on symbolic links and best of luck on your programming journey.

Источник

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