Символьная ссылка linux создать

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.

Читайте также:  Linux better than windows server

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:

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.

Читайте также:  Adding user to wheel group linux

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.

Источник

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.

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.

Читайте также:  Astra linux расшарить папку samba

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.

Источник

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