Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Cabhan
Hi, I was wondering: if I want to make a file/shell script runnable by typing in its name (or a shortcut for the name), is it then really necessary to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    4

    Methods to making a file executable


    Hi,

    I was wondering: if I want to make a file/shell script runnable by typing in its name (or a shortcut for the name), is it then really necessary to add the directory of the file to the $PATH of my shell?
    I mean, the larger the PATH is, the longer it takes for the shell to find the appropriate program, no?

    Is there no other way (like creating a shortcut, similar like I would have done in Windows)
    I suppose I could create a symbolic link to the file and put in in the /usr/local/sbin directory, but what is the Jedi way?

    Thanks in advance!

    Bill

  2. #2
    Administrator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,864
    Hello and Welcome. To change file permissions to executable
    Code:
    cd /path/to/file/
    Code:
    chmod +x filename
    If the file is in a directory other than what is in your path, it won't be found. For example, if the file is in /usr/sbin/ but you do not have /usr/sbin/ defined in your system path, you'll get a "not found" error probably.
    I do not respond to private messages asking for Linux help, Please keep it on the forums only.
    All new users please read this.** Forum FAQS. ** Adopt an unanswered post.

    I'd rather be lost at the lake than found at home.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So, as you know, executable files must be located in a directory in your $PATH in order to be executed without being given the full path. As you rightly observe, there are three approaches to this:

    1) Add the binary's directory to $PATH. This used to be somewhat common when /opt was used. Now, however, it's not very popular. It is more preferred to have a few standard directories. Having said that, this does sometimes happen.

    2) Copy the binary into a directory in $PATH. This is the most common approach. It is taken by the "install" program that is generally invoked as a part of the "make install" command that you run when you compile a new program.

    3) Create a symlink for your binary in a directory in $PATH. This is done, but probably not as much as 2). It is generally used for smaller, personal programs. The problem with doing this for standard programs is that depending on where the symlink points to, it may be difficult for other users to access the binary, and it ties a file in a systemwide directory to something in someone's home directory, which could make it easy to break.


    I have a ~/bin directory that contains symlinks for little utilities that I wrote, and that are only accessible to me. Everything else pretty much gets copied into /usr/local/bin.
    MikeTbob likes this.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    4
    Alright. Thanks guys! B-)

  6. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Afterthought: you can also define an alias for your program in your login profile. E.g., in your ~/.bashrc, you might have:
    Code:
    alias myprog='/opt/app/bin/myprog'
    although this technique is usually more popular when you want to call the prog a certain way or pass default command line arguments to it, e.g.:
    Code:
    alias adminprog='sudo /opt/app/sbin/myprog -v'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •