regex - why the ls -R (recursing down) doesn't work with regular expression -


in case, directory tree following

 [peter@centos6 a]$ tree   .   ├── 2.txt   └── b       └── 1.txt  1 directory, 2 files 

why following 2 command 2.txt?

 [peter@centos6 a]$ ls -r *.txt 2.txt [peter@centos6 a]$ ls -r | grep *.txt 2.txt 

it isn't clear if asking "why" meaning "explain output" or "how should done". steephen has answered latter, answer former.

the reason called "shell expansion". when type *.txt in command line, program doesn't parameter, rather shell expands , passes results.

*.txt expands "all files in current directory arbitrarily many symbols in beginning, ending '.txt' , not starting '.'".

this means when type "ls -r *.txt" command executes "ls -r 2.txt"; , when "ls -r | grep *.txt" executes "ls -r | grep 2.txt".

this exact reason why steephen has put quotation marks around wildcard in answer provided. necessary stop expansion. in fact single quotes or placing slash before special character. of following work:

find . -name "*.txt" 

or

find . -name '*.txt' 

or

find . -name \*.txt 

Comments