LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-05-2013, 03:25 AM   #1
bsrajan
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Rep: Reputation: Disabled
Usage of -exec option in "find" command


Hi,

I have a question on how does -exec option work in "find" command. In the following example, I expected that all the .h files will be found and then this list will be fed to "exec" command. But the output shows all the files (not only .h files), as "ls -al" seems to have been executed simply on "find ." o/p and not on "find . -name "*.h" o/p..

find . -name "*.h" -exec ls -al {}\;
 
Old 04-05-2013, 04:29 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,171

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
At a quick glance I think you need a space after the } and before the \

Code:
find . -name "*.h" -exec ls -al {} \;
 
1 members found this post helpful.
Old 04-05-2013, 08:53 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
For each file found, find forks a new process and uses the exec system call to start the specified program. The {} identifies where the file name is to be put in the parameter list to the program being run. When the program finises, find then goes to the next identified file name and repeats the process.

For some things, this is ideal (such as copying identified files within a directory tree) for others it is less useful. In this particular case, a "-a" is usually not that useful (ls will list the specified file, -a is to include hidden files in a generic listing).
 
Old 04-05-2013, 12:44 PM   #4
bsrajan
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks. The following worked (a space after the } and before the \)

find . -name "*.h" -exec ls -al {} \;
 
Old 04-06-2013, 04:51 PM   #5
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
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

A couple more points:

gnu find has a built-in "-ls" option, which gives an output similar to "ls -l".

Code:
find . -name "*.h" -ls
Also, when using '-exec' with a command that can accept multiple input files, you can use '+' at the end instead of '\;'. This will run the command in xargs-style batch-processing mode, and only as many processes as necessary to satisfy the system's ARG_MAX restriction will be spawned. This should speed things up considerably on most jobs.

Code:
find . -name "*.h" -exec ls -l '{}' +
The only restriction with the above is that the '{}' must now come only at the end of the command, and that command must be able to accept the full list of files it outputs in that position. mv and cp, for example, must use the '-t' option to specify the target directory before the files to be moved into it.

Notice too that find returns hidden files by default anyway, so the '-a' option in ls is redundant. In fact, you usually don't need to use any file selection options in your commands at all, because it's find that's doing the selecting.

(edit; Whoops, I just noticed jpollard mentioned that too.)

Last edited by David the H.; 04-06-2013 at 04:53 PM.
 
Old 04-08-2013, 03:05 AM   #6
bsrajan
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi David,

I tried the following, as you mentioned. But I am getting an error. May be I am missing something or misundertsood..

>find . -name "*.h" -exec ls -l '{}' +
find: missing argument to `-exec'
>
 
Old 04-09-2013, 04:15 PM   #7
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
I can't see any reason why that should be failing, unless perhaps you're using an unusual implementation of find of some kind. It runs just fine for me. '+' is defined by posix, so any relatively recent version should support it, except maybe one designed for an embedded system or something, a-la busybox.

Or are you perhaps using a shell that treats that character as special in some way? If so, it may need escaping. (That's why I put quotes around the brackets, BTW, since most shells will try to interpret them in some way before passing them on to the command.)

And again please use code tags whenever you post any commands longer than 2 or 3 words.
 
Old 04-09-2013, 04:22 PM   #8
TKH
Member
 
Registered: Jul 2011
Location: Milky Way
Distribution: Ubuntu, LFS, Slackware, Fedora
Posts: 218

Rep: Reputation: 20
you forgot to put ';'
 
Old 04-09-2013, 04:52 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by bsrajan View Post
Hi David,

I tried the following, as you mentioned. But I am getting an error. May be I am missing something or misundertsood..

>find . -name "*.h" -exec ls -l '{}' +
find: missing argument to `-exec'
>
is your prompt supposed to be ">"? This is normally used to indicate that a command is part of a continuation (as in part of a string).

Usually the prompt would end with a "$". if it turns out to be part of a continuation then it is possible that the command you list is being considered part of the previous command, thus the error message is related to that command, and not the part you showed.

The command works on my system as you show it (BTW, the "+" does terminate the command as does ";", the difference is that with the "+" find keeps appending file names to the end of the command until it finishes its search.
 
  


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
Usage of "k size" option in "tar" ?? kcarun Solaris / OpenSolaris 4 08-20-2007 03:59 PM
Where to find a "nice" Howto" for modem setup and use for IPS usage xerxesii Linux - Hardware 2 03-27-2007 01:21 AM
does "find -exec" not recognize built-in command? yaru22 Linux - General 1 06-30-2006 11:35 AM
Several "find -exec" and "find | xargs" questions thanhvn Programming 4 12-02-2005 02:04 PM
Gaim idle time reporting: "x usage" option disappeared DiscoStoo Linux - Software 2 12-23-2004 03:32 PM


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