How to find full path to a file in Linux

There are several methods to find the full path of a file in Linux, ranging from using simple commands like readlink and realpath to more advanced approaches involving shell scripts and the find command.

Table of Contents

  1. Using readlink command
  2. Using realpath command
  3. Using dirname command
  4. Using a shell script
  5. Using the find command
  6. Using ls command

The readlink command is used to display the target of a symbolic link (shortcut of a file), or to resolve the absolute path of the file. It can be especially useful when dealing with symlinks, as it provides a way to see the actual file or directory a symlink (shortcut) points to.

To find the full path to a file using readlink, we can use the -f option of the readlink command, as below-

$> readlink -f <FILE_NAME>

Method-2 Using realpath command

The realpath command is used to resolve all symbolic links in a given file path and print the absolute (canonical) path of the file or directory. It effectively resolves any symlinks, relative paths, or symbolic references, providing the “real” path to the file, which is particularly useful for ensuring that you are working with the actual location of a file or directory.

To find full path to a file using realpath use the command below-

$> realpath <FILE_NAME>

Method-3 (Using dirname command)

The dirname command used to extract the directory path from a full file path. Essentially, it strips the filename (or last component) from a given path and returns the directory portion of the path.

To find the full path to a file using dirname use the command below-

$> dirname $(realpath <FILE_NAME>)

Method-4 Using a shell script

To find the full path to a file, we can create a Shell script using the dirname and basename commands. Since we’re already familiar with the dirname command, let’s focus on the basename command and then move on to creating the script.

The basename command is used to extract the filename from a given path, essentially removing the directory portion and returning only the last part of the path (the file or directory name).

Thus, the basename command is used as below-

$> basename $(realpath <FILE_NAME>)

Hence, the basename is the counter part of dirname command, when used together they can provide full path to a file. That’s what we are going to do in our Shell Script.

Let’s put together a Shell Script as find_fullpath.sh

#!/bin/bash

echo “$(cd “$(dirname “$1″)” && pwd -P)/$(basename “$1”)”\n

To use this script, use the command below-

 $> ./find_fullpath.sh /path/to/file

So, what happened was-

  • “$1”: $1 represents the first argument passed to the script, which is the file or directory path. Thus, as we ran the script as ./find_fullpath.sh /path/to/file, then $1 will be /path/to/file.
  • dirname “$1”: dirname extracts the directory portion of the path. For /path/to/file, dirname returns /path/to.
  • cd “$(dirname “$1″)”: cd changes the current directory to the one returned by dirname “$1”. It ensures that you are in the directory containing the file. For /path/to/file, it changes to /path/to.
  • pwd -P: pwd -P prints the absolute path of the current directory, resolving any symbolic links. It gives the real, physical directory path. If /path/to is a symlink to /real/path, pwd -P returns /real/path.
  • $(basename “$1”): basename extracts the filename from the path, removing the directory. For /path/to/file, it returns file.

Method-5 Using the find command

The find command is used to search for files and directories in a directory hierarchy based on various criteria, such as name, type, size, and more. It is highly versatile and powerful for locating files on your system. The find command takes path of the directory and file name as argument. The syntax of the find command is as follows-

$> find /path/to/search -Option <FILE_NAME>

Option used with find command are-

-name : This option is used to specify the name of the file to search for. If the name is specified with a wildcard, it can find files of the same format.

$> find /path/to/search -name “file.txt”
$> find /path/to/search -name “*.txt”

-type : This option is used to specify the type of file to search for. ‘f’ is used to specify files, ‘d’ is used to specify directories and ‘l’ is used to specify symlinks.

$> find path/to/search -type f

-size : This option is used to specify the size of the file to search for. ‘+’ means file greater than the specified size whereas ‘-‘ means files smaller that there specified size.

$> find path/to/search -size +100M

To find full path to a file in your current working directory, you may use find command as below-

$> find $PWD -type f -name “FILE_NAME”

Method-6 Using ls command

 ls is the most basic command used in a Linux Terminal to print the content of the current working directory. We can modify the argument provide to the ls command to find full path to a file inside our current working directory.

To find full path of a file using ls use the command below-

$> ls -l $(pwd)/FILE_NAME