LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with find command (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-find-command-517608/)

mrinal.kant 01-09-2007 02:15 AM

problem with find command
 
find . -name *.lex

This command lists the output from the current directory as expected, but not from the subdirectories. Also, the command shows error in running from any of the subdirectories.

Why?

bigrigdriver 01-09-2007 02:21 AM

find . -name: the dot says "this directory". It's that simple. If you want it to recurse into sub-directories, replace the dot (.) with the directory name, such as:

find ~ -name *.lex, where ~ represents your home directory

or

find /home/<username> -name *.lex

If you wish to find in directories other than your home directory, expect errors because you may not have permission to read some of them. Su to root, then run the find command.

jschiwal 01-09-2007 02:27 AM

That command should be:
find . -name "*.lex"

mrinal.kant 01-09-2007 02:40 AM

Thanx jschiwal.
It works.

timmeke 01-09-2007 03:31 AM

To explain why you see this behaviour:
*.lex is expanded by the shell to all filenames with extension .lex in the current directory, after which the result is passed to "find".

So, if you have a directory with files "1.lex" and "2.lex", the parameters that "find" gets
are
find . -name 1.lex 2.lex
which is clearly not what you want.

By quoting the "*.lex" (either single or double quotes), you prevent the filename/wildcard expansion.
This means that "find" then gets the parameter "*.lex" literally, which it then uses to filter the names of objects (files/dirs) found.

Quote:

Originally Posted by bigrigdriver
find . -name: the dot says "this directory". It's that simple. If you want it to recurse into sub-directories, replace the dot (.) with the directory name, such as:
find ~ -name *.lex, where ~ represents your home directory

Sorry, but I disagree with this. "." is a valid directory and doesn't need replacing. "find" always recurses into sub-directories of a given directory (be that ".", ~/, /home/you/ or whatever), unless you limit it using the "-maxdepth" option.


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