LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-09-2005, 01:26 PM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
The find command


Why didn't the following command work? What is those {} , \ and ; are doing?
I expected the output of the find command will go to the grep command


[ka@c83-250-88-135 ka]$ find -name dear -exec grep dear {}\;
find: missing argument to `-exec'
[ka@c83-250-88-135 ka]$
 
Old 06-09-2005, 01:34 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You forget to tell the path to begin the search
Code:
find / -name dear -exec grep dear {} \;
 
Old 06-09-2005, 01:41 PM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thank you very much. It worked fine.

What is the meaning of the forward slash [ / ] ?

What is the braces { } are doing?
 
Old 06-09-2005, 02:32 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
The / is to tell to find command to begin the
search in /

You can search files in your home dir: find /home/Gins -name ...

The braces represent the matching file
 
Old 06-09-2005, 02:56 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Keefaz
You wrote 'The braces represent the matching file' .

What does it mean? I can't take in your point.

I would like to know what this sign \ doing too.
 
Old 06-09-2005, 03:03 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
There is a good utility named ' man ' which will help you
better than me ;)

man find :
Code:
 -exec command ;
              Execute command; true if 0 status is returned.  All
              following arguments to find are taken to  be  argu_
              ments  to  the command until an argument consisting
              of `;' is encountered.  The string `{}' is replaced
              by the current file name being processed everywhere
              it occurs in the arguments to the command, not just
              in arguments where it is alone, as in some versions
              of find.  Both of these constructions might need to
              be  escaped  (with a `\\') or quoted to protect them
              from expansion by the shell.
 
Old 06-09-2005, 03:06 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
In my home directory, I will find a lot of letters written using the Open office Word processor.

I want it to find the word ' Bill ' in those letters. It didn't work. Why?
It gave me no results. I can't fathom the output.

[ka@c83-250-88-135 ka]$ find /home/ka -name Bill -exec grep Bill {} \;
[ka@c83-250-88-135 ka]$

Last edited by Gins; 06-09-2005 at 03:08 PM.
 
Old 06-09-2005, 03:13 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Tri search and grep both insensitive :

Code:
find /home/ka -iname "*Bill*" -exec grep -i Bill {} \;
note the -iname and grep -i

[edit]
how your files are named ? Actually it will search only
in files with the name contains the word bill

Last edited by keefaz; 06-09-2005 at 03:15 PM.
 
Old 06-09-2005, 04:18 PM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks keefaz

The -iname command didn't give me any result. I looked for the word 'until' in my letters

[ka@c83-250-88-135 ka]$ find /home/ka -iname "*until*" -exec grep -i until {} \;
[ka@c83-250-88-135 ka]$
 
Old 06-09-2005, 04:26 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
No, that 's not what I asked.

A file has two part (basically) :
- its content (in this case the text)
- its name (the name of the file, as seen with ls in a dir)

When you do :
find /home/ka -iname "*until*" -exec grep -i until {} \;

Find goes into /home/ka and search only the files named
like ...until... , then it grep its content to search the word
like 'until'

you probably don't want that... I suggest search with extension
say your file names end with ".txt", then :
Code:
find /home/ka -name '*.txt' -exec grep -i until {} \;
 
Old 06-09-2005, 04:37 PM   #11
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Code:
grep until -r -H *.txt
should do what you want, but if you're gonna ask questions about every single linux command you're going to get annoying very quickly. I suggest you do what an earlier poster recommended and read the man page for anything you're not sure about. The questions you're asking are BASIC syntax questions for commands that can be answered just by reading the manual page for that command.

Last edited by Komakino; 06-09-2005 at 04:45 PM.
 
Old 06-10-2005, 04:48 PM   #12
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I have a text document called ' testing1004 ' . It was written using the ' vi ' text editor. I have the word ' Schroeder ' in the document.

The following command didn't find it. What is the problem?
What ís the meaning of Permission denied?

[ka@c83-250-88-135 ka]$ find / -name '*.txt' -exec grep -i Schroeder {} \;
find: /boot/lost+found: Permission denied
find: /tmp/gconfd-root: Permission denied
find: /tmp/lost+found: Permission denied
find: /tmp/1352172254: Permission denied
find: /etc/skel/tmp: Permission denied
find: /etc/default: Permission denied
find: /etc/cups/certs: Permission denied
find: /etc/cups/ssl: Permission denied
find: /var/lib/xdm: Permission denied
find: /var/lib/slocate: Permission denied
find: /var/lib/mysql/mysql: Permission denied
find: /var/lib/mysql/tmp: Permission denied
find: /var/run/sudo: Permission denied
find: /var/spool/cron: Permission denied
find: /var/spool/postfix/active: Permission denied
find: /var/spool/postfix/bounce: Permission denied
find: /var/spool/postfix/corrupt: Permission denied
find: /var/spool/postfix/defer: Permission denied
find: /var/spool/postfix/deferred: Permission denied
find: /var/spool/postfix/flush: Permission denied
find: /var/spool/postfix/hold: Permission denied
find: /var/spool/postfix/incoming: Permission denied
find: /var/spool/postfix/maildrop: Permission denied
find: /var/spool/postfix/private: Permission denied
find: /var/spool/postfix/public: Permission denied
find: /var/spool/postfix/trace: Permission denied
find: /var/spool/at: Permission denied
find: /var/spool/cups: Permission denied
find: /root: Permission denied
find: /proc/tty/driver: Permission denied
find: /proc/1/task/1/fd: Permission denied
find: /proc/2/task/2/fd: Permission denied
find: /proc/3/task/3/fd: Permission denied
find: /proc/4/task/4/fd: Permission denied
find: /proc/5/task/5/fd: Permission denied
find: /proc/6/task/6/fd: Permission denied
find: /proc/7/task/7/fd: Permission denied
find: /proc/8/task/8/fd: Permission denied
find: /proc/9/task/9/fd: Permission denied
find: /proc/11/task/11/fd: Permission denied
find: /proc/158/task/158/fd: Permission denied
find: /proc/283/task/283/fd: Permission denied
find: /proc/930/task/930/fd: Permission denied
find: /proc/998/task/998/fd: Permission denied
find: /proc/1061/task/1061/fd: Permission denied
find: /proc/1099/task/1099/fd: Permission denied
find: /proc/1120/task/1120/fd: Permission denied
find: /proc/1127/task/1127/fd: Permission denied
find: /proc/1135/task/1135/fd: Permission denied
find: /proc/1566/task/1566/fd: Permission denied
find: /proc/1636/task/1636/fd: Permission denied
find: /proc/1662/task/1662/fd: Permission denied
find: /proc/1678/task/1678/fd: Permission denied
find: /proc/1698/task/1698/fd: Permission denied
find: /proc/1702/task/1702/fd: Permission denied
find: /proc/1741/task/1741/fd: Permission denied
find: /proc/2025/task/2025/fd: Permission denied
find: /proc/2049/task/2049/fd: Permission denied
find: /proc/2050/task/2050/fd: Permission denied
find: /proc/2051/task/2051/fd: Permission denied
find: /proc/2117/task/2117/fd: Permission denied
find: /proc/2248/task/2248/fd: Permission denied
find: /proc/2249/task/2249/fd: Permission denied
find: /proc/2250/task/2250/fd: Permission denied
find: /proc/2251/task/2251/fd: Permission denied
find: /proc/2252/task/2252/fd: Permission denied
find: /proc/2253/task/2253/fd: Permission denied
find: /proc/2571/task/2571/fd: Permission denied
find: /proc/2673/task: No such file or directory
read long file names. (Schroeder)
page. (Michael Schroeder)
find: /.mozilla: Permission denied
find: /lost+found: Permission denied
[ka@c83-250-88-135 ka]$
 
Old 06-10-2005, 05:54 PM   #13
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
It means you're not allowed in that directory because you don't have read permission.

Did you try the command I suggested?
Code:
grep Schroeder -r -H *
 
Old 06-11-2005, 03:46 AM   #14
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Komakino

It worked very well. I take my hat off for the help. This is a very simple command to find a text in your documents; it just has a few words. If you compare it with the ' find ' command, this is a very short one. I can always write it without looking at a written paper on those commands.

I use to look at some papers before writing commands like ' grep ' , 'egrep' , ' fgrep ' and ' find ' . I have written down all the important commands. I always make mistakes if I wrote a command without looking at a paper.

I would like to know the meanings of the switches.

What is ' H ' is doing?

What is ' * ' is doing?

What is ' r ' is doing?
 
Old 06-11-2005, 05:27 AM   #15
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Read the man page!!

Code:
man grep
In this case -r is recursive - it means that if it encounters a directory it moves into that directory and continues looking through files (and so on)

-H means it prints the name of the file in which it found the matching text, and the line it appears on.

* is the wildcard - it matches everything and in this case it's in the position where you would put the name of the file you want to search in. For example, if I just wanted to search in a file called sample1234.txt to see if it contained the word Schroeder, I could do:
Code:
grep Schroeder -H sample1234.txt
(and I wouldn't need the -r because I'm not going to encounter any directories)
 
  


Reply



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 On
HTML code is 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 9 06-30-2016 05:30 PM
Find command ? hpladd Linux - Newbie 5 08-09-2005 09:42 PM
find command slack66 Slackware 8 03-14-2005 03:17 PM
help with the find command clockworks Linux - Software 2 02-17-2004 04:24 PM
find command talkinggoat Linux - Newbie 4 10-05-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:46 PM.

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
Open Source Consulting | Domain Registration