Open In App

How to Get the Full Path of a File in Linux

Last Updated : 24 Jul, 2025
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 readfile.txt
touch

The above output indicates that 'readfile.txt' has been successfully created.

Methods to Get the 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.

Syntax:

readlink -f filename

Example:

readlink -f readfile.txt
readlink

Method 2: The "realpath" Command

The 'realpath' command in Linux is used to resolve the full path of a file or directory.

Syntax:

realpath filename

Example:

realpath readfile.txt
realpath

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.

Syntax:

dirname [FILE_PATH]

Example:

dirname $(realpath readfile.txt)
dirname

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:

Syntax:

basename [path] [suffix]

Example:

basename /home/sujal/localworkingDir/Dir1/Dir2/readfile.txt
basename

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

Syntax:

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

Example:

./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:

Syntax:

find / -name "filename" 2>/dev/null

Example:

find / -name "readfile.txt" 2>/dev/null
find

Method 6: The "ls" Command

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

Example:

find "$PWD" -maxdepth 1 -exec ls -ld {} \;
ls

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.


Similar Reads