The brevity of relative paths can be convenient and save you some typing, but sometimes, having quick access to the absolute paths of a directory's objects is desired. The find
command is one great way to get this information.
Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.
Absolute Paths For a Directory Tree
By default, find
is recursive and prints the paths of all file system objects in the specified directory tree. Whether it prints absolute or relative paths is determined by how you specify the directory tree to search.
So, if we want to view absolute paths for the tree starting at the current directory, we can do:
find "$(pwd)"
Above, we use command substitution and the pwd
command to print the absolute path of the working (current) directory, which is used as the directory tree to search for the find
command.
Occasionally, this is can be overkill and we just want the absolute paths for the objects in the current directory. We can specify this by adding -maxdepth 1
to our command:
find "$(pwd)" -maxdepth 1
By default, this command includes the absolute path of the current directory itself. If this is undesirable, we can add -mindepth 1
to our command, as well:
find "$(pwd)" -maxdepth 1 -mindepth -1
If we want to target a different directory tree, we can swap out "$(pwd)"
with the absolute path of the directory of interest.
Absolute Paths For Specific Object Types
Narrowing down a search to specific object types is a task that find
is well suited for. By using -type
, find
can filter our results to display the absolute paths for many potential file system object types.
Several examples include:
find "$(pwd)" -type b
- Block devices
find "$(pwd)" -type d
- Directories
find "$(pwd)" -type f
- Files
find "$(pwd)" -type l
- Symbolic links
find "$(pwd)" -type s
- Sockets
Absolute Paths For Specifically Named Objects
Our find
commands can be adjusted to target objects with specific names, too. For example, the following returns absolute paths for files that end with the .csv
extension:
find "$(pwd)" -type f -name '*.csv'
This command targets directories that start with the string py_
:
find "$(pwd)" -type d -name 'py_*'
Having Fun With find
find
's power and complexity may make you think that it is a tool that should only be utilized for the most advanced searches. In reality, its flexibility means that it can be used for mundane (but very handy) tasks, as well.