Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I don't know tcsh, but according to a short web search it uses backticks, not $().
Now why backticks don't work in your case is hard to say, because you don't tell us what you mean by "doesn't work". What do you expect to happen when you enter the command, and what happens? Do you get error messages? Are you sure there are regular files in this directory structure?
i don't know about tcsh per se, but I always use single (regular) quotes here
Code:
# so not
-name "*"
# but
-name '*'
to prevent interpolation by the shell & instead force the find cmd to handle the '*'.
This is useful generally (& what you really mean ) and in particular solves the 'Too many args' error in dirs with very large nums of files.
the following worked before, but not anymore on tcsh.
> grep abc `find . -name "*" -type f -print`
It seems strange that there are reports of it having worked. Normally you'd have to pipe the output of find into grep
Code:
find . -type f -print | grep 'abc'
However, you can offload the work onto find and omit the grep which is then redundant.
Code:
find . -type f -name '*abc*' -print
Remember that there is an implied logical AND between each option unless an OR is specified explicitly. Also, find can do certain type of regular expression pattern matching, though not up to the level which grep can. See "man find".
I'm not sure about it. On my system, they both search in dotfiles. ack also searches in most dotfiles, but ignores some of them, and the list is configurable:
Quote:
The default options for ack ignore certain files and directories. These include:
Backup files: Files matching #*# or ending with ~.
I also can't explain the subtleties as to why it doesn't work, or if it should not have previously worked.
Isn't what you wrote (other forms also shown) the same as?
Code:
$ find -type f "*" -exec grep abc {} /dev/null \;
I'd probably use sudo due to the wildcard there, but just style differences. I guess my thinking is that there have been so many changes to things over the years, I go with the flow, and however a shell behaves is just something I deal with, the various times I encounter a uniqueness such as this. I'm neither a shell designer, or much into shell programming, I just use some scripts when they help, or use the shell as best as I can. And it's been so long since I've had something other than bash.
I did some more experiments and found some interesting behaviors.
First, whether being on bash or tcsh doesn't seem to be a primary cause of the problem. Even on tcsh, the behavior varies.
The followings are experiments I did.
All were done under the directory, /tmp/ICC2/REF/scripts and its subdirectory(you can see the current directory at the shell prompt) on tcsh.
The 1st one doesn't return any matching result, but when I change dir to one of the subdirectories and run the same command, it finds matching lines.
$> /tmp/ICC2/REF/scripts : grep host `find . -name "*" -type f -print`
grep: No match.
$> /tmp/ICC2/REF/scripts : cd N7_H240_reference_script
THIs is on a company system and I have no control over when and what to be installed or upgraded, so something must have been changed with the system, but the inconsistent results bother me.
does not work with special characters in filenames. The shell breaks space characters into different arguments, and expands wildcards * ? [ ]
Further it can overflow with "too many arguments".
Better take the robust version (from shruggy)
Code:
find . -name "*" -type f -exec grep -H abc {} +
Now find directly passes the filenames to grep without involving the shell.
Also correct but slower is (what rtmistler attempted)
Code:
find . -name "*" -type f -exec grep -H abc {} \;
Last edited by MadeInGermany; 07-09-2020 at 06:02 PM.
The 1st one doesn't return any matching result, but when I change dir to one of the subdirectories and run the same command, it finds matching lines.
As a first step to solve this, I would look at the two outputs of the find command to see if they contain anything that might change grep's behaviour, such as file names that start with a dash.
Or more realistically, perhaps the output of the first find is too long, though if it were bash I'd expect to see an error message in this case.
I would say you can simply omit -name "*".
Saying backtick does not work is just wrong. It does work as it is expected, also find and grep works. The problem is that your oneliner does not do what you expected.
Most probably because something has been changed in that directory [somewhere inside].
As it was mentioned check what was found by that find command - without grep (if the result was what you need).
Found a cause.
It was because one of the files in the directory tree the find command searches through was named with special characters and i think when grep command was run on that file, it went wrong and produced no result.
Last edited by lostinxlation; 07-12-2020 at 01:09 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.