How to Get the Full Path of a File in Linux
Last Updated :
23 Oct, 2024
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
Creating file.txt Command OutputThe 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:
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.
readlink -f file.txt
Output of the command readlink -f file.txtMethod 2: The "realpath" Command
realpath file.txt
Output of the command realpath file.txtMethod 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)
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)
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
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:
find $PWD -type f -name "file.txt"
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
Command output for ls -l $(pwd)/file.txtKey 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