Link in linux command line

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.

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.

Читайте также:  Linux mkdir create file

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:

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.

Читайте также:  Linux copy all to clipboard

Источник

Computer Hope

In your Linux file system, a link is a connection between a file name and the actual data on the disk. There are two main types of links that can be created: «hard» links, and «soft» or symbolic links. Hard links are low-level links which the system uses to create elements of the file system itself, such as files and directories.

Most users do not want to create or modify hard links themselves, but symbolic links are a useful tool for any Linux user. A symbolic link is a special file that points to another file or directory, which is called the target. Once created, a symbolic link can be used in place of the target file name. It can have a unique name, and be located in any directory. Multiple symbolic links can even be created to the same target file, allowing the target to be accessed by multiple names.

The symbolic link is a file in its own right, but it does not contain a copy of the target file’s data. It is similar to a shortcut in Microsoft Windows: if you delete a symbolic link, the target is unaffected. Also, if the target of a symbolic link is deleted, moved, or renamed, the symbolic link is not updated. When this happens, the symbolic link is called «broken» or «orphaned,» and will no longer function as a link.

One way to create a symbolic link in the X Windows GUI is with your file manager. Some Linux distributions use different file managers, but the process is similar. Locate a target file in your file manager GUI, highlight it by clicking it once, and select the «create a link» option. This option is usually found under the Edit menu, or in the context menu that appears when you right-click the highlighted file.

Creating symbolic links with a Linux file manager

In the example shown above, using the Thunar file manager, we have highlighted the file myfile.txt, then selected Make Link in the Edit menu. After completed, a new symbolic link called link to myfile.txt is created. This link can be renamed or moved to another location. It always points to the target, unless the target is later moved or deleted, resulting in the link becoming orphaned.

The command line is a powerful tool in Linux because it gives you greater control over your commands. (For more information about the command line, and how to access it from Linux, see our Linux and Unix shell tutorial).

You can create symbolic links using the ln command’s -s option. The general syntax for creating a symbolic link is:

For instance, if we have a file in our working directory called myfile.txt, and we want to create a symbolic link in the same directory called mylink, we could use the command:

In this command, we have opened a terminal session that places us at our shell’s command prompt. We are logged in a system named myhost as a user named user, and our working directory is a folder in our home directory called myfolder:

First, let’s use ls with the -l option to produce a long list of all the files in our directory:

We see our file, myfile.txt, which is the only file in the directory. («total 4» refers to how many blocks on the disk are used by the files listed, not the total number of files).

Читайте также:  Downloading java on linux

Let’s use the cat command to view the contents of myfile.txt:

Now, let’s create a symbolic link to mylink.txt called mylink using the ln -s command:

It seems like nothing happened, but this means it worked as expected. If there was an error, or if an unexpected condition was encountered, we would receive a notification.

Now, if we do another ls -l, we see two files — our target and our link:

One of the benefits of doing a long listing with «-l» is that we see extra information in addition to the file name. Notice the «l» at the beginning of the line containing our link name, indicating that the file is a symbolic link. Also, after mylink (in blue text) is the «->» symbol, followed by the name of the target.

Most shells, by default, are configured to display certain file types in different colors, but your terminal might show different colors or none at all.

Now, let’s use our symbolic link. If we run cat on it, it displays the contents of myfile.txt:

We can rename our link with mv, and it still points to the same target:

But what happens if we move our link somewhere else? In this case, our link breaks. We can see this by making a new directory using mkdir, and moving the link into the new directory using mv:

You can see that when we view the contents of directory newfolder with ls -l, our link is highlighted in red, indicating that it is a broken link. If we try to cat the contents of the link, the shell informs us that the file does not exist. It points to «myfile.txt» with no other path information. Therefore, the operating system looks for myfile.txt in the same directory as the link.

Let’s start over by removing newfolder and its contents using the command rm -r:

This time, let’s create the symbolic link using the absolute path to myfile.txt. Let’s double check the name of our working directory using pwd:

Our working directory is /home/user/myfolder, so let’s include this in the target name when we create the link:

As you can see from the output of ls -l, our link now points to the file /home/user/myfolder/myfile.txt. With this path information, we can move the link to another location, and it still points to our target:

Your bash shell keeps an environment variable called $PWD that always stores the value of your working directory. You can use this variable to insert the full path before your target name, as long as the target is in your working directory. We can view the value of $PWD using the echo command:

This text is inserted if we use $PWD as part of a command. It is a good idea to enclose it in quotes as «$PWD» in case the directory name has any spaces. The quotes make sure the shell knows they are part of the pathname and not command separators.

Here is our command, and a directory listing to show that it worked:

Notice that we also put a slash («/«) directly between «$PWD» and myfile.txt to complete the full path name.

Источник

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