How to Get the Full Path of a File in Linux
Last Updated :
24 Jul, 2025
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
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:
Method 1: The "readlink" Command
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
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
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)
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
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
Command output for ./file_path.sh file.txtMethod 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
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 {} \;
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
How to Open a File in Linuxâ In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
Extract Filename From the Full Path in Linux Linux is a family of open-source operating systems and comes as various distributions or distros. The full path in Linux means starting from the root directory "/", the address of the file includes the directories and subdirectories until the file name. A full file path in Linux looks as follows: /h
2 min read
How to Find Out File Types in Linux In Linux, everything is considered as a file. In UNIX, seven standard file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket. In Linux/UNIX, we have to deal with different file types to manage them efficiently.Categories of Files in Linux/UNIXIn
7 min read
How to find the full path of the Python interpreter? Method 1: Using the sys Module (Windows, Ubuntu, macOS)The sys module provides access to variables and functions that interact with the Python interpreter. The sys.executable attribute gives the absolute path of the Python interpreter. Steps:Import the sys module.Print the path.Example: Python impor
2 min read
How to Display Path of an Executable File in Linux | Which Command In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th
6 min read
How to get a File Extension in PHP ? In this article, we will learn how to get the current file extensions in PHP. Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css" Using $_SERVER[âSCRIPT_NAMEâ]: $_SERVER is an array o
2 min read