What "executing a file" means

Executing a file means telling your Linux computer to run a program or script — to actually do what the code inside it is written to do. When you execute a file, Linux reads the instructions in it and carries them out. This is different from just opening a file to read it; executing it means the computer performs the actions the file contains.

On a Linux system, you execute files most often through the command line (also called the terminal). You type a command that points to the file, and Linux runs it. The file has to have permission to be executed — Linux won't run a file unless it's marked as executable, which is a safety feature to prevent accidental or malicious code from running.

Key Takeaways

  • A file must have execute permission before Linux will run it; you set this with the chmod command.
  • The most common way to execute a file is to type its path in the terminal, like ./script.sh or /home/user/program.
  • Scripts need a shebang line at the top (like #!/bin/bash) to tell Linux which interpreter should run them.
  • You can execute a file from any directory if you know its full path, or from the same directory using ./filename.

How to give a file execute permission

Before you can execute any file, Linux needs to know it's allowed to run. You change this permission using the chmod command in the terminal. The simplest way is to type chmod +x filename, which adds execute permission to that file. The +x part means "add execute permission to everyone who has access to this file."

For example, if you have a script called backup.sh, you would type:

chmod +x backup.sh

After you run that command, the file is now executable. You only have to do this once per file. If you want to remove execute permission later, you use chmod -x filename instead.

Running a script or program from the terminal

Once a file has execute permission, you run it by typing its path in the terminal. If the file is in the folder you're currently in, you type ./filename. The ./ means "in the current directory." So if you have an executable file called myscript.sh, you would type:

./myscript.sh

If the file is in a different folder, you type the full path to it. For example:

/home/username/scripts/myscript.sh

Or you can use a relative path if you know where the file is compared to where you are now. The terminal will run the file and show you any output it produces. If the script has errors, the terminal will usually show you an error message.

What a shebang line does

Scripts (text files containing code) need a special first line called a shebang to tell Linux which program should interpret and run the code. The shebang always starts with #! followed by the path to an interpreter. For a bash script, the shebang is:

#!/bin/bash

For a Python script, it would be:

#!/usr/bin/python3

The shebang must be the very first line of the file, with no spaces before it. When you execute the script, Linux reads this line and knows to use bash, Python, or whatever interpreter the shebang points to. Without a shebang, the script may not run correctly, or Linux may not know how to interpret it.

Different ways to run programs

You can execute a file in several ways depending on what you're trying to do. The most direct way is typing the path as shown above. But you can also use the bash command to run a bash script without needing execute permission first:

bash myscript.sh

This works because you're telling bash itself to open and run the file, rather than asking Linux to execute the file directly. Similarly, you can run a Python script with:

python3 myscript.py

You can also add a file to your system's PATH so you can run it from anywhere without typing the full path. This is more advanced and usually done for programs you use regularly, but it's worth knowing exists.

What happens when you execute a file

When you execute a file, Linux starts a new process — a running instance of that program. The program runs in your terminal (or in the background, depending on how you started it). While it's running, you see any output the program produces. When the program finishes, control returns to your terminal prompt.

If you want to run a program in the background so you can keep using the terminal, you add an ampersand (&) at the end:

./myscript.sh &

The program will run, but you'll get your terminal prompt back when ready. You can also stop a running program by pressing Ctrl+C in the terminal, which sends a stop signal to it.

Frequently Asked Questions

Why does Linux say "permission denied" when I try to run a file?

The file doesn't have execute permission set. Use chmod +x filename to add it. You may also see this error if you're trying to run a file you don't own and the owner hasn't given you permission — in that case, you may not be able to execute it.

Do I need the shebang line if I run a script with bash or python3?

No. If you type bash myscript.sh, Linux uses bash to run it regardless of what the shebang says. The shebang only matters when you execute the file directly with ./myscript.sh. It's still good practice to include it so the script works the way you expect.

Can I execute a file that's on a USB drive or external disk?

Yes, if the file has execute permission and you know its path. You can type the full path to it, like /media/usb/program. Some external drives are formatted in a way that doesn't support Linux permissions, so you may need to use bash scriptname instead of ./scriptname.

What's the difference between running a program and opening a file?

Running (executing) a program means Linux carries out the instructions in it. Opening a file usually means displaying its contents in an editor or viewer. You can open a script file to read the code, but that doesn't execute it — you have to run it separately.