Open In App
Related Articles

How to Get the Full Path of a File in Linux

Improve Article
Save Article
Like Article

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:

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.

Now, we will move on to the demonstration. But before demonstrating the combined use of basename and dirname, we will first see how basename works:


Clearly, the above output shows just the file name.

Next, we will see how the above commands can be used together. Let’s create a shell script named, which contains the following:

echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"

Above, we have used the dirname command to find the location of the file, which we will input as the first command line argument. Then, we navigated to the file location and printed the current working directory. We requested the physical location of the file by using the -P command. Finally, we have used the basename command to print only the file name.

If we execute the above shell script, the output will be:


We can see above that the outputs of those two commands are concatenated into one to get the full path of the file.

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:


Above, we have used the find command with the following sub-commands to get the file location:

  • The -type f command helps us by looking for files.
  • The -name command helps us by looking for the files in the current working directory and its sub-directories.

The above output clearly shows us the full path of the file.

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:


We can see above that the full path of the file is displayed on the screen.


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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials