Open In App

How to Get the Full Path of a File in Linux

Last Updated : 23 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let’s consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem.

Before we show you the methods, let’s create a sample file that we will use in all our examples:

touch file.txt
2024-10-13_10-55

Creating file.txt Command Output

The above output indicates that the file.txt has successfully been created.

Methods to Get Full Path of a File

Let us look into six different methods of finding the full path of a file in Linux:

The readlink command will print canonical file names. We’ll use the -f option of readlink and print where the file is located.

readlink -f file.txt
The "readlink" Command

Output of the command readlink -f file.txt

Method 2: The “realpath” Command

realpath file.txt
The "realpath" Command

Output of the command realpath file.txt

Method 3: The “dirname” Command

It accepts the full path of a file, removes the file name from the path, and prints the rest to the screen.

dirname $(realpath file.txt)
The "dirname" Command

Command output for dirname $(realpath file.txt)

Method 4: “basename” and “dirname” Commands Together

We can use basename and dirname together to print the full path of a file. Let’s start with basename first:

basename $(realpath file.txt)
basename" and "dirname" Commands Together

Command output for basename $(realpath file.txt)

Let’s put together a shell script named file_path.sh containing these two commands:

#!/bin/bash
echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"\n        

When we run this, we obtain:

./file_path.sh file.txt
basename" and "dirname" Commands Together

Command output for ./file_path.sh file.txt

Method 5: The “find” Command

The find command is another way to acquire the full path of a file:

find $PWD -type f -name "file.txt"
The "find" Command

Command output for find $PWD -type f -name ‘file.txt’

Method 6: The “ls” Command

The ls command can also be used to get the full path of a file:

ls -l $(pwd)/file.txt
The "ls" Command

Command output for ls -l $(pwd)/file.txt

Key Options and Common Usage

Command

Option

Description

readlink

-f

Canonicalize by following every symlink in every component of the given name recursively

find

-type f

Specifies that we’re looking for files (not directories)

find

-name

Specifies the name of the file we’re looking for

ls

-l

It produces a long listing format displaying detailed information of the file

Conclusion

Each way has its own strengths and weakness, applications. Readlink and realpath are very simple and intuitive; their combination with basename and dirname makes them quite handy when used from scripts. Find command is good at finding files across directories. For everything else concerning file information, including path, we can use the ls command:.

As you understand the above approaches, you can choose the appropriate method for your specific needs when dealing with file paths in Linux.



Next Article

Similar Reads