How To Run A File In Linux Terminal – Terminal File Run Commands Linux

The Linux terminal gives you precise control over how you run any file. Learning how to run a file in linux terminal is one of the first skills you need to master. This guide walks you through every method step by step.

You don’t need to be a command-line expert. With a few basic commands, you can execute scripts, programs, and binaries quickly. Let’s start with the simplest ways and build up to more advanced techniques.

Understanding File Types And Permissions

Before you run anything, you need to know what kind of file you’re dealing with. Linux treats files differently based on their type and permissions.

Check File Type With The File Command

The file command tells you what a file is. It reads the file’s magic numbers and headers.

file myfile

This shows if it’s a script, binary executable, or something else. For example, a Python script shows “Python script text executable.”

View Permissions With Ls -L

Use ls -l to see file permissions. The output looks like this:

-rwxr-xr-x 1 user user 1234 Mar 15 10:00 myfile

The first column shows permissions. The “x” means executable. If there’s no “x”, you need to add it.

Change Permissions With Chmod

To make a file executable, use chmod +x:

chmod +x myfile

This adds execute permission for the owner, group, and others. You can also use numeric modes like chmod 755 myfile.

How To Run A File In Linux Terminal

Now you understand permissions. Here’s the core method for running files directly.

Run An Executable Binary Or Script

If the file is in your current directory, use ./ before the filename:

./myfile

The dot-slash tells the shell to look in the current directory. Without it, Linux searches the PATH environment variable, which usually doesn’t include the current directory for security reasons.

Run A File From Any Directory

If the file is elsewhere, provide the full or relative path:

/home/user/scripts/myfile

Or use a relative path:

../projects/myfile

Run A File In The Background

Append an ampersand (&) to run the file in the background:

./myfile &

This returns the prompt immediately. The process continues running. Use jobs to see background tasks.

Running Script Files By Interpreter

Scripts need an interpreter. You can run them directly if they have a shebang line.

Bash Scripts

Bash scripts start with #!/bin/bash. Make them executable and run with ./script.sh. Or run them explicitly:

bash script.sh

Python Scripts

Python scripts start with #!/usr/bin/env python3. Run them directly after making executable:

./script.py

Or use the Python interpreter:

python3 script.py

Perl Scripts

Perl scripts use #!/usr/bin/perl. Run them with:

perl script.pl

Node.Js Scripts

Node.js scripts use #!/usr/bin/env node. Execute with:

node script.js

Running Files With Sudo Or Root

Some files require root privileges. Use sudo before the command:

sudo ./myfile

This runs the file as root. Be careful—only use sudo when necessary. Running unknown files as root can damage your system.

You can also switch to root with su - and then run the file. But sudo is safer because it logs commands.

Running Compiled Programs

Compiled programs like C or C++ binaries are executables. After compiling, run them like any other binary.

Compile And Run A C Program

First compile with gcc:

gcc -o myprogram myprogram.c

Then run:

./myprogram

Compile And Run A C++ Program

Use g++:

g++ -o myprogram myprogram.cpp
./myprogram

Run A Java Program

Java is different. Compile first:

javac MyProgram.java

Then run the class file:

java MyProgram

Note: No file extension needed with java command.

Running Files With Arguments

Many programs accept command-line arguments. Pass them after the filename:

./myfile arg1 arg2 arg3

Inside the script, these are accessible as $1, $2, $3. For Python, use sys.argv.

Example With A Bash Script

#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"

Run it:

./script.sh hello world

Outputs “First argument: hello” and “Second argument: world”.

Running Files With Environment Variables

Set environment variables before running a file. Use the export command or prefix the variable:

VAR=value ./myfile

This sets VAR only for that command. For multiple variables:

VAR1=val1 VAR2=val2 ./myfile

Persistent Variables

Export variables in your shell or .bashrc:

export MY_VAR="some value"

Then run the file normally.

Running Files From Different Locations

You don’t have to be in the file’s directory. Use absolute or relative paths.

Absolute Path

/opt/myapp/bin/start

Relative Path

../bin/start

Add To Path

Add a directory to your PATH to run files from anywhere:

export PATH=$PATH:/path/to/directory

Add this line to your ~/.bashrc to make it permanent. Then you can run files by name only:

myfile

Running Files With No Execute Permission

Sometimes you can’t change permissions. Run the file through its interpreter instead.

Run A Script Without Execute Bit

bash script.sh
python3 script.py
perl script.pl

Run A Binary Without Execute Bit

You cannot run a binary without execute permission unless you’re root. Use sudo chmod +x or copy it.

Running Files In Different Shells

You can run a file in a subshell or a different shell.

Run In A Subshell

(./myfile)

This runs in a child process. Changes to the environment don’t affect the parent shell.

Run With A Specific Shell

sh ./myfile
zsh ./myfile
fish ./myfile

Running Files With No Hangup (Nohup)

Use nohup to keep a process running after you log out:

nohup ./myfile &

Output goes to nohup.out by default. This is useful for long-running tasks.

Running Files With Timeout

Limit how long a file can run. Use the timeout command:

timeout 10 ./myfile

This stops the file after 10 seconds. Useful for testing or preventing runaway processes.

Running Files With Strace For Debugging

If a file doesn’t run correctly, trace its system calls:

strace ./myfile

This shows every system call the file makes. It helps find missing libraries or permission issues.

Running Files With Valgrind For Memory Checking

For C/C++ programs, check memory leaks:

valgrind ./myprogram

This reports memory errors. It’s essential for debugging.

Common Errors And Solutions

Here are frequent issues when running files and how to fix them.

Permission Denied

Error: “Permission denied.” Solution: Add execute permission with chmod +x.

Command Not Found

Error: “command not found.” Solution: Use ./ prefix or check PATH.

No Such File Or Directory

Error: “No such file or directory.” Solution: Check the path. The file might be missing or the shebang line points to a non-existent interpreter.

Bad Interpreter

Error: “bad interpreter: No such file or directory.” Solution: The shebang line has a wrong path. Edit the file and correct it.

Text File Busy

Error: “Text file busy.” Solution: The file is being written to. Wait for the write to finish.

Running Files With Different Users

Use su or sudo -u to run as another user:

sudo -u username ./myfile

This runs the file with that user’s permissions. Useful for testing access controls.

Running Files In Docker Containers

If you use Docker, run files inside containers:

docker run --rm -v $(pwd):/app ubuntu /app/myfile

This mounts the current directory and runs the file inside an Ubuntu container.

Running Files With Cron

Schedule a file to run automatically with cron:

crontab -e

Add a line like:

0 5 * * * /home/user/scripts/backup.sh

This runs the script daily at 5 AM. Make sure the file is executable and uses full paths.

Running Files With Systemd Services

For persistent services, create a systemd unit file. Example:

[Unit]
Description=My Service

[Service]
ExecStart=/home/user/myfile
Restart=always

[Install]
WantedBy=multi-user.target

Save as /etc/systemd/system/myservice.service. Then enable and start:

sudo systemctl enable myservice
sudo systemctl start myservice

Running Files With Screen Or Tmux

Use terminal multiplexers to run files in persistent sessions.

With Screen

screen -S mysession
./myfile
# Detach with Ctrl+A, D

Reattach later with screen -r mysession.

With Tmux

tmux new -s mysession
./myfile
# Detach with Ctrl+B, D

Reattach with tmux attach -t mysession.

Running Files With Xargs

Run a file on multiple inputs using xargs:

echo "file1 file2 file3" | xargs -n1 ./myfile

This runs myfile once for each input. The input becomes an argument.

Running Files With Find And Exec

Run a file on multiple found items:

find . -name "*.txt" -exec ./myfile {} \;

This runs myfile on each .txt file found.

Running Files With Parallel

Run multiple instances in parallel:

parallel ./myfile ::: arg1 arg2 arg3

This runs three instances concurrently. Install GNU parallel if not present.

Security Considerations

Running files from untrusted sources is risky. Here are safety tips.

Verify The Source

Only run files you trust. Check checksums with sha256sum.

Use A Sandbox

Run unknown files in a container or virtual machine.

Check The Code

Read scripts before running. Use cat or less to view contents.

Run With Limited Permissions

Create a user with minimal privileges for testing.

Frequently Asked Questions

How Do I Run A .Sh File In Linux Terminal?

Make it executable with chmod +x file.sh then run ./file.sh. Or run bash file.sh directly.

What Does ./ Mean In Linux?

The dot-slash tells the shell to look in the current directory. It’s needed to run files not in your PATH.

How Do I Run A Python File In Terminal?

Use python3 filename.py or make it executable with a shebang and run ./filename.py.

Why Do I Get “Permission Denied” When Running A File?

The file lacks execute permission. Use chmod +x filename to add it.

How Do I Run A File In The Background?

Append an ampersand: ./filename &. Use jobs to list background processes.

Putting It All Together

You now know multiple ways to run any file in the Linux terminal. Start with the basics: check permissions, use ./, and pass arguments as needed. As you gain confidence, explore background execution, scheduling, and debugging tools.

Remember to always verify file sources. The Linux terminal gives you immense power—use it wisely. Practice these commands on test files first. Soon, running files will become second nature.

For quick reference, keep these commands handy:

  • chmod +x file – Add execute permission
  • ./file – Run from current directory
  • bash file – Run as bash script
  • python3 file – Run as Python script
  • sudo ./file – Run as root
  • ./file & – Run in background
  • nohup ./file & – Run after logout

Master these and you’ll handle any file Linux throws at you. The command line is your friend—embrace it.