Search code examples
linuxbashawkgrepls

Search directory and subdirectories for file matching a string and listing paths using ls and grep instead of find


I know there is a nice Linux program called find, which used as

find /home/user -name ".txt"

will print all the files with .txt extension with their relative path

How can I achieve the same using ls, grep and possibly awk?

I tried listing all files in directory and its subdirectories recursively using

ls -LR1

but have no idea how to parse it using grep.

Any advice would be greatly appraciated.


Solution

  • You can do this with and :

    shopt -s globstar # required if not enabled with bash4
    printf '%s\n' **/*.txt
    

    But don't trust anyone finding a solution with ls + awk, not the good way to achieve this with robustness