How to Check Path in Unix

If you've typed a command and see the error "command not found," it might mean the directory in which your executable is saved hasn't been added to your path. This minHour teaches you how to find the full path to a file, how to display your path environment variables, and how to add new directories to your path when necessary.

Steps

Find the full path to a file.

If you need to find the absolute path to a file on your system, you can do so using the find command. Let’s say you needed to find the full path to a program called :

  • Type find / -name “fun” –type f print and press .This shows the full path to the file called fun no matter which directory you’re in.If fun in a directory called /games/awesome, you’d see /games/awesome/fun in the results of the command.
  • This shows the full path to the file called fun no matter which directory you’re in.
  • If fun in a directory called /games/awesome, you’d see /games/awesome/fun in the results of the command.

Display your path environment variable.

When you type a command, the shell looks for it in the directories specified by your path. You can use echo $PATH to find which directories your shell is set to check for executable files. To do so:

  • Type echo $PATH at the command prompt and press .The results should look something like this: usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binThis output is a list of directories where executable files are stored. If you try to run a file or command that isn’t in one of the directories in your path, you’ll receive an error that says the command is not found.
  • The results should look something like this: usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • This output is a list of directories where executable files are stored. If you try to run a file or command that isn’t in one of the directories in your path, you’ll receive an error that says the command is not found.

Add a new directory to the path.

Let’s say you want to run that file called . You learned from running the find command that it’s in a directory called /games/awesome. However, /games/awesome is not in your path, and you don’t want to type the full path just to run the game. To add it to your path:

  • Type export PATH=$PATH:/games/awesome and press .Now you can run fun just by typing its name at the command line (instead of /games/awesome/fun) and pressing ↵ Enter.This change only affects the current shell. If you open a new terminal window or sign in elsewhere, you’ll have to re-add the path. To make the change permanent, add the command to your shell’s config file (e.g., .bashrc, .cshrc).
  • Now you can run fun just by typing its name at the command line (instead of /games/awesome/fun) and pressing .
  • This change only affects the current shell. If you open a new terminal window or sign in elsewhere, you’ll have to re-add the path. To make the change permanent, add the command to your shell’s config file (e.g., .bashrc, .cshrc).

Tips

  • By default, the shell does not search your current directory in Unix-type OSes (BSD, Linux, etc.) unless it’s already in your path. This can be fixed by adding a period (dot), which is the Unix short-cut for the current directory. This can be changed by going to your home directory. This should contain ‘.profile’. Use an editor, such as vi, to open, change, and save.

Warnings

  • As always, be careful what you do while logged in as root.

Leave a Comment