The “realpath” Command

The realpath command displays a file’s absolute path. We will not use any option here, unlike the readlink command. The implementation and output are given below:

 

How to Get the Full Path of a File in Linux

We often work with Linux files while creating shell scripts. But sometimes we need to know the full path of a file we are working on and this article will let you know how you can get the full path of a file. We have listed some methods that will help you find the location of a particular file.

But before going any further, let’s first create a file:

 

The above output clearly shows that file.txt has been successfully created.

Now, we will take a look at the methods that will give us the full path of a file. In total, there are 6 methods listed in this article. They include:

Similar Reads

Method 1: The “readlink” Command

The readlink command is used to print canonical file names. We will use the -f option in combination with readlink to print the location of the file. Here are the implementation and output:...

Method 2: The “realpath” Command

The realpath command displays a file’s absolute path. We will not use any option here, unlike the readlink command. The implementation and output are given below:...

Method 3: The “dirname” Command

Another interesting option is the dirname command. It takes the full path of a file, strips the file name from the path, and displays the rest on the screen. Check out the implementation and output below:...

Method 4: The combination of “basename” and “dirname” commands

We have seen how to use the dirname command. But there’s another thing we can do with this command. We can combine this command with another one called basename. The basename command does the complete opposite of dirname. It takes the full path of a file, strips whatever appears before the file name, and displays the rest on the screen....

Method 5: The “find” Command

The find command is another way to get the full path of a file. Let’s see how it works:...

Method 6: The “ls” Command

Yet another method is to use the ls command. If a directory is passed as an argument, then it lists all the files and folders that are located inside that directory. But if a file name is passed along with the pwd command, then the command shows us the file location. See the following:...

Conclusion:

In this article, we have discussed some useful ways to get the full path of a file. So, the next time you get stuck, use one of the above methods to get your answer immediately....