LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 12-28-2012, 11:25 AM   #1
dnuttle
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Rep: Reputation: Disabled
-print option with find


Is there any reason to specify the -print option with find, when using the bash shell? Apparently, find always prints to stdout by default (in bash, anyway). I keep seeing code examples that show -print when piping the results, or using -exec. Experimentation seems to indicate that the commands will work the same either way, e.g.:

Code:
find . -name="*.txt" -print -exec grep -l "abc" {} \;
Or:

Code:
find . -name="*.txt" -exec grep -l "abc" {} \;
I can see the value of specifying -print0, if one of the filenames returned might have unusual characters like a line break, but I'm wondering if there is any other case where -print or -print0 is genuinely needed.
 
Old 12-28-2012, 02:23 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Hi and welcome to LinuxQuestions!

-print is required in conjunction with -prune, unless another action is specified, otherwise the name of the excluded directory is printed out together with the results. For example suppose you want to find a file named "magic" in your system, excluding that one in /etc directory. Without -prune you get all the files:
Code:
$ find / -name magic
/opt/kde3/share/mimelnk/magic
/etc/magic
/usr/share/mime/magic
/usr/share/misc/magic
With -prune but without -print
Code:
$ find / -wholename /etc -prune -o -name magic
/opt/kde3/share/mimelnk/magic
/etc
/usr/share/mime/magic
/usr/share/misc/magic
Notice the /etc directory that is not desired in the results. Finally using -print as an action alternative to -prune:
Code:
$ find / -wholename /etc -prune -o -name magic -print
/opt/kde3/share/mimelnk/magic
/usr/share/mime/magic
/usr/share/misc/magic
 
2 members found this post helpful.
Old 12-29-2012, 06:22 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


In general, the basic pattern of the gnu find command looks like this:

Code:
find <starting dirs> <global options> <matching expressions> <actions> [-a/-o <matching expressions> <actions>]
When you only have a single, simple expression, then you can usually leave the actions out, and it will default to -print. But if you have a more complex expression with multiple matching patterns, then their separate actions must all usually be explicitly entered, even -print.

The details of how it works are actually a bit more complex than that, as detailed in section 9 of the first link above. Check it out.
 
1 members found this post helpful.
Old 12-30-2012, 08:24 AM   #4
dnuttle
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks to both of you, very helpful.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 05:36 PM
[SOLVED] About the -print option in find command the_gripmaster Linux - Newbie 3 11-06-2011 01:15 PM
print option biplabbijay Programming 6 03-04-2010 03:11 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
CUPS, option to print self-test page and clean print heads greyed out? BallsOfSteel Linux - Software 1 08-13-2008 03:22 PM


All times are GMT -5. The time now is 05:53 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration