ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi,
I don't know hot to make this command work:
ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

It should return the list of file .txt
It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions

ls -laR | nawk '/^-/ && /.txt$/ {print $NF}'

ls -laR | grep "^-" | awk '{print $9}'| grep ".txt"

ls -laR  *.txt
ls -lar *.txt

It doen't work correctly.
The files with a space in their names are splitted and the file names are the last words, example:
MacBook-di-Mirko:~ mirko$ sudo ls -laR /Users/mirko/Unix | nawk '/^-/ && /.txt$/ {print $NF}'
Ga.txt
Me.txt
Rhye.txt
file.txt
lettera.txt
README.txt

I had already tryed, but doing so, the output could be:
Readme.txt
folder_txt.png
folder_txt.png
folder_txt.png

maybe is this what y want

find . -type f | grep txt$ | while read line; do echo "${line}";cat "${line}"; done

well... your original version would have had the same issue. This is a limitation of this approach to begin with.
Try or ghostdog74's post above:

find /Users/mirko/Unix -type f -name '*.txt'

I had already tryed, but:
It works using it "stand-alone", for example:
MacBook-di-Mirko:unix mirko$ find /Users/mirko/Unix -type f -name '*.txt'
/Users/mirko/Unix/file.txt
/Users/mirko/Unix/lettera.txt
/Users/mirko/Unix/motion/README.txt
/Users/mirko/Unix/Radio Ga Ga.txt
/Users/mirko/Unix/Save Me.txt
/Users/mirko/Unix/Seven Seas Of Rhye.txt

It doesn't work using it in a for, for example:
MacBook-di-Mirko:unix mirko$ ./test.sh
/Users/mirko/Unix/file.txt
/Users/mirko/Unix/lettera.txt
/Users/mirko/Unix/motion/README.txt
/Users/mirko/Unix/Radio
Ga
Ga.txt
/Users/mirko/Unix/Save
Me.txt
/Users/mirko/Unix/Seven
Seas
Of
Rhye.txt

It split the file names with one or more spaces in their names

PS: the script is:

#!/bin/sh

for file in `find /Users/mirko/Unix -type f -name '*.txt'`
do
	echo ${file}
done

that's why now

find . -type f | grep txt$ | while read line; do echo "${line}";cat "${line}"; done

#!/bin/sh

find /Users/mirko/Unix -type f -name '*.txt' | while read file
do
	echo "[${file}]"
done

There's more than one way to skin a cat :smiley:

As ever, you need to put your variables in double quotes. Just get in the habit of doing that always and you will avoid a lot of surprises.

for file in `find /Users/mirko/Unix -type f -name '*.txt'`
do
	echo "${file}"
done

However, the "for" will also split on whitespace, so you need to do that differently, too.

find /Users/mirko/Unix -type f -name '*.txt' |
while read file; do
  echo "$file"
done

Or, you can fidget with the value of $IFS to make space not be a token separator, but that's not something I particularly recommend.

(There are still some pitfalls. If you had files with backslashes in their names, those would be tricky even with this code.)

what do you want to do with those *.txt files it finds?? I believe you don't just want to echo their names out. If that's the case, no need for a for loop/while loop because the find command displays to stdout. Otherwise, you can use -exec or xargs for simple task, or coupled with while loop for more complicated tasks