1. Introduction

In this tutorial, we’ll learn five ways to list Linux files with the full path.

2. Using find

The find command output displays files with their full path by default, so we can use it for listing our files in a directory. $PWD pointing to the current directory. We can limit the depth of searches with the -maxdepth option:

$ find $PWD -maxdepth 1

We can also replace it with a specific path:

$ find /root -maxdepth 1

We list all hidden files and directories by using ‘.*’:

$ find $PWD -name ".*" -maxdepth 1

The result can be limited to files only, for doing this we can use -type f:

$ find /root -maxdepth 1 -type f

Moreover, -type d can be used for directories.

3. Using ls

We can also use the ls command to list all files. We use -d to change how ls interprets directories. The -1 option is for listing line by line. The pwd command prints the current directory, so we need to use $PWD as a variable because ls doesn’t print the path by default. For following commands to work correctly, the directory shouldn’t be empty:

$ ls -d -1 $PWD/*.*

Directories can be printed only by using ‘*’ at the path:

$ ls -d -1 $PWD/*

We can list just inside of directories and skip files in the current directory:

$ ls -d -1 $PWD/*/*

We can also print just hidden files:

$ ls -d -1 $PWD/.*

We use realpath as an alternative for $PWD:

$ ls -1 | xargs realpath

4. Using realpath

The output of the realpath command includes the full path of files, so we can efficiently use it:

We check the full path of a specific file:

$ realpath filename

We can list all files in the current directory with their paths:

$ realpath *

Or we can use the specific path:

$ realpath /root/*

Note that it doesn’t include hidden files.

5. Using printf

We can also use printf . The format specifier, %s is replaced with the argument, and \n is a newline character. It doesn’t contain hidden files:

$ printf '%s\n' "$PWD"/*

We use Quotation Mark in “$PWD” to consider paths with spaces.

6. Using a Script

We can also write a simple Bash script to list files.

In this script, we get a directory as an input parameter and print all files with their full path via a loop. Our script name is the list.sh:

#!/bin/bash
path=$1
for filename in $path/*
do
  echo $filename
done;

Finally, let’s save it and make it executable:

$ chmod +x list.sh

We can simply run it and specify the path:

$ list.sh /root

And if we want to list files in the current directory, we use $PWD:

$ list.sh $PWD

Note that In all of the above commands, we may want to save the output to a file. For this purpose, we can add “> filename “ at the end of the command:

$ list.sh $PWD > listfile

7. Conclusion

There are many ways to list files in Linux with the full path. We’ve looked at four commands and one simple Bash script. By default, ls and printf don’t include the path, so we need to use $PWD to generate it.

In this article, we covered the list of files in a directory (not just a single file). We can also get the absolute directory of a file or get the full path of a file in Linux.