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
Examples
# 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
Examples
# 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
Examples
# 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.