- How to Add a Directory to PATH in Linux
- What is $PATH in Linux #
- Adding a Directory to your $PATH #
- Conclusion #
- Linux Directory Structure explained: /bin folder
- 1./bin folder
- 2./sbin folder
- 3./usr/bin folder
- 4./usr/sbin folder
- 5./path/to/some/bin
- 6./path/to/some/sbin
- Surendra Anne
- Latest posts by Surendra Anne (see all)
- What are «$PATH» and «~/bin»? How can I have personal scripts?
- 2 Answers 2
- Where should one put a bin directory in Linux? [closed]
- 1 Answer 1
How to Add a Directory to PATH in Linux
When you type a command on the command line, you’re basically telling the shell to run an executable file with the given name. In Linux, these executable programs like ls , find , file and others, usually live inside several different directories on your system. Any file with executable permissions stored in these directories can be run from any location. The most common directories that hold executable programs are /bin , /sbin , /usr/sbin , /usr/local/bin and /usr/local/sbin .
But how does the shell knows, what directories to search for executable programs? Does the shell search through the whole filesystem?
The answer is simple. When you type a command, the shell searches through all directories specified in the user $PATH variable for an executable file of that name.
This article shows how to add directories to your $PATH in Linux systems.
What is $PATH in Linux #
The $PATH environmental variable is a colon-delimited list of directories that tells the shell which directories to search for executable files.
To check what directories are in your $PATH , you can use either the printenv or echo command:
The output will look something like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
If you have two executable files sharing the same name located in two different directories, the shell will run the file that is in the directory that comes first in the $PATH .
Adding a Directory to your $PATH #
There are situations where you may want to add other directories to the $PATH variable. For example, some programs may be installed in different locations, or you may want to have a dedicated directory for your personal scripts, but be able to run them without specifying the absolute path to the executable files. To do this, you simply need to add the directory to your $PATH .
Let’s say you have a directory called bin located in your Home directory in which you keep your shell scripts. To add the directory to your $PATH type in:
The export command will export the modified variable to the shell child process environments.
You can now run your scripts by typing the executable script name without needing to specify the full path to the file.
However, this change is only temporary and valid only in the current shell session.
To make the change permanent, you need to define the $PATH variable in the shell configuration files. In most Linux distributions when you start a new session, environment variables are read from the following files:
- Global shell specific configuration files such as /etc/environment and /etc/profile . Use this file if you want the new directory to be added to all system users $PATH .
- Per-user shell specific configuration files. For example, if you are using Bash, you can set the $PATH variable in the ~/.bashrc file. If you are using Zsh the file name is ~/.zshrc .
In this example, we’ll set the variable in the ~/.bashrc file. Open the file with your text editor and add the following line at the end of it:
Save the file and load the new $PATH into the current shell session using the source command:
To confirm that the directory was successfully added, print the value of your $PATH by typing:
Conclusion #
Adding new directories to your user or global $PATH variable is pretty simple. This allows you to execute commands and scripts stored on nonstandard locations without needing to type the full path to the executable.
The same instructions apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
Feel free to leave a comment if you have any questions.
Linux Directory Structure explained: /bin folder
This is second post in our Linux directory explanation series. /bin is one more important folder. Bin stands for binary which means an executable file. This folder contains commands or scripts or executable which can be executed to accomplish a task. We have some cousins to this folder as given below along with bin folder.
Let’s start with /bin folder.
1./bin folder
/bin: Stands for binary. This folder contains base executables which are required for minimal system working. These commands are available in runlevel 1 for basic administration. Commands which are available in /bin folder is accessed by every one and can run by every user. This folder contains basic commands such as cat, chmod, chgrp, chown, date, dir, dd, df, ln, mv, echo and zipping tools such as bzip, gzip etc.
2./sbin folder
/sbin: This folder stands for system binaries or super user binaries. This folder contains commands which are required for changing system properties or system level settings such as disk management, network management etc. This folder is accessed some times by normal user but they can not execute any of the commands located in this and what ever commands/scripts located in this folder are run only by root user. If you want to make normal user to run these commands you have to implement SUDO or Powerbroker to give elevated access. Some of the commands available in this folder are chkconfig, dhcpclient, fsck and it’s related commands, ifconfig and it’s related commands, init and it’s related commands, lvm and it’s related commands etc.
3./usr/bin folder
/usr/bin: This folder is similar to /bin. This folder contains normal commands which are an extend set of commands to /bin folder for normal user and not that much essential to run the machine. This folder contains commands such as at, atq, bc, awk, cal, cmp, dig, diff, du, env, find, free, ftp, gcc, groups, id, info iostat, last, lsof, md5sum, nmap, rar, seq, tail, top, vi, unzip, who.
4./usr/sbin folder
/usr/sbin: This folder is similar to /sbin. This folder contain system commands which are an extend set of commands to /sbin folder for root user and not that much essential to run the machine. This folder contain commands such as arp, adduser, cron, cups related commands, grub related commands, kvm, ppp related commands, tcpdump etc.
5./path/to/some/bin
Some times you will see bin folder in other locations such as /usr/local/bin this is the place you can see some of the binaries which are installed on the system locally. Some time you can see a bin folder in /opt which indicates that some binaries are located in this /opt bin folder.
6./path/to/some/sbin
Some times you will see bin folder in other locations such as /usr/local/sbin this is the place you can see some of the binaries which are installed on the system locally and are system related commands. Some time you can see a sbin folder in /opt which indicates that some binaries are located in this /opt/bin folder.
PATH variable is one of the important variable related to this binaries. To know more about PATH variable read our post on path variable.
stay tuned to our next post on /dev folder
Surendra Anne
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container — June 30, 2020
- Anisble: ERROR! unexpected parameter type in action: Fix — June 29, 2020
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS — August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) — August 27, 2018
- How to use ohai/chef-shell to get node attributes — July 19, 2018
What are «$PATH» and «~/bin»? How can I have personal scripts?
What is $PATH ? How can I have commands/programs which are only available for me?
I have seen this path ~/bin mentioned before, but what is it used for, and how do I use it?
I am somewhat experimenting because this is more FAQ or wiki material than a «real question». It was prompted because I mentioned ~/bin on a previous answer (in the linked sidebar to the right) and someone commented on how to add it to PATH: now instead of brief comments, we can link to this post when ~/bin is mentioned.
2 Answers 2
$PATH is an environment variable used to lookup commands. The ~ is your home directory, so ~/bin will be /home/user/bin; it is a normal directory.
When you run «ls» in a shell, for example, you actually run the /bin/ls program; the exact location may differ depending on your system configuration. This happens because /bin is in your $PATH.
To see the path and find where any particular command is located:
$ echo $PATH /home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin. $ which ls # searches $PATH for an executable named "ls" /bin/ls $ ls # runs /bin/ls bin desktop documents downloads examples.desktop music pictures . $ /bin/ls # can also run directly bin desktop documents downloads examples.desktop music pictures .
To have your own private bin directory, you only need to add it to the path. Do this by editing ~/.profile (a hidden file) to include the below lines. If the lines are commented, you only have to uncomment them; if they are already there, you’re all set!
# set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ]; then PATH="$HOME/bin:$PATH" fi
Now you need to create your ~/bin directory and, because .profile is run on login and only adds ~/bin if it exists at that time, you need to login again to see the updated PATH.
$ ln -s $(which ls) ~/bin/my-ls # symlink $ which my-ls /home/user/bin/my-ls $ my-ls -l ~/bin/my-ls lrwxrwxrwx 1 user user 7 2010-10-27 18:56 my-ls -> /bin/ls $ my-ls # lookup through $PATH bin desktop documents downloads examples.desktop music pictures . $ ~/bin/my-ls # doesn't use $PATH to lookup bin desktop documents downloads examples.desktop music pictures .
Where should one put a bin directory in Linux? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
If i have the source folder of a program containing a /bin directory, what is the «classic» directory to put it in Linux ? Thanks
1 Answer 1
Read FHS to find out where to store different files
In your project you can store files as you want. For example:
- bin: The output executables go here, both for the app and for any tests and spikes.
- build: This folder contains all object files, and is removed on a clean.
- doc: Any notes, like my assembly notes and configuration files, are here. I decided to create the development and production config files in here instead of in a separate config folder as they “document” the configuration.
- include: All project header files. All necessary third-party header files that do not exist under /usr/local/include are also placed here.
- lib: Any libs that get compiled by the project, third party or any needed in development. Prior to deployment, third party libraries get moved to /usr/local/lib where they belong, leaving the project clean enough to compile on our Linux deployment servers. I really use this to test different library versions than the standard.
- spike: I often write smaller classes or files to test technologies or ideas, and keep them around for future reference. They go here, where they do not dilute the real application’s files, but can still be found later.
- src: The application and only the application’s source files. test: All test code files. You do write tests, no?
There are more standard ways (GNU C++ standard project?). It also depends of tools you are using.