0

I use this to list all text files in d:\ root:
ls d:\*.txt
This to list all text files in all sub-directories:
ls d:\*\*.txt

How can I list all text files in the root AND in all sub-directories using ls?

This doesn't work:
ls d:/{,**/}*.txt

edit: in ls not in find, grep, awk, sed or whatever other search command

Reman
  • 7,931
  • 11
  • 55
  • 97
  • Possible duplicate of [List files recursively in Linux CLI with path relative to the current directory](https://stackoverflow.com/questions/245698/list-files-recursively-in-linux-cli-with-path-relative-to-the-current-directory) – nortontgueno Feb 12 '19 at 19:50
  • Why is this tagged `linux`, but using paths that look like `d:\....`?? – William Pursell Feb 12 '19 at 20:09
  • NO duplicate! This is about 'ls'. I know there is the 'find' and grep command but in my question I write about 'ls'. :( – Reman Feb 12 '19 at 20:09
  • @WilliamPursell, good question. I'm using windows subsystem for linux (=linux on windows 10). – Reman Feb 12 '19 at 20:14
  • @jww I know. I doubted in the beginning. Could not find a better site. – Reman Feb 15 '19 at 10:47

1 Answers1

1

You can try ls **/*.txt. This is worked in zsh.

or

ls $(START_DIR)/**/*.txt

J.Z
  • 411
  • 3
  • 14
  • Yes that works but there is no way to indicate the d:\ directory (the start directory)? --> Edit: It doesn't work.. It doesn't include the text files in root. – Reman Feb 12 '19 at 20:11
  • just append the start directory in front. ex. ls ~/home/*/**/*.txt – J.Z Feb 12 '19 at 20:15
  • Sorry, it doesn't work. I made a mistake. It doesn't include the text files in root. – Reman Feb 12 '19 at 20:17
  • 1
    @Reman Which shell are you doing this in? Bash requires you to `shopt -s globstar` first – that other guy Feb 12 '19 at 20:40
  • @thatotherguy the default Windows 10 Shell Command and sometimes the debian bash shell – Reman Feb 12 '19 at 20:59
  • 1
    They do not share syntax, even if some commands are similar. Please choose one to ask about. – that other guy Feb 12 '19 at 21:02
  • @J.Z `ls ~/home/*/**/*.txt` does only find .txt files in the 2nd subdirectories (the subdirectory of a subdirectory). Is there no way to indicate to `ls` that all subdirectories need to be searched? – Reman Feb 13 '19 at 13:53
  • @thatotherguy Windows shell is my default shell – Reman Feb 13 '19 at 13:56
  • @J.Z gives an error...~/home/**/*.txt: No such file or directory – Reman Feb 13 '19 at 15:53
  • In bash or zsh, this error will display if there is no matching result. Is there any .txt file under your ~/home directory or its subdirectories? – J.Z Feb 13 '19 at 19:02
  • @J.Z This (home) directory doesn't exist on my system. I've read these days more and more about `ls`. It seems not to be possible using `ls` to scan all sub directories. Many point to `find.exe` to scan all subdirectories. – Reman Feb 15 '19 at 10:42