Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-20-2004, 01:02 PM   #1
LQ Newbie
Registered: Sep 2004
Location: malaysia
Posts: 7

Rep: Reputation: 0
purpose of symbol ^

command to get the count of filenames start with "Foo" and end with an odd digit is:
ls -1 | grep '^Foo.*[13579]' | wc -l

what is the purpose of '-1' , '^', and '.' ?
will this command work if i don't put any one of these symbol?
Old 09-20-2004, 01:07 PM   #2
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
The -1 argument to ls lists one file per line. For the rest. you need an understanding of how regular expressions (regexps) work, which is not necessarily that easy. Basically the ^ is negation or beginning of the line, depending on how its used in the regexp. In your case, it's the beginning of the line, so it's telling grep to match everything that starts with the pattern. The . is a single character wildcard. Combined with * the multi character wildcard, it basically says match everything. The command will still "work" if you change things, but it will match different patterns, so the resukts won't be the same. Read a tutorial on regexps for more info, they're way too complicated to explain in a forum post.
Old 09-20-2004, 01:12 PM   #3
LQ Newbie
Registered: Sep 2004
Location: malaysia
Posts: 7

Original Poster
Rep: Reputation: 0
Ok. Thank you...
Old 09-20-2004, 02:14 PM   #4
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
More info:

'ls -1' is used because you want to count files. If you used plain old 'ls' then you wouldn't get one file per line, so you couldn't pipe the output to the 'wc' command and tell it to count lines.

For the pattern '^Foo.*[13579]':

^Foo matches any line that starts with 'Foo' - the ^ means "start of the line" in this case

The single period means "any character", the * means "0 or more of the preceeding character", so .* means "0 or more of any character". This matches anything in the filename after Foo and before the next pattern.

Characters in braces - the [] - match any single character that is inside the braces, so [13579] matches any odd number.

If you took out the ^ and the .* you'd end up with a pattern that matched files with names that contained "Foo" immediately followed by a single odd number, like "BooFoo9". It wouldn't limit the match to files that started with "Foo" and it wouldn't match any file that had other characters between the "Foo" and the number, like "FooBar9".


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
snort failed: snort: symbol lookup error: undefined symbol: usmAES192PrivProtocol Emmanuel_uk Linux - Security 1 07-10-2005 11:29 AM
what distro for my purpose mht Linux - Newbie 2 07-06-2005 11:29 PM
What is the purpose of LiveCDs? john_walsh54 Linux - Newbie 4 04-05-2005 01:12 AM
Purpose of HCL the_imax LQ Suggestions & Feedback 6 01-11-2005 06:31 AM
Purpose of some directories Menestrel Linux - Newbie 2 01-08-2005 06:47 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:29 AM.

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