How to Run a Program from the Command Line on Linux

Download ArticleDownload Article

Most Linux distributions have a graphical user interface that allows you to open programs by just clicking on the program's icon in the Apps menu. However, there are situations where you may want to run a program from the Terminal. The Terminal is a powerful tool that allows you to run programs and manage your Linux system using keyboard commands. This How.com.vn teaches you how to run a program from the Terminal in Linux.

Method 1
Method 1 of 4:

Running a Program From the Terminal

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to...
    Press Ctrl+Alt+T to open the Terminal. You can open the Terminal using the keyboard shortcut on most Linux distributions. The keyboard shortcut is Ctrl + Alt + T. You can also click the Terminal icon in your Apps menu. It generally has an icon that resembles a black screen with a white text cursor.[1]
  2. How.com.vn English: Step 2 Type the name of the program and press ↵ Enter.
    Unlike the Windows command-line (CMD), you do not need to type out the full directory path or change directories for most programs in Linux.[2] As long as a program is in your user "$PATH" variable, Linux will search $PATH for the program and launch it automatically.
    • For example, if you want to run Firefox from the Terminal, you would simply type firefox and press Enter.
    • Type -h or --help after the program name to display the help menu for that program. Many programs have additional command modifiers you can use to launch the program in a specific way. For example, you can launch a website in a web browser by typing the web browser name followed by the web address and press Enter to launch that website in the web browser (i.e. firefox www.wikihow.com.
    • If you receive a message that says you don't have permission to run a program or access is denied, type sudo before the program name and press Enter. The "sudo" command allows regular users to run Terminal commands with administrative privileges or root access.
    • If you want to run a C or C++ program from the Terminal, you will first need to complie the program before you can launch it from the Terminal.
  3. Advertisement
Method 2
Method 2 of 4:

Running a Program Outside the $PATH Variable

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal.
    You can open the Terminal using the keyboard shortcut on most Linux distributions. The keyboard shortcut is Ctrl + Alt + T. You can also click the Terminal icon in your Apps menu. It generally has an icon that resembles a black screen with a white text cursor.[3]
  2. How.com.vn English: Step 2 Type cd followed by a space and the location the program file is saved to.
    If the program launch file is saved to a location that is not in your $PATH variable, then you will need to navigate to that location inside the Terminal. You can do so using the "cd" command. For example, if you have folder for Python programs saved in your "Documents" folder, you can navigate to it in the Terminal by typing cd ~/Documents/Python or something similar, and then press Enter.
  3. How.com.vn English: Step 3 Type chmod a+x [filename] and press ↵ Enter.
    Replace "filename" with the actual launch file of the program. The "chmod a+x" command tells Linux the file is an executable file.[4]
  4. Step 4 Type "./" followed by the launch filename and press ↵ Enter.
    This launches the program. For example, if you have a Python file called "Helloworld.py", you would type ./helloworld.py to launch the file.[5]
  5. Advertisement


Method 3
Method 3 of 4:

Adding a Directory to your $PATH Variable

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal.
    If you cannot run a program by simply typing the program's name, you may need to add the directory that the program is installed in to your $PATH variable. You can do this from the Terminal as well. Use the keyboard shortcut to open the Terminal if you haven't already done so.[6]
  2. How.com.vn English: Step 2 Type the command to export the programs directory to the $PATH variable.
    To do so, type export PATH=$PATH:[path/to/program] and press Enter. Replace "[path/to/program]" with actual directory tree the program is installed in. This will only last until the end of your current session.
    • For example, if you have a program installed in the "bin" directory of your "Home" folder, you would type export PATH=$PATH:$Home/bin and press Enter. This will temporarily add the "$Home/bin" directory to your $PATH variable.
    • You can see which directories are currently added to your $PATH variable by typing the command echo $PATH and pressing Enter.
  3. How.com.vn English: Step 3 Type nano ~/.bashrc and press ↵ Enter.
    This opens the ".bashrc" file in a text editor that is based in the Terminal. You can use this file to permanently add the directory to your $PATH variable.
  4. Step 4 Add the "export PATH" command to the file.
    To do so, scroll down to the bottom of the file using the mouse wheel and type export PATH=$PATH:[path/to/program] at the bottom. Replace "[path/to/program]" with the actual directory tree the program is installed in.[7]
  5. How.com.vn English: Step 5 Press Ctrl+X.
    This exits the text editor. It will ask if you want to save the file.
  6. How.com.vn English: Step 6 Press Y and press ↵ Enter.
    This confirms that you want to save and exit the text editor. You will be returned to the standard command prompt in the Terminal.
  7. How.com.vn English: Step 7 Type source ~/.bashrc and press ↵ Enter.
    This loads the updated $PATH variable into your current session.[8]
  8. How.com.vn English: Step 8 Type the name of the program and press ↵ Enter.
    With the program's directory now added to your $PATH variable, you should be able to launch the program by simply typing the program name and pressing Enter.
  9. Advertisement
Method 4
Method 4 of 4:

Installing and Uninstalling a Program in the Terminal

Download Article
  1. How.com.vn English: Step 1 Press Ctrl+Alt+T to open the Terminal.
    In addition to launching programs from within the Terminal, you can also install and uninstall programs from within the Terminal. Use the keyboard shortcut to open the Terminal if you haven't already done so.[9]
  2. How.com.vn English: Step 2 Type sudo apt install [app_name] and press ↵ Enter (Debian).
    Replace "[app_name]" with the actual name of the program you want to install. This command works in Debian-based Linux distributions, such as Ubuntu, and Mint.[10]
  3. How.com.vn English: Step 3 Type sudo apt remove [app_name] and press ↵ Enter (Debian).
    Replace "[app_name]" with the name of the app you want to uninstall. This command works in Debian-based Linux distributions, such as Ubuntu, and Mint.
  4. How.com.vn English: Step 4 Type sudo dnf install [app_name] and press ↵ Enter (Red Hat).
    Replace "[app_name]" with the actual name of the program you want to install. This command works for Red Hat Linux distributions such as RHEL, Fedora, and CentOS.[11]
  5. How.com.vn English: Step 5 Type sudo dnf remove [app_name] and press ↵ Enter (Red Hat).
    Replace "[app_name]" with the name of the app you want to uninstall. This command works for Red Hat Linux distributions such as RHEL, Fedora, and CentOS.[12]
  6. How.com.vn English: Step 6 Type the name of the program and press ↵ Enter.
    Once the program is installed, you can launch the program by simply typing the program's name and pressing Enter in the Terminal.
  7. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about computer programming, check out our in-depth interview with Kevin Burnett.

      About This Article

      How.com.vn English: Kevin Burnett
      Written by:
      Software Developer
      This article was written by Kevin Burnett and by How.com.vn staff writer, Travis Boylls. Kevin Burnett is a Software Developer with over 20 years of professional experience. He spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Ruby, Python, and JavaScript. This article has been viewed 430,635 times.
      How helpful is this?
      Co-authors: 6
      Updated: September 18, 2023
      Views: 430,635
      Categories: Programming
      Article SummaryX

      1. Press Ctrl + Alt + T to open the Terminal.
      2. Type the name of the program you want to launch.
      3. Press Enter.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 430,635 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement