How to display the complete path of a file using the ls command in Linux?

Published on Aug. 22, 2023, 12:17 p.m.

To display the complete or absolute path of a file using the ls command in Linux, you can use the -d or -l options with the pwd command or the ${PWD} variable. Here are some examples:

  1. To display the absolute path of a single file:
ls -d "$(pwd)/file_name"

or

ls -ld "${PWD}/file_name"
  1. To display the absolute paths of all files in a directory:
ls -d "$(pwd)/*"

or

ls -ld "${PWD}/"*

Both of these commands will display the absolute path of each file in the current directory (including subdirectories, if any). The -d option tells ls to list the directory itself, rather than its contents, while the -l option provides a long listing of each file, including ownership and permission information. The $(pwd) or ${PWD} variable provides the full path of the current working directory.

Keep in mind that using ls to display file paths is not always the most efficient way, especially when dealing with large directory structures or complex filters. For more complex operations, you may need to use other commands such as find or locate.

Tags:

ls