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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Wildcards used in find, ls and grep commands
# 1  
Old 11-13-2012
[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 Smilie .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
$

# 2  
Old 11-13-2012
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? Smilie
This User Gave Thanks to vbe For This Post:
# 3  
Old 11-13-2012
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 10:53 AM.. Reason: missing /...
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 11-13-2012
THANK YOU VERY MUCH for all this info Jim.
God Bless.
# 5  
Old 11-13-2012
Quote:
Originally Posted by jim mcnamara
. . .
* 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 11:23 AM..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-13-2012
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
# 7  
Old 11-16-2012
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. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find command and use of wildcards

greetings, below is the find command i am using for some filesystem maintenance: find /data/Engine \( -type d -name .snapshot -prune -o -type d -wholename "/data/Engine/*/CAE" \ -prune -o -type d -wholename "/data/Engine/*/CAD" -prune -o -name ".*.case" \)\ -mtime +365 -print0 -fls... (5 Replies)
Discussion started by: crimso
5 Replies

2. Shell Programming and Scripting

Grep multiple patterns that contain wildcards

job_count=`grep -e "The job called .* has finished | The job called .* is running" logfile.txt | wc -l` Any idea how to count those 2 patterns so i have a total count of the finished and running jobs from the log file? If i do either of the patterns its works okay but adding them together... (8 Replies)
Discussion started by: finn
8 Replies

3. Shell Programming and Scripting

[Solved] Grep within find command

Platform: AIX 6.1/ksh Question1. I want to grep for the string "CUSTOM_PKMS" in all the files in server except those files with extensions .dbf , .ctl and .dmp I started running the following command but it is taking too long because there are lots of .dbf , .ctl and .dmp files in this... (6 Replies)
Discussion started by: John K
6 Replies

4. Shell Programming and Scripting

Grep wildcards

Hi all I want to search for number in file presented with wildcard as shown below. cat file.txt 1405 1623 1415 ....... ....... How to search for the number 141526 for example? If the number exist print "Number 141526 exist" if no, print "The number not exist" Thank you in advance. (3 Replies)
Discussion started by: vasil
3 Replies

5. Homework & Coursework Questions

Need help using find or locate with wildcards

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: List all files in ~c12100 directory beginning with "BOZO" that end with either "123" or "456" 2. Relevant... (3 Replies)
Discussion started by: ScarletRavin
3 Replies

6. Homework & Coursework Questions

find grep sed commands homework

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to make as home work several commands with gerp find and sed 2. Relevant commands, code, scripts, algorithms: FIND command -use command find... (8 Replies)
Discussion started by: ViruS89
8 Replies

7. Shell Programming and Scripting

Perl - grep issue in filenames with wildcards

Hi I have 2 directories t1 and t2 with some files in it. I have to see whether the files present in t1 is also there in t2 or not. Currently, both the directories contain the same files as shown below: $ABC.TXT def.txt Now, when I run the below script, it tells def.txt is found,... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

8. UNIX for Dummies Questions & Answers

help with find & grep commands

Folks; First about find: when i run this: find . -name '*log*' -mtime +10 -print | sed 's+^\./++;s+/.*++' | sort -u i got list of log files but also get a directories (although directory names doesn't have "log" in it). How can i exclude the directory from the output of this find command? ... (2 Replies)
Discussion started by: moe2266
2 Replies

9. UNIX for Dummies Questions & Answers

find and grep commands

I'm having trouble with the following commands i. count the number of lines which end in a 4 letter word grep '{4\}$' bfile <<seems to print out everything abc abc abcd joe joe john bob bill gregory greg greg gregory the grep command prints out the lines with 4 letter words and the... (3 Replies)
Discussion started by: StrengthThaDon
3 Replies

10. UNIX for Dummies Questions & Answers

grep and wildcards

Hi guys, a small problem today, I'm grepping a log file containing lines like this below: Mar 09 16:04:00 blabla Mar 09 16:04:02 blabla Mar 09 16:04:05 blabla Mar 09 16:04:15 blabla Mar 09 16:05:06 blabla Mar 09 16:05:23 blabla Mar 09 16:05:25 blabla ... in this file I'm grepping... (5 Replies)
Discussion started by: Lomic
5 Replies
Login or Register to Ask a Question