At some point, you're going to want to run commands from non-standard directories. When that happens, you'll want to add those directories to your $PATH. Jack Wallen shows you how.
Your Linux PATH is how you define the directories for which commands can be run globally. In other words, if you have an executable file in a directory that is configured to be in your PATH, you can run that executable from anywhere in the Linux file structure. This is what makes it possible to run commands in /usr/bin from your home directory (or anywhere, for that matter).
SEE: 5 Linux server distributions you should be using (TechRepublic Premium)
Out of the box, the Linux PATH contains the usual entries, such as /usr/bin/, /usr/sbin/, /usr/local/bin, and so on. But what happens if you have a non-standard directory from which you need to be able to execute commands? That's when you have to manually add those directories to the PATH.
How do you do that? Let me show you.
Let's say you have a directory called SCRIPTS in your home directory. Let's add that to the PATH.
- Log into your Linux machine and open a terminal window.
- Open your .bashrc file for editing with the command nano ~/.bashrc.
- Scroll to the bottom of that file and add the following: PATH="~/SCRIPTS:$PATH".
It's very important to include the $PATH portion, as that makes sure the standard directories remain in your path (otherwise, the only directory in your PATH would be SCRIPTS and that would not be good). - Save and close the file.
- Close and reopen the terminal.
At this point, you can run any executable, found in the SCRIPTS directory, from anywhere in the filesystem hierarchy.
SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)
And that's how you add new directories to your PATH. This little trick will come in very handy, especially when you start writing your own bash scripts that you don't want to be saved in common directories.
Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
Open Source Weekly Newsletter
You don't want to miss our tips, tutorials, and commentary on the Linux OS and open source applications. Delivered Tuesdays
Sign up todayAlso see
- Linux 101: How to execute commands from within the nano text editor (TechRepublic)
- Linux 5.14 kernel: New and exciting features coming to the release (TechRepublic)
- How to become a developer: A cheat sheet (TechRepublic)
- Kubernetes: A cheat sheet (free PDF)(TechRepublic)
- Could Microsoft be en route to dumping Windows in favor of Linux? (TechRepublic)
- A guide to The Open Source Index and GitHub projects checklist (TechRepublic Premium)
- How open source-software transformed the business world (ZDNet)
- Linux, Android, and more open source tech coverage (TechRepublic on Flipboard)