LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 11-18-2021, 01:39 AM   #1
cybernewbie727
LQ Newbie
 
Registered: Nov 2021
Posts: 4

Rep: Reputation: Disabled
command: find and command: grep question


Hello,
I'm not sure if I am posting on the right subsection because I am new to this community and also Linux in general. I am learning the find and grep command I have a question regarding placement of wildcards. When using a wildcard in find, I am to place the wildcard directly before and after (ex: find *txt). But when I am using grep, why do I place the wildcard after a space? (ex: grep .txt *)

Many thanks!
 
Old 11-18-2021, 02:13 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Hi, and welcome here, at LQ
the command line arguments (what you add/append to the command) depends on the command you use, because the command itself will process that.
So you need to check man find to understand what find *txt means and man grep to know how grep uses those arguments.
For example the usual case: grep pattern file, so .txt is a pattern here.

Arguments are separated by a space (or more), so *txt and .txt * are two different things.
Additionally * can be evaluated by the shell (see globs), so *txt and * can be replaced before execution.
 
Old 11-18-2021, 02:47 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,358
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by pan64 View Post
Arguments are separated by a space (or more), so *txt and .txt * are two different things.
Additionally * can be evaluated by the shell (see globs), so *txt and * can be replaced before execution.
1) To expand on that, no pun intended, "wildcards" in that context will either take effect in the shell and thus be globbing (see "man 7 glob") or take place in find as various flavors of regular expression aka "regex". So any special characters, like the asterisk *, which might get interpreted by the shell need to be enclosed in quotes to have the shell pass them intact to find.

See https://mywiki.wooledge.org/glob and https://mywiki.wooledge.org/Quotes

Contrast:

Code:
find /home/cybernewbie/ -type f -name '*.txt' -print

find /home/cybernewbie/ -type f -name *.txt -print
In the latter, the shell interface gets hold of the asterisk first and tries to interpret it before passing it to find.

The same applies to grep, though the focus at any given time with it is on one of three flavors of regular expression: POSIX, Extended, or Perl-Compatible.

2) as for the different types of regular expression:

https://www.regular-expressions.info/posix.html

https://perldoc.perl.org/perlre
 
Old 11-18-2021, 03:22 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,152

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
That all should help confuse things no end ...

Down the rabbit hole you go ...
 
Old 11-18-2021, 01:10 PM   #5
cybernewbie727
LQ Newbie
 
Registered: Nov 2021
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you everyone who replied. I think I should really get myself more acquainted with the man page. It's intimidating but I think it will be really beneficial if I learn how to read and process the man pages.
Thank you!!
 
Old 11-18-2021, 04:35 PM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by cybernewbie727 View Post
It's intimidating but I think it will be really beneficial if I learn how to read and process the man pages.
Definitely - being able to comprehend man pages isn't always as straight-forward as it should be, but is a useful skill to develop.

Also useful is skipping to the the "See Also" section to check if there's additional documentation: for example, grep has online + downloadable manuals which are both more thorough and easier to follow than the man page.

 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Find & grep - how to return pathes, not grep phrases ? postcd Linux - General 2 11-25-2014 12:43 PM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 08:57 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