Combining find, grep and possibly ls?!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining find, grep and possibly ls?!
# 1  
Old 10-13-2011
Question Combining find, grep and possibly ls?!

Hi - can someone please help me combine find, grep and possibly ls into something workable:

i.e. How can I list all the files that contain the word "pet" in all directories under the current directory that are called "animal", bar those anywhere under directories called "archive"?

I suspect this will be a combination of grep -l pet, find . -type d -name animal
and prune, but can't quite work out what! Smilie

Any help most appreciated... Smilie
# 2  
Old 10-13-2011
run this under the directory animal
Code:
find -iname "*" -exec grep -li "pet" {} \; | grep -v archive

# 3  
Old 10-13-2011
What shell and operating system are you using?
# 4  
Old 10-13-2011
Try
Code:
find ./animal/* -name "*pet*" -ls | grep -v archive

EDIT: nm that, mis-read the question.Smilie

Last edited by CarloM; 10-13-2011 at 11:28 AM..
# 5  
Old 10-13-2011
SunOS & Korn or Bash Shell

An example. The following files contain the word "pet":
Code:
archive_file.csv
file_2.csv
file_3.csv

Here they are in the directory structure:
Code:
/directory_1/
---animal/
------file_1.csv
------archive_file.csv
------directory_3/
---------file_4.csv
/directory_2/
---animal/
------file_2.csv
---archive/
------animal/
---------file_3.csv

I would only expect the command to return:
Code:
/directory_1/animal/archive_file.csv
/directory_2/animal/file_2.csv

...because "file_3.csv" is under an "archive" sub-directory.
# 6  
Old 10-13-2011
Code:
(
 IFS='
'
 
 find $(
   find . -type d -name archive -prune -o -type d -name animal -print
     ) \
     -type f -exec grep -l pet {} +
   )

# 7  
Old 10-13-2011
Works a treat! Thanks radoulov... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How would I construct a (possibly simple) IF statement?

Hi all, I thought this would be simple, but I've been having a lot of trouble trying to write this IF statement, if I may ask for help pls: In BASH, how would I construct the if statement: Should ONLY be true if USEROPTscript=="yes"]] AND $mode=="INSTALL" /or/ $mode=="CHANGE" ]]... (3 Replies)
Discussion started by: jmccoughlin
3 Replies

2. UNIX for Dummies Questions & Answers

Combining grep patterns with OR condition?!

Hello! I have a question about how to combine patterns in grep commands with the OR operator. So I have this little assignment here: Provide a regular expression that matches email addresses for San Jose City College faculty. A San Jose City college faculty’s email address takes the form:... (1 Reply)
Discussion started by: kalpcalp
1 Replies

3. HP-UX

Cdrom device possibly missing?

Hello, I am following the HPUX 11.31 install/update guide and I am trying to install "Update-UX" from the installation media. I put the CD into the drive, and I am trying to mount the device. The instructions state:Find the DVD-ROM device file name: ioscan -C disk -f -n -k | more A typical... (5 Replies)
Discussion started by: bstring
5 Replies

4. Shell Programming and Scripting

FIND: Combining -size & -prune

I am having an issue adding the -size test to my find command. I am trying to find all files smaller than 250mb, that are not in .snapsnot or man directories. What i started with find . -xdev -type d \( -name man -o -name .snapshot \) -prune -o -type f What I have tried..unsuccessfully... (4 Replies)
Discussion started by: nitrobass24
4 Replies

5. UNIX for Dummies Questions & Answers

$PATH error (possibly)

Upon opening Terminal I get the following message: -bash: /usr/bin/manpath: No such file or directory -bash: /usr/bin/perl: No such file or directory -bash: grep: command not found -bash: grep: command not found -bash: grep: command not found -bash: grep: command not found I searched... (9 Replies)
Discussion started by: SartreSmartre
9 Replies

6. UNIX for Dummies Questions & Answers

Comparing Two Files, Possibly with VIM

Hello, I have a problem comparing two files, both .csv. One file, let's call it files.csv, contains a list of thousands of file names for models of molecules that have made it through an optimization process. The other file, formulas.csv, contains file names for the same kind of models, and has... (2 Replies)
Discussion started by: Stuart Ness
2 Replies

7. UNIX for Dummies Questions & Answers

Combining find, grep and gzip

I'm trying to see which files have a particular word in them. The files are all text files but are gzipped and are in sub directories. In order to view the content of a single gzipped file I tried: cat filename | gzip -d | less , and it shows the files contents. But, I want to see a list... (1 Reply)
Discussion started by: thoughts
1 Replies

8. Solaris

Why in.mpathd errors - performance possibly?

Hello all, Run a search but see no previous queries. Trying to get to the bottom of why a server running Solaris 9 reports the following every other day: lonpcbcfp1:Jun 20 16:33:20 lonpcbcfp1 in.mpathd: missed sending 17 probes cur_time 1478014085 snxt_time 1478015026 snxt_basetime 1478014018... (0 Replies)
Discussion started by: bookiebarton
0 Replies

9. Shell Programming and Scripting

stupid question possibly

how would I search through subdirectories under the current directory and delete all files in certain directories. in ThisDirectory.... want to go into foundMe directory which there are several in other subdirectories and delete all files in foundMe ? thank you... (1 Reply)
Discussion started by: MrJaunty
1 Replies
Login or Register to Ask a Question