Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to form a correct syntax to sift out according to complementary patterns with 'find'? Post 303011461 by drysdalk on Thursday 18th of January 2018 12:29:39 PM
Old 01-18-2018
Hi,

Taking these in turn:

1. grep -l basically just prints the names of files that match the pattern you're searching for, rather than printing the matching lines in the files themselves. For example, compare:

Code:
$ cat test.txt
This is a test file.
This is the only line that contains the string 'FOO'.
This line doesn't contain it, and is the last line in the file.
$ grep FOO test.txt
This is the only line that contains the string 'FOO'.
$ grep -l FOO test.txt
test.txt
$

So we can see that when we used the -l flag, we just got the filename returned, rather than the matching line within the file.

2. grep -L prints out only the names of those files which do not match the given string. Again, best demonstrated with an example.

Code:
$ cat test.txt
This is a test file.
This is the only line that contains the string 'FOO'.
But because of that, a 'grep -L FOO' won't return the name of this file.
$ cat test2.txt
This file does not contain the string we're searching for.
So its filename will be printed when we do a grep -L
$ grep -L FOO *.txt
test2.txt
$

So here, we only got test2.txt in our output and not test.txt, since test2 did not contain the string we were searching for, whereas test did. Because we wanted to only see the names of those files which did not contain our string, this makes sense.

3. I don't have time right now for a full write-up of how -prune behaves, but basically it tells find not to consider everything that the arguments before the -prune lfag found, more or less. If this isn't clear then I'll try to reply again tonight with a bit more detail on this last point.

Hope this helps.

-

Right, a bit more detail on -prune. As others have mentioned throughout previous replies, in its usual usage find performs a series of tests to ultimately return whatever it is you've asked it to find. By default, all of these tests must pass, and only things which pass all of the tests you've specified will be acted upon by find in the end.

So in the case of the command find . -type f -exec grep -l FOO \{\} \;, there are a few things being tested here.

Firstly, that the thing being considered resides underneath the current working directory, represented by . (the first argument is always the root of the path that find will start from).

Second, we are only interested in things which are files. The -type flag can be used to search for directories, files, symbolic links, device files, all kinds of things. Files are represented by -type f

Now before we go on, remember that in order to proceed to the next test, the previous test must have passed. So at this point we've found all files that reside in or somewhere beneath our current directory. So the third step is continued into.

The third flag is a bit different, or might seem so at first. The purpose of -exec is to execute an external command on whatever it is we've found up 'til now. So here, we execute the command grep -l FOO on all files that reside in or beneath the current working directory. The item currently being processed is represented by two curly brackets, and the end of the command is signified by a semi-colon. So the \{\} is substituted at execution time by whichever of our found-so-far-things is currently up for consideration. And the ; signifies the termination of the command to be executed.

Now, that explains things so far. But what if we first want to exclude a category of things from consideration that would otherwise normally be caught by find ? That's where -prune comes in. It will remove from subsequent consideration anything that has been matched by any flags or actions taken up until this point in the find command. So things matched prior to the -prune flag will not be matched by anything that follows the -prune.

So if we look again at my original proposed solution to your problem, the command find . -type d -name 'browser?' -prune -o -type f -exec grep -l FOO \{\} \; can be broken down as follows:

1. The path we are running our find beneath. This is the current working directory, .

2. -type d -name 'browser?', meaning "all directories whose names match the regular expression 'browser?'".

3. -prune -o, meaning "we want to exclude from consideration all things matched by whatever comes next, if they also match whatever came before this point". So no content within directories in or underneath the current working directory whose names match 'browser?' will be affected by whatever follows this point.

4. -type f -exec grep -l FOO \{\} \;, meaning "execute grep -l FOO on all files". But because of our previous -prune -o, the end result of this is only to execute the grep -l FOO on all files that do not reside in or underneath a directory whose name matches the regex 'browser?'.

I hope this helps clear things up. If you have any more questions let me know and I'll be happy to help if I can.

Last edited by drysdalk; 01-18-2018 at 04:30 PM..
This User Gave Thanks to drysdalk For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Correct Syntax For Calling Shell Script in Perl Module

Problem: I have a shell script that will be called by a Perl module that will connect to a db and delete rows. The Perl module will be called by CRON. I am using a Perl module to call a shell script because I need to get the db connection from Perl. Here is the Perl pseudocode: ... (4 Replies)
Discussion started by: mh53j_fe
4 Replies

2. Shell Programming and Scripting

Plz correct my syntax of shell script

Dear all I am still bit new in shell script area.I am writing down a shell script which I guess somewhere wrong so please kindly correct it. I would be greatful for that. What I actually want from this shell script is that it will move all the files one by one to another server which can be... (2 Replies)
Discussion started by: girish.batra
2 Replies

3. Shell Programming and Scripting

if [ $NOWDATE -gt $STARTDATE ] , date comparison correct syntax?

i've looked at a bunch of the date comparison threads on these boards but unfortunately not been able to figure this thing out yet. still confused by some of the way conditionals handle variables... here is what i where i am now... # a bunch of initializition steps are here ...... (1 Reply)
Discussion started by: danpaluska
1 Replies

4. UNIX Desktop Questions & Answers

Correct syntax

Hi, I want to check if file(s) exist even in subdirectories and perform an action. After searching here couldn't find solution that would work, but made my own solution that works fine: if then echo egrep "$1|$2|$3" `find| grep MLOG` else echo "MLOG does not exist" fiThat will check... (1 Reply)
Discussion started by: Vitoriung
1 Replies

5. Shell Programming and Scripting

Do syntax is correct ?

I tried with sed command to create a space between namespace from the XML file. I used this syntax. Can someone tell me is this syntax is vaild? /usr/xpg4/bin/sed -e 's/<\/^.*><^.:Errort>/<\/^.*> <^.:Errort>/g' test > test2 I dint find any changes or any space being created between... (10 Replies)
Discussion started by: raghunsi
10 Replies

6. Shell Programming and Scripting

how to form Records[multiple line] between two known patterns

file contents looks like this : #START line1 of record1 line2 of record1 #END #START line1 of record2 line2 of record2 line3 of record2 #END #START line1 of record3 #END my question how should i make it a records between #START and #END . willl i be able to get the contents of the... (5 Replies)
Discussion started by: sathish92
5 Replies

7. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

8. Shell Programming and Scripting

Cannot find correct syntax to make file name uppercase letters

I have a file name : var=UsrAccChgRpt I want to make them upper case. Tried: $var | tr Error: tr: Invalid combination of options and Strings. Usage: tr | -ds | -s | -ds | -s ] String1 String2 tr { -d | -s | -d | -s } String1 Could you please help. I am using AIX... (2 Replies)
Discussion started by: digioleg54
2 Replies

9. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

10. OS X (Apple)

Can't figure out the correct syntax for a command loading a webkit plugin

Hello, Using Bash on Mac OS X 10.7.5 (Lion). I downloaded a GrowlSafari plugin for Webkit from its GitHub page GitHub - uasi/growl-safari-bridge: GrowlSafariBridge enables arbitrary javascript (including Safari Extensions) to notify via Growl.. In the description it says that after installing for... (0 Replies)
Discussion started by: scrutinizerix
0 Replies
All times are GMT -4. The time now is 05:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy