List files recursively in Linux CLI with path relative to the current directory
This is similar to this question, but I want to include the path relative to the current directory in unix. If I do the following:
ls -LR | grep .txt
It doesn't include the full paths. For example, I have the following directory structure:
test1/file.txt
test2/file1.txt
test2/file2.txt
The code above will return:
file.txt
file1.txt
file2.txt
How can I get it to include the paths relative to the current directory using standard Unix commands?
Use find:
find . -name \*.txt -print
On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.
Use tree
, with -f
(full path) and -i
(no indentation lines):
tree -if --noreport .
tree -if --noreport directory/
You can then use grep
to filter out the ones you want.
If the command is not found, you can install it:
Type following command to install tree command on RHEL/CentOS and Fedora linux:
# yum install tree -y
If you are using Debian/Ubuntu, Mint Linux type following command in your terminal:
$ sudo apt-get install tree -y
Try find
. You can look it up exactly in the man page, but it's sorta like this:
find [start directory] -name [what to find]
so for your example
find . -name "*.txt"
should give you what you want.