Curious Minds

Clear answers to science and history questions

How to Run a File in Terminal: Step-by-Step

· 5 min read · Science

To run a file in the terminal, you first open a terminal, navigate to the folder that contains the file, and then execute it with the appropriate command. For most scripts, that means typing ./filename (if it is executable) or calling the interpreter directly, such as python3 script.py or bash script.sh. The exact method depends on the file type and on your operating system.

What You'll Need

  • A terminal application: Terminal on macOS, a terminal emulator on Linux (GNOME Terminal, Konsole, etc.), or Windows Terminal, PowerShell, or Command Prompt on Windows.
  • The file you want to run, saved somewhere you can find (note its full path).
  • The right interpreter or runtime installed for that file type (e.g., Python for .py files, Node.js for .js files, Bash for .sh files).
  • Basic familiarity with typing commands and pressing Enter.

Step-by-Step

  1. Open the terminal. On macOS, press Cmd+Space, type Terminal, and press Enter. On Linux, look for Terminal in your applications menu or press Ctrl+Alt+T. On Windows, search for Windows Terminal, PowerShell, or Command Prompt.
  2. Find out where you are. Type pwd (print working directory) and press Enter. This shows the current folder. On Windows Command Prompt, use cd without arguments to see the current directory.
  3. Navigate to the file's folder. Use cd path/to/folder. For example, cd Documents/scripts. If the path contains spaces, wrap it in quotes: cd "My Scripts". On Windows, you can also type cd /d D:\folder to change drives.
  4. List the files to confirm the file is there. Type ls on macOS/Linux or dir on Windows. Check the exact filename, including extension and capitalization.
  5. Choose the right command for the file type. Common cases:
    • Shell script (.sh): bash script.sh or ./script.sh if it is executable.
    • Python (.py): python3 script.py (or python script.py on Windows).
    • JavaScript (.js): node script.js.
    • Executable binary: ./program on macOS/Linux, or program.exe on Windows.
    • Batch file (.bat): script.bat in Command Prompt.
  6. Make the file executable if needed (macOS/Linux). If you see “Permission denied” when using ./script.sh, run chmod +x script.sh once, then try again. This is not needed when you call the interpreter directly (e.g., bash script.sh).
  7. Run the command. Type it exactly and press Enter. For example: python3 hello.py. The program's output appears in the terminal.
  8. Provide arguments if the program expects them. Add them after the filename, separated by spaces: python3 script.py input.txt output.txt. Quote arguments that contain spaces.
  9. Stop a running program if necessary. Press Ctrl+C to interrupt it. This sends an interrupt signal and usually returns you to the prompt.

Common Mistakes

  • Running from the wrong folder. If the terminal says “No such file or directory,” you are probably not in the file's folder. Use pwd and ls to check, then cd to the correct location.
  • Forgetting ./ for local executables. On macOS and Linux, typing script.sh alone often fails because the current directory is not in your PATH. Use ./script.sh or call the interpreter.
  • Using the wrong interpreter. A Python 3 script run with python may fail on systems where python points to Python 2 or is missing. Try python3.
  • Missing execute permission. “Permission denied” usually means the file is not marked executable. Run chmod +x filename.
  • Typos in the filename. Filenames are case-sensitive on macOS and Linux. Script.py and script.py are different files.
  • Spaces in paths. Unquoted spaces split the path into separate arguments. Wrap the path in single or double quotes.

Tips

  • Use Tab completion: type the first few letters of a filename and press Tab to autocomplete it, reducing typos.
  • Press the Up arrow to recall previous commands instead of retyping them.
  • Use --help or -h after many commands to see usage instructions (e.g., python3 --help).
  • Redirect output to a file when you want to keep a log: python3 script.py > output.txt.
  • On Windows, if a command is not recognized, check that the program's install folder is in your PATH, or use the full path to the executable.
  • Keep scripts in a dedicated folder and use relative paths consistently to avoid confusion.

Troubleshooting

  • “command not found” or “not recognized”: The interpreter or program is not installed or not in your PATH. Install it or use its full path. On Windows, try py instead of python.
  • “No such file or directory”: Check your current folder with pwd and list files with ls. Confirm the exact filename and extension. If the file is elsewhere, cd to its folder or use an absolute path.
  • “Permission denied”: On macOS/Linux, run chmod +x filename to make it executable, or invoke it through the interpreter (e.g., bash script.sh). On Windows, right-click the file, open Properties, and check that it is not blocked.
  • Syntax errors from the interpreter: These come from the file's contents, not the terminal. Open the file in an editor and check the line number in the error message.
  • Nothing happens: The program may be waiting for input. Type input and press Enter, or press Ctrl+C to stop it. Also check that you pressed Enter after the command.
  • Wrong version of a language: If a script needs Python 3 but python runs Python 2, use python3. For Node.js, check with node --version.

Frequently Asked Questions

How do I run a file that has no extension?

Treat it as an executable: on macOS/Linux, make it executable with chmod +x filename and run ./filename. On Windows, it may need a specific program to open it; check the file's contents or documentation.

Can I run a file by double-clicking instead?

Sometimes, but behavior varies by operating system and file type. The terminal gives you more control and shows error messages, which is why it is preferred for scripts.

Why does ./script.sh work but script.sh does not?

On macOS and Linux, the current directory is usually not in your PATH for security reasons. The ./ prefix tells the shell to look in the current folder.

How do I run a file from a different folder without changing directories?

Use the full path: python3 /home/user/scripts/script.py. On Windows, use the drive and path, e.g., python C:\Users\Name\script.py.

What does Ctrl+C do?

It sends an interrupt signal to the running program, usually stopping it and returning you to the command prompt. It does not close the terminal window.

Do I need administrator or root privileges to run a file?

Only if the file or the program it calls needs to modify protected system areas. For ordinary scripts in your home folder, no special privileges are needed. Avoid running unknown scripts with elevated privileges.

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions