Recursively Listing Files with Relative Paths in Linux Command Line

Nov 21, 2025 · Programming · 11 views · 7.8

Keywords: Linux | recursive file listing | relative paths | find command | tree command | command line tools

Abstract: This article provides an in-depth exploration of methods for recursively listing files while displaying their paths relative to the current directory in Linux command line environments. By analyzing the limitations of the ls command, it focuses on the find command solution, including basic syntax, parameter explanations, and practical application examples. The article also compares the tree command as an alternative approach, offering complete code examples and operational guidance to help readers deeply understand core concepts of filesystem traversal and path handling.

Problem Background and Requirements Analysis

In Linux and Unix system administration, there is often a need to recursively traverse directory structures and list specific types of files. However, the standard ls -LR command, while capable of recursive file listing, does not include complete path information in its output, displaying only filenames. This output format creates incomplete information when dealing with nested directory structures, making it impossible to accurately identify the specific location of files.

Consider the following directory structure example:

test1/file.txt
test2/file1.txt
test2/file2.txt

After using ls -LR | grep .txt command, the output becomes:

file.txt
file1.txt
file2.txt

From this output, it's impossible to distinguish which subdirectories these files belong to, creating significant information gaps when handling complex directory structures.

Core Solution: The find Command

The find command is a powerful tool in Unix/Linux systems for searching files, capable of recursively traversing directory trees and performing various operations. For the problem discussed in this article, the simplest solution is:

find . -name \*.txt -print

Breakdown of this command's components:

In most GNU/Linux distributions, using GNU find tools, the -print option can be omitted since it's the default action:

find . -name \*.txt

After executing the above command, the output will include complete relative paths:

./test1/file.txt
./test2/file1.txt
./test2/file2.txt

Advanced Usage of find Command

The find command supports rich matching conditions and operation options, making it the Swiss Army knife of filesystem management.

File Type Filtering

Beyond filename pattern matching, filtering by file type is also possible:

find . -type f -name \*.txt

Where -type f ensures only regular files are matched, excluding directories and symbolic links.

Multiple Condition Combinations

find supports logical operators to combine multiple conditions:

find . -name \*.txt -o -name \*.md

This command matches all .txt or .md files. The -o represents logical OR operation.

Depth Control

Search depth can be controlled using -maxdepth and -mindepth options:

find . -maxdepth 2 -name \*.txt

This command searches only in the current directory and its immediate subdirectories, without going deeper into the directory tree.

Alternative Approach: The tree Command

While find command is the standard solution, the tree command provides another visual option. Using tree -if --noreport generates a complete list of file paths:

tree -if --noreport .
tree -if --noreport directory/

Parameter explanations:

Output example:

./test1/file.txt
./test2/file1.txt
./test2/file2.txt

Installing tree Command

The tree command may require manual installation on some systems:

On RHEL/CentOS/Fedora systems:

yum install tree -y

On Debian/Ubuntu/Mint systems:

sudo apt-get install tree -y

Performance and Applicability Comparison

The find command, as a Unix standard tool, is available on all Unix-like systems, offers high execution efficiency, and provides powerful functionality. While the tree command offers more user-friendly output formatting, it requires additional installation and may have slightly inferior performance compared to find when processing extremely large directories.

For simple file listing requirements, find . -name \*.txt is the most direct and effective solution. For more complex filtering conditions or subsequent processing, the rich options of the find command provide greater flexibility.

Practical Application Examples

Here are some common usage scenarios and corresponding commands:

Find all Python files in current directory and subdirectories:

find . -name \*.py

Find text files modified within the last 7 days:

find . -name \*.txt -mtime -7

Save found file paths to a file:

find . -name \*.txt > file_list.txt

These examples demonstrate the flexible application of the find command in practical work, where combining different options can meet various complex file search requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.