LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   * expansion (https://www.linuxquestions.org/questions/linux-newbie-8/%2A-expansion-4175440849/)

prasunjit 12-10-2012 01:37 PM

* expansion
 
Hello

i have two text files in the current directory: lserror.txt lsouput1.txt

while executing the below command to find files without double quotes as "*.txt" it is throwing error
find . -type f -name *.txt
But the below command without double quotes as "*.txt" is giving correct result
ls *.txt

pls explain

colucix 12-10-2012 01:43 PM

You have to pass a literal asterisk to the find command, otherwise the shell expansion brings to a wrong command line. Since the .txt files are in the current working directory, the shell substitutes their name in the command line, givin'
Code:

find . -type f -name lserror.txt lsouput1.txt
which is wrong. On the other hand, two (or more) arguments are accepted by the ls command, so that the shell substitution
Code:

ls lserror.txt lsouput1.txt
bring to a correct command line. To pass a literal * to find, either escape it or use single or double quotes:
Code:

find . -type f -name \*.txt
find . -type f -name '*.txt'
find . -type f -name "*.txt"

All the lines above are equivalent.

prasunjit 12-11-2012 11:55 PM

@colucix..thnks for the stuff..
But it seems all the below commands are going to recursion giving text files within sub directories as well...

f u can help

Quote:

Originally Posted by colucix (Post 4846447)
You have to pass a literal asterisk to the find command, otherwise the shell expansion brings to a wrong command line. Since the .txt files are in the current working directory, the shell substitutes their name in the command line, givin'
Code:

find . -type f -name lserror.txt lsouput1.txt
which is wrong. On the other hand, two (or more) arguments are accepted by the ls command, so that the shell substitution
Code:

ls lserror.txt lsouput1.txt
bring to a correct command line. To pass a literal * to find, either escape it or use single or double quotes:
Code:

find . -type f -name \*.txt
find . -type f -name '*.txt'
find . -type f -name "*.txt"

All the lines above are equivalent.


colucix 12-12-2012 12:30 AM

find is recursive by its nature, but you can limit the level of recursion (that is how much it descends into subdirectories) using -maxdepth (and eventually the related -mindepth) expression. In order to limit the search to the current working directory, you can try:
Code:

find . -maxdepth 1 -name \*.txt
Anyway, in this case is more convenient to use
Code:

ls *.txt
unless you want to use some more strict search criteria or apply some action on the results by means of the find command facilities.

prasunjit 12-12-2012 07:10 AM

Thanks a lot....this was really helpful.

Quote:

Originally Posted by colucix (Post 4847486)
find is recursive by its nature, but you can limit the level of recursion (that is how much it descends into subdirectories) using -maxdepth (and eventually the related -mindepth) expression. In order to limit the search to the current working directory, you can try:
Code:

find . -maxdepth 1 -name \*.txt
Anyway, in this case is more convenient to use
Code:

ls *.txt
unless you want to use some more strict search criteria or apply some action on the results by means of the find command facilities.



All times are GMT -5. The time now is 02:02 PM.