Bash: How to Use ls Command to List Files with Full Path


You can use the ls command in Bash to list all files in the current directory.

By default, the ls command returns only the filenames without the full file paths.

If you would like to display the full path for each filename, you can use the following syntax:

find $PWD -maxdepth 1 -type f

This particular example will return the full path for each filename only in the current directory.

Here is what the various commands do:

  • find: Returns items from a particular directory
  • $PWD: References the current directory
  • -maxdepth 1: Specifies that only items should be returned from current directory (and not from subfolders)
  • -type f: Specifies that only paths to files should be returned

The following example shows how to use this syntax in practice.

Example: How to List Files with Full Path in Bash

Suppose that we use the ls command in Bash to view all files and folders in the current directory:

Note that this returns the following items:

  • athletes.txt
  • managers.txt
  • new_data (folder)
  • owners.txt
  • teams.txt

Notice that the full path is not shown for any of the files.

In order to display the full path for each file in the current directory, we can instead use the find command with the following syntax:

find $PWD -maxdepth 1 -type f

The following screenshot shows how to use this syntax in practice:

Bash ls full path

We can see that this returns the full path of each filename in the current directory:

  • /c/users/bobbi/data1/athletes.txt
  • /c/users/bobbi/data1/managers.txt
  • /c/users/bobbi/data1/owners.txt
  • /c/users/bobbi/data1/teams.txt

Note: If you would like to return the full path to each filename in the current directory, including filenames from subfolders, simply leave out the -maxdepth 1 argument.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Size
Bash: How to List All Files Matching a Specific Pattern
Bash: How to List All Files in Folder Without Extension

Leave a Reply