- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Full Path of a File in Linux
Abstract
Every file and folder in Linux has a path that directs the user to it. This path is required for programs and scripts to locate and access files. There are various ways to locate the path to a file or folder if you need to. We can get a full file path with different commands on a Linux machine. In other words, if its arguments were supplied, this command would return the pathnames of the files that would be run in the current context.
In this tutorial, we'll show you how to obtain a file's complete path in Linux.
The file path in Linux
In Linux, there are two different kinds of paths: absolute and relative. A forward slash (/) designates the root directory, which is where an absolute path always begins. On the other hand, a relative route begins in the current directory.
In the file system, the root directory is the one with the highest level. The forward slash (/) symbol in Linux designates the root directory.
For example, the absolute path to the file "script" is /home/webmaster/script. If we are under /home/webmaster directory, the relative path to /home/webmaster/script is ./script. We can use cat script or vim script commands to view the content of the file “script”.
Using the readlink command
The “readlink” command is used to resolve symbolic links. We will see how to use the "readlink" command to find the full path. In the following example, we will use the "readlink" command to get the full path of the filename "new.txt". In the output, we will get the actual filepath,
Example
$ readlink -f new.txt
Output
/tmp/new.txt
Using the realpath command
The "realpath" command is used for resolving the absolute file names. In the following example, we will use the "realpath" command to get the full path of the filename "hsperfdata_root". In the output, we will get the full path of the file,
Example
$ realpath hsperfdata_root
Output
/tmp/d1/hsperfdata_root
Using the ls command
The "ls" command when used with the environment variable "PWD" prints the full path of the file. In the below example, we will use the "ls" command to display the full path of the filename "nates.txt". In the output, we will get the full path of the file with other details such as file creation date, time, and permissions,
Example
$ ls -l $PWD/nates.txt
Output
-rw-r--r-- 1 webmaster webmaster 0 Oct 16 07:31 /tmp/nates.txt
Using the find command
The "find" command looks for files in a directory hierarchy, this command can also be used to print a file's absolute path. We can use this command to look for files or directories that meet specific requirements. The result will be a list of all files and folders, together with their respective file paths, that fit your search parameters.
In the following example, we see how to use the “find” command to get the full path of a filename “nates.txt”. In the output, we will get the full path of the file,
Example
$ find $PWD -type f -name nates.txt
Output
/tmp/nates.txt
Conclusion
In this article, we learned about how to determine a file's full path, and we explored some actual instances. The use of the "readlink" and "realpath" commands was covered initially. Later we implemented "ls" and "find" commands to get the full path of a file in Linux.
These are the several scenarios that are used to get the full path of a file in Linux. I hope you find these examples of the commands useful and that it's easy to learn and use Linux.