Linux create link to executable

I am installing p4v in /opt , but /usr/bin is on my path. Is it possible to create a soft or symbolic link for p4v from /opt to /usr/bin , so I can just type «p4v» since /usr/bin is in my path?

8 Answers 8

To create a symlink at /usr/bin/bar which references the original file /opt/foo , use:

You would need to apply the above command as root (i.e. with sudo ).

I am using: sudo ln –s /etc/apache2/sites-available/redmine /etc/apache2/sites-enabled/000-redmine getting error: ln: target ‘/etc/apache2/sites-enabled/000-redmine’ is not a directory

The Ubuntu documentation says » Creates hard links by default, symbolic links with —symbolic.» Will the above solution create a symbolic link as asked by OP?

I though César wanted to put his files in the /opt and /usr/bin to have the symbolic link, not other way around.

@kevinmicke after your explanation finally realized that the explanation of the answer was stated in reverse order from the command, making my brain (and others’) read it backwards

The error is that you are writing the command wrong. The correct way is

If the ‘p4v’ executable is at /opt/bin/p4v, you can simply run:

sudo ln -s /opt/bin/p4v /usr/bin/p4v sudo chmod ugo+x /usr/bin/p4v 

It would be better to add /opt/bin (or wherever the executable is) to your path:

echo "export PATH=\$PATH:/opt/bin" >> ~/.profile reset 

Check the software location by this.

which application-name #replace for the application you are looking for 

To create the soft link. for example you want to create the soft link for skype on your desktop

For more information about ln .

This template was more helpful for me than the above answers. Probably not more correct, just less obfuscated:

Just replace the parts in <> ‘s

ln -s -n ./TargetDirectory ./Nickname 

Note, this works if you both nodes are below you in the same tree. You can use relative notation

  • -s command makes it a symbolic link
  • -n makes it possible de create a folder-type symlink

Welcome to askubuntu.com. In this case the $ to indicate a command line prompt is a style choice, and not likely to be a problem. However bear in mind that including things in a code block other than the code and its output can cause confusion.

If it is saying target is not a folder , it means there are spaces in your folder names eg: New Folder has a space

You need to edit the path and add a backslash \ after every space in the paths

ln -s /opt/bin /usr/var/New\ Folder 

This is not an answer to the OPs question. Please wait until you have enough reputation to add comments.

Читайте также:  Which is better windows server or linux server

I have found that it is easier to go to where you want the link to be and then create the link using sudo ln -s /path/to/source/file , than doing ln -s target source .

So in your case I would do cd /usr/bin then sudo ln -s /opt/bin/pv4 . The other way has not been working in my case.

Источник

The ln Command in Linux: Create Soft and Hard Links

Links are one of the essential part of the Linux filesystem. Learn how to create links using ln command in this tutorial.

A link is a reference to another file. Links give the same file multiple names and allowing them to live in two or more locations simultaneously.

There are two types of links:

  • Soft link or symbolic link: This is merely a shortcut to the original file.
  • Hard link: This points to the memory location of the original file.

This picture shows how the two types of links work:

soft link and hard link in Linux

Both hard links and soft links are created with the ln command.

In this tutorial, I’ll show you how to use the ln command for creating various types of links in Linux.

Examples of the ln command

The syntax for ln command is simple:

 ln [option] target_file link_name

Let me show you some examples of using the ln command to create links in Linux.

To create a hard link to a file, you can use the ln command without any options like this:

Creating hard link in Linux with ln command

To create a symbolic link to a file, use the option -s with the target file name and the link name

 ln -s target_file link_name

Creating soft link in Linux using ln command

Most Linux terminals will show the soft link in a different color along with the destination it points to.

You’ll also notice that links start with l instead of the usual — for files in the long listing view.

Even if your terminal doesn’t show soft links in different color, you can identify links in this way.

Creating a soft link to a directory is the same as creating symbolic link to a file. You just need to replace the target file name/path with the directory name/path.

 ln -s target_directory link_name

Creating soft link to a directory in Linux

You’ll notice that the color of the soft link and hard link is usually different in the Linux terminal. Hard link to a directory is not possible (normally).

You can overwrite an existing link. By default, if you try to use an existing link to point to a new file, it will throw you an error:

ln: failed to create symbolic link 'soft-link-to-file': File exists

The ln command has two options for this purpose:

  • -i : The interactive mode asks you if you want to overwrite the existing link.
  • -f : The force mode just updates the existing link without any confirmation.

Suppose, you want to force update a symbolic link. Here’s what you can do:

ln -sf new_file existing_soft_link

Mind to add the s for soft link otherwise you’ll convert the soft link to hard link.

Читайте также:  Linux mint old versions

Update soft links in Linux

Bonus Tip: Getting the original file following a chain of links

Normally, when you use the ls command with the -l option, it shows the file it points to.

But if there is chain of links, it won’t show the original file. For example, you create a link to a file and then create another link to the first link. In the long listing, the second link will point to first link.

To find the original file from a chain of links, you can use the readlink -f in the following fashion:

The -f options stands for ‘follow’ as in ‘follow the chain’.

This image explains the example better:

Follow chain of links with readlink command in Linux

Now that you know how to create links, let’s briefly why do we need links? What practical purpose do they serve?

There could be several use cases. Let’s say you downloaded a software that comes with its code and an executable file. You keep the entire thing in the /opt directory. But to run the program from anywhere, you need to put its executable in the /usr/bin directory.

If you move the executable to this directory, it may not work as it needs to access some of the code and it won’t find the path to these files from the /usr/bin directory. This is where you can create a link to this executable file in the /usr/bin directory.

This way, the program can be run from anywhere on the system and the executable of the program remains in its original program folder.

Links are an essential part of Linux. You’ll find them used at many places in your systems. Just look in the /lib directory and you’ll see plenty of soft links.

I highly recommend reading the following articles that relate to links concept in Linux:

I do hope you learned to use the ln command effectively in this tutorial. Questions and suggestions are always welcome.

Источник

I have a directory with code files and executable files and I want to know how to create a symbolic link only to executable files in that directory? I know that path-of-directory/* selects all files however I only want the executables.

3 Answers 3

You can use find to list executable files:

find /foo -type f -executable 

You can then use the -exec option to create the link.

 -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `<>' is replaced by the current file name being processed everywhere it occurs in the arguments to the command [. ] 

So, to create a link in ~/bar to each executable file in ~/foo , you would do

find ~/foo -type f -executable -exec ln -s <> ~/bar \; 

Note that this is not searching for binary files, simply for those that have the executable bit set.

Читайте также:  Linux сменить владельца root

@tony_pt you’re very welcome. If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

What man find says about -executable : «Because this test is based only on the result of the access(2) system call, there is no guarantee that a file for which this test succeeds can actually be executed». However this caution is probably irrelevant for Joe averages files on a local disk.

@HagenvonEitzen yes, that’s why I clarify that this will only search for files that have the executable bit set at the end of my answer.

for i in ~/foo/*; do [ -x "$i" ] && ln -s "$i" ~/bar; done 

-x tests if the file exists and is executable. See man test for more information.

If you want to create working relative links you might need to think about how you provide your directory paths, or use the -r option to ln (which is a somewhat new GNU extension (≥ 8.16), but should be present in all «recent» distributions).

If you want to traverse into subdirectories using this method, activate globstar with shopt -s globstar and give the search path as ~/foo/** (see globstar in the Bash manual).

The [ -x ] test will include directories with the executable bit set, which might not be wanted. If it is a problem, an extra [ -f ] test to see if the match is a file can be added.

Источник

I’m assuming you want to be able to call these executables without typing out the full path each time. Am I right?

Add your folder to the default path, and that’ll do it:

Just create the folder and insure that you have the following in ~/.profile

# set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi 

In Ubuntu 20.04, a user’s ~/.profile file by defaut contains the statement:

# set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi 

Hence all a user has to do is to:

  1. Create the private bin directory. Either via the nautilus GUI or via issuing the command mkdir ~/bin in the terminal.
  2. Reload the ~/.profile file. You do this by typing executing the terminal command source ~/.profile .
  3. Copy and paste your executable into the ~/bin directory or create a link to your executable into the ~/bin directory.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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