[Solved] Wildcards used in find, ls and grep commands | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[Solved] Wildcards used in find, ls and grep commands

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-13-2012
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
[Solved] Wildcards used in find, ls and grep commands

Platforms : Solaris 10 and RHEL 5.6

I always get double quotes , single quotes and asteriks mixed up for find, ls and grep commands.

The below commands retrieve the correct results. But , unders stress , I get all these mixed up .So, i wanted to get a clear picture.

Please check if my assumptions mentioned below are correct. The below tests are conducted in Solaris 10

I. find Command
For find command, the wildcard character is asterik and it should be enclosed in double quotes.

Code:
$ touch KLS.dmp
$
$
$
$ find ./ -name "K*.dmp" 2> /dev/null
./KLS.dmp
$
$
$
$
$ find ./ -name "K*.dmp*" 2> /dev/null
./KLS.dmp

-- Single quotes work too
$ find ./ -name 'K*.dmp*' 2> /dev/null
./KLS.dmp

II. grep Command
For grep, the wildcard character is asterik and it should be enclosed in single quotes.

Code:
$ echo "blue skies" > MyFile.txt
$
$
$ cat MyFile.txt
blue skies
$
$
$ grep blu* *.txt
blue skies
$
$
$ grep 'blu*' *.txt
blue skies

III. ls Command

For ls command, wildcard character is again asterik, but don't use single quotes or Double quotes.


Code:
$ ls -alrt M*
-rw-r--r--   1 oracle   oinstall      11 Nov 13 11:37 MyFile.txt
$
$
$
$ ls -alrt "M*"
M*: No such file or directory
$
$ ls -alrt 'M*'
M*: No such file or directory
$

Sponsored Links
    #2  
Old 11-13-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
About grep:

Code:
slo4:/export/home/vbe $ cat file*.txt
Blue skies
blutt
Blott on the landscape
#
slo4:/export/home/vbe $ grep "blu" *.txt  
file001.txt:blutt
slo4:/export/home/vbe $ grep "Blu*" *.txt
file001.txt:Blue skies
file001.txt:Blott on the landscape
slo4:/export/home/vbe $ grep 'Blu*' *.txt
file001.txt:Blue skies
file001.txt:Blott on the landscape
slo4:/export/home/vbe $ grep Blu* *.txt   
file001.txt:Blue skies
file001.txt:Blott on the landscape
slo4:/export/home/vbe $ grep Blu *.txt  
file001.txt:Blue skies
slo4:/export/home/vbe $ uname -sr
SunOS 5.10
slo4:/export/home/vbe $

What were you saying?
The Following User Says Thank You to vbe For This Useful Post:
John K (11-13-2012)
Sponsored Links
    #3  
Old 11-13-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2014, 4:09 PM EDT
Location: NM
Posts: 10,188
Thanks: 275
Thanked 785 Times in 734 Posts
I will try to clear up one thing for you.

The shell does things to certain characters you type. * and ? or ??? or ?????? are all expanded into filenames. BEFORE the command actually runs. This is called globbing.

? expands into all files named with a single character like q
??? expands into all three character file names
?????? all 6 character file names
* expands into all file names in the current directory

The characters are called metacharacters because they do not behave like most things you can type.

You turn off globbing by surrounding the phrase or symbol with the single tic ' or the double quote "

Next.
There are other metacharacters - $ is the prefix for asking the shell to find the value of a variable. Double quotes play unfairly. They let the shell work on all of the $ metacharacters inside double quotes. Not so inside single ticks (or single quotes).

So. grep uses regular expressions. *,?, and $ are used in regexes all the time.
You use single tics to block them off from the shell's evil influence. Things would work out fine with grep "pattern" if the pattern had no $ int it. In fact

Code:
grep me myfile

works great to find the string==me. No quotes at all.

Code:
grep "me and you" myfile

needs double quotes to me and you to force it into a SINGLE pattern, again turning off the evil influence of the shell which would otherwise make it TWO patterns. And confuse the heck out of grep.

Code:
 grep -F  "*" somefilename

uses -F to turn off regular expressions for grep.
This is how you look for a metacharacter in a file. But you still have to stop the evil influence of the shell. " " do that for the * character. But not the $ character.

' ' single tics make the stuff inside completely immune to the shell.
" " gives partial immunity to to the shell.

Finally, since all ls cares about is file names, letting the shell mess with metacharacters is cool.

Last edited by vbe; 11-13-2012 at 09:53 AM.. Reason: missing /...
The Following User Says Thank You to jim mcnamara For This Useful Post:
John K (11-13-2012)
    #4  
Old 11-13-2012
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
THANK YOU VERY MUCH for all this info Jim.
God Bless.
Sponsored Links
    #5  
Old 11-13-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 2 September 2014, 6:26 PM EDT
Location: Aachen, Germany
Posts: 4,164
Thanks: 69
Thanked 995 Times in 943 Posts
Quote:
Originally Posted by jim mcnamara View Post
. . .
* expands into all file names in the current directory
. . .
May I humbly add onto Jim's explanations: above is true except for filenames starting with "." (a period, so called hidden files)

Last edited by RudiC; 11-13-2012 at 10:23 AM..
The Following User Says Thank You to RudiC For This Useful Post:
John K (11-13-2012)
Sponsored Links
    #6  
Old 11-13-2012
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
In Solaris 10 (x86-64 )
-F option with grep seems to have some issue.


Code:
$ echo "HELLO WORLD * GREEN" > MyNewFile.txt
$
$
$ cat MyNewFile.txt
HELLO WORLD * GREEN
$
$ grep -F "*" MyNewFile.txt
grep: illegal option -- F
Usage: grep -hblcnsviw pattern file . . .

$ grep -F "*" *.txt
grep: illegal option -- F
Usage: grep -hblcnsviw pattern file . . .


-- version info
$ uname -a
SunOS hewspike214 5.10 Generic_147441-01 i86pc i386 i86pc

$ cat /etc/release
                    Oracle Solaris 10 8/11 s10x_u10wos_17b X86
  Copyright (c) 1983, 2011, Oracle and/or its affiliates. All rights reserved.
                            Assembled 23 August 2011

But solaris's man page on grep does mention -F

Code:
-F                 Matches using fixed strings. Treats  each
                        pattern  specified as a string instead of
                        a regular expression. If  an  input  line
                        contains  any  of  the patterns as a con-
                        tiguous sequence of bytes,  the  line  is
                        matched.  A  null  string  matches  every
                        line. See fgrep(1) for more information.

-F option for grep works fine with RHEL 5.4
Sponsored Links
    #7  
Old 11-16-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
Quote:
User Commands grep(1)

NAME
grep - search a file for a pattern

SYNOPSIS
/usr/bin/grep [-bchilnsvw] limited-regular-expression
[filename]...

/usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] -e pattern_list...
[-f pattern_file]... [file]...

/usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx]
[-e pattern_list]... -f pattern_file... [file]...

/usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] pattern
[file]...

Code:
#demo:
slo4:/export/home/vbe $ grep -?
grep: illegal option -- ?
Usage: grep -hblcnsviw pattern file . . .
slo4:/export/home/vbe $ which grep
/bin/grep
slo4:/export/home/vbe $ /usr/xpg4/bin/grep -?
/usr/xpg4/bin/grep: illegal option -- ?
Usage:  grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
        grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]
slo4:/export/home/vbe $

In order to make use of grep -F you have to use /usr/xpg4/bin/grep, instead of the default grep in /usr/bin.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep wildcards vasil Shell Programming and Scripting 3 06-11-2012 07:44 AM
find grep sed commands homework ViruS89 Homework & Coursework Questions 8 02-12-2011 03:46 PM
help with find & grep commands moe2266 UNIX for Dummies Questions & Answers 2 07-25-2007 03:18 PM
find and grep commands StrengthThaDon UNIX for Dummies Questions & Answers 3 03-18-2007 09:14 PM
grep and wildcards Lomic UNIX for Dummies Questions & Answers 5 03-11-2005 04:02 AM



All times are GMT -4. The time now is 10:09 PM.