LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-08-2020, 10:01 PM   #1
lostinxlation
LQ Newbie
 
Registered: Dec 2009
Location: San Jose
Distribution: Slackware, Debian
Posts: 22

Rep: Reputation: 1
backticks not working anymore


hi
I have been using `command` (with backticks) heavily for many years, but I recently discovered it didn't work anymore.

the following worked before, but not anymore on tcsh.
> grep abc `find . -name "*" -type f -print`


Q&As on some websites say `command` and $(command) are same, and I tried grep abc $(find . -name "*" -print), but it didn't work either.

What's the alternative of `command` on tcsh ?
 
Old 07-08-2020, 10:48 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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?
 
Old 07-09-2020, 12:54 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 07-09-2020, 01:35 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by lostinxlation View Post
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".
 
Old 07-09-2020, 03:07 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Turbocapitalist View Post
Code:
find . -type f -name '*abc*' -print
But this:
Code:
grep abc `find . -name "*" -type f -print`
is quite valid and something else. It means grepping abc in all the files found.
 
Old 07-09-2020, 03:16 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
IMO, the -exec action of find is the right way to do it:
Code:
find -name \* -type f -exec grep -H abc {} +
But in this particular case, grep by itself will also do:
Code:
grep -r abc *
Although I would rather use ack or ag for this.

Last edited by shruggy; 07-09-2020 at 03:25 AM.
 
Old 07-09-2020, 03:47 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by shruggy View Post
But in this particular case, grep by itself will also do:
Code:
grep -r abc *
Not quite. It includes files whose names start with a dot, whereas the find command doesn't.
 
Old 07-09-2020, 04:34 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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 ~.
  • Coredumps: Files matching core.\d+
  • Version control directories like .svn and .git.

Last edited by shruggy; 07-09-2020 at 04:57 AM.
 
Old 07-09-2020, 07:11 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 07-09-2020, 05:04 PM   #10
lostinxlation
LQ Newbie
 
Registered: Dec 2009
Location: San Jose
Distribution: Slackware, Debian
Posts: 22

Original Poster
Rep: Reputation: 1
Thank you all.

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

$> /tmp/ICC2/REF/scripts/N7_H240_reference_script : grep host `find . -name "*" -type f -print`
./scr/project_setup.tcl:set_host_options -max_core 8
./scr/common_setting.tcl:set_host_options -max_core 8
./scr/a/b:host host




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.
 
Old 07-09-2020, 06:00 PM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
grep -H abc `find . -name "*" -type f`
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.
 
Old 07-09-2020, 06:24 PM   #12
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by lostinxlation View Post
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.
 
Old 07-10-2020, 03:08 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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).
 
Old 07-12-2020, 12:55 AM   #14
lostinxlation
LQ Newbie
 
Registered: Dec 2009
Location: San Jose
Distribution: Slackware, Debian
Posts: 22

Original Poster
Rep: Reputation: 1
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.
 
1 members found this post helpful.
Old 07-12-2020, 04:17 PM   #15
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by lostinxlation View Post
the following worked before, but not anymore on tcsh.
> grep abc `find . -name "*" -type f -print`
I'd be surprised if this command didn't routinely result in an "grep: Argument list too long" error.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] cron not liking ?backticks, ?pipes ?head exp compused Linux - Software 5 07-28-2017 12:19 AM
[SOLVED] SSH and Backticks Not Executing the Remote Command metallica1973 Programming 1 05-13-2012 12:18 AM
NIC not installed anymore, KDE not installed anymore, LILO duplicate Volume ID error scottad Slackware 2 03-31-2012 12:13 AM
Perl: Not all output is captured by backticks Poetics Programming 2 07-31-2007 03:40 PM
Perl not running backticks within chroot jail Consul Linux - Security 2 08-02-2005 05:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration