ls Full Path

While the ls command is excellent for listing files and directories, it doesn't directly provide their full, absolute paths. This guide explores various methods to achieve this, which is particularly useful for scripting, automation, and unambiguous file referencing.

Methods to Get Full Paths

Since ls itself doesn't have a dedicated option for printing full paths, we often combine it with other commands or use alternative tools.

1. Using pwd and ls (for current directory)

This method is simple for listing files in the current directory with their full paths.

Syntax

pwd && ls -d $PWD/*

Examples

pwd && ls -d $PWD/*
# Example Output: # /home/user/documents # /home/user/documents/file1.txt # /home/user/documents/folder_a

This will first print the current directory, then list the full paths of all files and subdirectories within it.

2. Using the find command (more robust)

The find command is more powerful for searching and can easily print full paths, including for files in subdirectories.

Syntax

find <start_directory> -print

Examples

find /home/user/documents -print
# Example Output: # /home/user/documents # /home/user/documents/file1.txt # /home/user/documents/folder_a # /home/user/documents/folder_a/subfile.txt

Lists all files and directories recursively from the specified starting directory with their full paths.

3. Using realpath with find and xargs (canonical paths)

For canonicalized absolute pathnames (resolving symlinks and ./..), combine find with realpath.

Syntax

find <start_directory> -print0 | xargs -0 realpath

Examples

find . -maxdepth 1 -print0 | xargs -0 realpath
# Example Output: # /home/user/current_dir/file1.txt # /home/user/current_dir/folder_a

This command lists the absolute, resolved paths of all files and directories in the current directory (.), excluding subdirectories' contents.

See also