The three ways to execute a file in Linux
To run a file in Linux, you need to tell the system it is executable and then call it by name or path. The most common method is to navigate to the file's location in the terminal and type ./filename — the dot-slash tells Linux to look in the current folder. You can also type the full path, like /home/username/myscript.sh, from anywhere. A third option is to add the file's folder to your system's PATH variable, which lets you type just the filename from any location.
Before you can run any file, Linux needs to know it has permission to execute it. By default, newly created files are not executable. You grant that permission with the chmod command, most commonly chmod +x filename. After that single step, the file can be run.
Key Takeaways
- Files must have execute permission before they can run; grant it with chmod +x filename.
- Run a file from its location using ./filename, or from anywhere using the full path /home/user/filename.
- Scripts need a shebang line at the top — #!/bin/bash for bash scripts — so Linux knows which interpreter to use.
- You can run files in the background by adding an ampersand at the end: ./filename &.
Setting execute permission with chmod
The chmod command changes file permissions. To make a file executable, open a terminal, navigate to the folder containing the file, and type chmod +x filename. The +x part means "add execute permission for everyone." You only need to do this once per file.
If you want to be more specific about who can run the file, you can use chmod u+x filename to let only the owner run it, or chmod 755 filename to use numeric permissions. For most personal scripts, chmod +x is sufficient. You can check a file's current permissions by typing ls -l filename — if you see an x in the output, the file is executable.
Running a file from the current directory
Once a file has execute permission, the simplest way to run it is from the folder where it lives. Type ./filename and press Enter. The dot represents the current directory, and the slash tells Linux to look there for the file. This method works whether the file is a compiled program, a shell script, or any other executable.
If you type just filename without the dot-slash, Linux will search your PATH variable — a list of standard system folders — but not the current directory. This is a safety feature: it prevents accidentally running a malicious file with the same name as a system command. Always use ./ when running files in your current folder.
Running a file from any location using the full path
You can run a file from anywhere by typing its complete path. For example, /home/username/scripts/myscript.sh will execute that file no matter which folder you are currently in. This is useful when you want to run a script from a different directory or include the command in another script.
You can also use a tilde ~ as shorthand for your home directory. If your script is in your home folder, type ~/myscript.sh. If it is in a subfolder called scripts, type ~/scripts/myscript.sh. The tilde expands to your actual home path automatically.
Adding the shebang line to scripts
Scripts need a shebang line at the very top to tell Linux which interpreter should run them. For a bash script, the first line must be #!/bin/bash. For a Python script, use #!/usr/bin/python3. For a Perl script, use #!/usr/bin/perl. Without this line, Linux may not know how to interpret the file, even if it is executable.
The shebang must be the first line of the file, with no spaces before the hash symbol. After you add it and save the file, make the file executable with chmod +x filename, and you can run it by typing ./filename. Linux will read the shebang and automatically use the correct interpreter.
Running files in the background
By default, when you run a file, the terminal waits for it to finish before returning control to you. If the program takes a long time, you cannot type new commands. To run a file in the background, add an ampersand at the end: ./filename &. The terminal will return immediately, and the program will continue running.
You can see all running background processes by typing jobs. To bring a background process back to the foreground, type fg. If you want a process to keep running even after you close the terminal, use nohup ./filename & — the nohup command stands for "no hang up" and prevents the process from stopping when you log out.
Troubleshooting common execution problems
If you see "Permission denied" when trying to run a file, it does not have execute permission. Run chmod +x filename and try again. If you see "command not found" when using a full path, check that the path is correct by typing ls /path/to/file first.
If a script runs but produces unexpected output or errors, the shebang line may be wrong, or the script itself may have a bug. Check the first line of the file to confirm it points to the correct interpreter. You can also run the script with the interpreter directly: bash myscript.sh or python3 myscript.py. This bypasses the shebang and can help you identify whether the problem is with the interpreter or the script.
Frequently Asked Questions
What is the difference between ./ and just typing the filename?
The dot-slash tells Linux to look in the current directory. Without it, Linux searches only the folders listed in your PATH variable, which usually does not include the current directory for security reasons. Always use ./ when running files in your current folder.
Do I need to use chmod every time I run a file?
No. You only run chmod +x once per file. After that, the file stays executable until you remove the permission with chmod -x filename.
Can I run a file without the shebang line?
You can run it directly with the interpreter: bash myscript.sh or python3 myscript.py. But if you want to run it as ./myscript.sh, the shebang line is required so Linux knows which interpreter to use.
What does nohup do?
The nohup command prevents a process from stopping when you close the terminal or log out. Use nohup ./filename & to run a file in the background that will keep running after you disconnect.
How do I stop a running program?
Press Ctrl+C in the terminal where it is running. If it is running in the background, type kill %1 (where 1 is the job number from jobs) or kill PID (where PID is the process ID from ps).