How to Handle the Path Variable in Unix: 3 Steps

Table of contents:

How to Handle the Path Variable in Unix: 3 Steps
How to Handle the Path Variable in Unix: 3 Steps
Anonim

Did you run a command and get the "command not found" error message as a result? Probably, the path where the executable is stored is not present in the system "path" variable. This article explains how to find the full path of a file, how to view environment variables related to object paths, and how to add a new folder to the "path" variable when needed.

Steps

Check Path in Unix Step 1
Check Path in Unix Step 1

Step 1. Identify the full path to a file

If you need to find the absolute path of a file within your system, you can use the find command. For example, assume that you need to find the full path of a named program fun. To fix the problem, follow these instructions:

  • Type the command find / -name "fun" –type f print and press the Enter key.

    • In this way, the absolute path of the named file will be displayed on the screen fun, regardless of the current working directory.
    • If the fun it is stored in the directory / games / awesome, the result of executing the given command will be / games / awesome / fun.
    Check Path in Unix Step 2
    Check Path in Unix Step 2

    Step 2. Display the contents of the "path" system variable

    When you try to execute a command, the system shell automatically looks for it in all directories specified in your "path" variable. To see the list of folders in which the command shell searches for files to execute, you can use the echo $ PATH command. Follow these instructions:

    • Type the command echo $ PATH inside the command prompt and press the Enter key.

      • The result that will appear on the screen should be similar to the following: usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin.
      • This is a directory listing that the system shell will use to search for files to execute when entering commands. When you try to execute or reference a file or command that is not present in one of the directories listed in the system "path" variable, you will receive an error message.
      Check Path in Unix Step 3
      Check Path in Unix Step 3

      Step 3. Add a new directory to the "path" variable

      Assume you want to run a file named fun. By running the find command you discovered that the fun it is stored in the / games / awesome folder. Unfortunately, the / games / awesome path is not contained in the "path" variable, but you also don't want to waste time typing the full path to the file every time you need to run it. To fix the problem, follow these instructions:

      • Type the command export PATH = $ PATH: / games / awesome and press the Enter key.

        • At this point, you can run the file fun simply by typing the corresponding name into the command prompt (instead of having to enter the full path / games / awesome / fun) and pressing the Enter key.
        • The indicated change will affect only the current instance of the command prompt. This means that by opening a second "Terminal" window or logging in with another user account, you will have to change the system "path" again. To make the change permanent, enter the command indicated in the command shell configuration file (for example .bashrc or .cshrc).

Recommended: