Linux Commands

The ./ Command in Linux

There are numerous commands in Linux, and each has its functionality. Among the commands that you will come across is the “./” command. Although it may appear basic, the “./” command is handy in different instances, and knowing how and when you should use it is vital.

We will discuss the “./” command in Linux. While at it, we will give examples and instances of when you should use the command to achieve a goal. Read on!

What Is the “./” Command  in Linux

When working with Linux, you often work with scripts, binaries, executables, etc. When you have such files, executing them is possible via the command line. Linux offers the “./” command to help the users to execute the scripts or other executables files in the current directory.

The “./” command is essential when the target script or file that you want to run is stored in another folder in the current directory. You can use the “./” command to run a shell script, a Python script, a Perl script, a binary file, an executable file, etc.

Let’s give different examples to see how the “./” command works.

Example 1: Running a Shell Script

Shell scripts help with the automation of activities. You can create a shell script, make it executable, then run it using the “./” command.

Here’s a simple shell script that we created using the nano editor.

Once we save and exit the file, we can run the “ls” command to confirm that the file exists in our current directory. To make it executable, we must use the following “chmod” command:

$ sudo chmod +x <file>

Once we list the files again, we can see that the shell script has changed color, confirming that it is now executable.

The last step is to run the shell script. For that, we use the “./” command as demonstrated in the following:

$ ./<file>

When you immediately run the command, your shell script will run and give the desired output.

Example 2: Running an Executable

Compiling your program gives you an executable file as the output. For this example, we have our “hello.cpp” that we compiled using the G++ compiler. Once we do, we get the “a.out” executable file.

To run this executable file, run the “./” command and specify the target file.

$ ./a.out

That’s it! The compiled program will run and display the output on your terminal.

Example 3: Running a Python Script

As a Python programmer, you can quickly run your Python script on the terminal using the “./” command. Here’s an example of a Python script that we created for this example:

First, use the “chmod” command to make your Python script executable.

Lastly, run the “./” command with the Python script and see how it successfully runs and gives an output on the terminal.

That’s how you can use the “./” command to work with Python scripts. The given demonstrations are a few examples on how you can work with the “./” command in Linux. There are numerous instances where the command comes in handy. Most of the cases involve running the files on the terminal.

Conclusion

The “./” command helps run the scripts and executable files on the command line. This post  discussed about the “./” command. Moreover, we’ve given different examples on how to use the command to run various scripts. With these examples, you now understand how the “./” command in Linux works.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.