LinuxQuestions.org
Visit Jeremy's Blog.
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 03-01-2011, 05:26 AM   #1
sublime188
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Rep: Reputation: 0
grep search help


I am trying find files in a directory that contain numbers.
I have tried ls /etc *[0-9]* but that doesn't work.
If I cd to /etc and run ls *[0-9]* it almost works but it also includes results from within files. My last thought was to try: find /etc [0-9] -type f but this does not work either.

My second problem is that I am trying to get list of files in a directory that were changed less than 10 hours ago, using grep, while leaving out directories.

I am completely stuck with the second problem. Any help will be appreciated.
 
Old 03-01-2011, 05:29 AM   #2
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Grep has a flag -E for regular expressions. Find has a flag (dont remember it right now) for access time.
 
1 members found this post helpful.
Old 03-01-2011, 06:37 AM   #3
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Rep: Reputation: 48
you want file whose name contain number present only in current folder
then
Code:
find . -maxdepth 1 -type f | grep -E [0-9]
ls *[0-9]*
will actually print all files present in current folder & subfolders , not deeper.
I don't think ls will do any regular expression filtering . * means everything for ls.
 
1 members found this post helpful.
Old 03-01-2011, 07:05 AM   #4
sublime188
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Much thanks to both of you who replied, sumeet that command worked brilliantly : )

For my second problem I solved it using find with:
find /home/usrname –type f -mmin -600

Is there anyway to accomplish this using grep?
 
Old 03-01-2011, 08:49 AM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Rep: Reputation: 48
I don't think so
grep can process contents of files
while
'find' is a powerful tool to locate desired files.

You can also create database of your files on system so that you don't have to scan the hard disk again for finding file with desired pattern in name.
Code:
updatedb #do as root
Now you can use
Code:
locate [options] [pattern] # see man locate for details
 
1 members found this post helpful.
Old 03-01-2011, 09:16 AM   #6
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Rep: Reputation: 0
Very good. I find this very useful for me.
Thank you.
 
Old 03-01-2011, 09:46 AM   #7
sublime188
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Ok, I just thought of one more problem.

How can you specify a .file (dot file) without including the dot as the first character in the glob?

For example ls *bash* and ls ?bash* gives an error "no such file or directory" but ls .bash* gives results.


There is a method of shopt -s dotglob but this method does not serve my purpose.
I know there is some other way, I just can't remember what it is

Again, thanks to all who are helping with this.

Last edited by sublime188; 03-01-2011 at 10:00 AM.
 
Old 03-01-2011, 06:39 PM   #8
Ignotum Per Ignotius
Member
 
Registered: Sep 2009
Location: Wales, UK
Distribution: Slackware
Posts: 69
Blog Entries: 3

Rep: Reputation: 42
Quote:
How can you specify a .file (dot file) without including the dot as the first character in the glob?

For example ls *bash* and ls ?bash* gives an error "no such file or directory" but ls .bash* gives results.
...Not completely sure what you're hoping to do there, but hidden files can be listed with

Code:
ls -a
Dunno whether that helps...
 
Old 03-11-2011, 01:52 AM   #9
sumeet inani
Member
 
Registered: Oct 2008
Posts: 895
Blog Entries: 26

Rep: Reputation: 48
'ls *something' will not show .something file/folder.

ls .something same as ls \.something
will show file info or contents of that folder.

Also
I noticed that 'ls .*' will show you
file/folder beginning with . (literal understanding)
all folders in current location (according to terminology)
content of parent i.e ..not shown in ls *

Last edited by sumeet inani; 03-11-2011 at 01:59 AM.
 
Old 11-29-2011, 09:10 AM   #10
thebusker
LQ Newbie
 
Registered: Mar 2009
Location: Illinois
Distribution: Fedora
Posts: 5
Blog Entries: 2

Rep: Reputation: 0
reply

I, too, am working through Machtelt's treatment on bash. As this answer is 6 months late, it's more for the googlers than anyone.

I believe for the first of your questions the answer is to use POSIX character classes, e.g.:
Code:
ls /etc | grep [[:digit:]]
The second has me stumped. The closest I've come is:
Code:
ls -lR | grep "`date +%b` `date +%d`" | grep -v ^d
This, of course, lists all files modified throughout the years on today's date and does not answer at all the 'in the last 10 hours' problem. In `info coreutils "ls invocation"' > Formatting file timestamps, there is an explanation of recent vs. non-recent file timestamps - but no mention of how one might adjust the time span which separates the two from 6 months to 10 hours (not that you would want to).

Obviously, find is an elegant and concise answer. To answer the question using grep as the question asked, though, I contend would require conditional statements. Of course, I am likely wrong and there is a hidden gem of bash-fu which would work splendidly.
 
Old 11-29-2011, 04:18 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yeah, I too know it's half-a-year old now, but...

Quote:
I have tried ls /etc *[0-9]* but that doesn't work.
Well, no wonder. Putting a space between the patterns makes the shell treat it as two separate arguments, so ls will list the entire contents of /etc, plus any files in the current directory with digits in their names. What you need to use is a single glob pattern that targets the directory you want.

This works just fine for me:
Code:
ls -d /etc/*[0-9]*
-d tells ls to only display the current directory, preventing recursive listing.

But since this is really a shell expansion function rather than a feature of ls, you could even just use echo (or printf, if you want them on individual lines).

Code:
echo /etc/*[0-9]*

printf "%s\n" /etc/*[0-9]*
In a script you can similarly use the globs to set an array or run in a for loop or whatever.

glob
exended globbing


As for the second question, I don't know why anyone ever want to try to use grep when the appropriate answer is just to use find, which was expressly made for just such a purpose.

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Any other technique would require first using ls or another tool that prints timestamps in its output, then parsing that output for the timestamp and filename. Then, since grep can't do direct comparisons, use some kind of function (shell or awk, perhaps) to compare it to a reference timestamp and print out the filenames corresponding to the appropriate stamps. Why bother?
 
  


Reply


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
[SOLVED] grep search between n-m X.Cyclop Programming 4 10-06-2010 05:28 AM
how to search files apart from GREP?? kapilbajpai88 Linux - Newbie 6 07-24-2008 12:15 PM
grep search... tobiasw Linux - General 4 10-28-2006 02:36 PM
can you specify which files to grep search? sneakyimp Linux - Software 4 10-12-2005 09:28 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 04:29 AM


All times are GMT -5. The time now is 06:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration