LinuxQuestions.org
Help answer threads with 0 replies.
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-10-2012, 10:43 AM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 574

Rep: Reputation: 62
Q on Find cmd


this is really a newbie Q, but i just cant figure it out.

sles 11.1, fully patched

if i run this why does it spit back the names of the dirs from "." ??
Code:
[host:/logs/syslog #] find . -name log -type f -mtime +20 | xargs ls -al
total 20
drwxr-x--- 5 root root 4096 2012-03-13 13:12 .
drwxr-x--- 6 root root 4096 2012-04-05 13:52 ..
drwxr-x--- 9 root root 4096 2012-03-09 18:09 dir1
drwxr-x--- 5 root root 4096 2012-03-09 16:09 dir2
drwxr-x--- 3 root root 4096 2012-03-13 13:13 dir3
i know i have no files named "log" that are older than 21 days so i dont expect to see files, but why the 3 dirs?

but if i run this i get nothing back (expected)
Code:
find . -name log -type f -mtime +20 -exec ls -al {} \;

Last edited by Linux_Kidd; 04-10-2012 at 10:47 AM.
 
Old 04-10-2012, 10:48 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
What happens if you run it without the 'xargs'? I presume they're still listed...
 
Old 04-10-2012, 10:59 AM   #3
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 574

Original Poster
Rep: Reputation: 62
yes.
 
Old 04-11-2012, 09:50 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,622

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
If you have no files named "log", then the find command produces no output and xargs runs the ls command with no non-flag arguments. When you run ls with no arguments it lists the content of the current directory.

You need to run xargs with the "--no-run-if-empty" (short form is "-r") option so that the ls command will not be run at all if there is no output from find.
 
Old 04-11-2012, 04:49 PM   #5
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 574

Original Poster
Rep: Reputation: 62
Quote:
Originally Posted by rknichols View Post
If you have no files named "log", then the find command produces no output and xargs runs the ls command with no non-flag arguments. When you run ls with no arguments it lists the content of the current directory.

You need to run xargs with the "--no-run-if-empty" (short form is "-r") option so that the ls command will not be run at all if there is no output from find.
why is that different than -exec ls -al {} \;
 
Old 04-11-2012, 05:28 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Because -exec is an argument of find that only gets executed on any matching files.

With the "| xargs", this is the equivalent of what you're running:
Code:
files=$(find . -name log -type f -mtime +20)
ls -al $files
Since the find returns nothing, "files" is empty, so your "ls -al $files" is really just "ls -al", which lists everything in the cwd.

With the -exec, this is the equivalent of what you're running:
Code:
files=$(find . -name log -type f -mtime +20)
for file in $files; do ls -al $file; done
Since the find returns nothing, "files" is empty, so ls -al is never even called.

Last edited by suicidaleggroll; 04-11-2012 at 05:31 PM.
 
Old 04-12-2012, 06:54 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by rknichols View Post
If you have no files named "log", then the find command produces no output and xargs runs the ls command with no non-flag arguments.
@Linux_Kidd, I thought you said they were listed if you ran it without piping it to the 'xargs ls -al' part of the command? Obviously a communication error at some point between us...
 
Old 04-12-2012, 03:18 PM   #8
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 574

Original Poster
Rep: Reputation: 62
Quote:
Originally Posted by Snark1994 View Post
@Linux_Kidd, I thought you said they were listed if you ran it without piping it to the 'xargs ls -al' part of the command? Obviously a communication error at some point between us...
dirs are listed when using pipe to xargs when no log files found.
dirs are not listed when using -exec and no log files found.

i had read some info about how using -exec causes a certain type of forking which slows down a search of such, but using xargs can be better for less forking. however, the side affects are obvious as shown.
 
Old 04-12-2012, 08:12 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,622

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Quote:
Originally Posted by Linux_Kidd View Post
i had read some info about how using -exec causes a certain type of forking which slows down a search of such, but using xargs can be better for less forking.
When you use "-exec" this way:
Code:
find ... -exec ls {} ;
the ls command is invoked separately for each name that matches the find expression. If there were 1000 matches, find would fork and exec ls 1000 times. Another side effect would be that each invocation of ls would see only one name, so the overall output would not be sorted unless, by sheer coincidence, the names happened to appear in the directory in their collating sequence.

Reasonably recent versions of find can be told that commands invoked by "-exec" can accept multiple arguments. The syntax is:
Code:
find ... -exec ls {} +
That tells find to pack as many arguments as possible into each invocation of ls, which is the same as what xargs would do. That greatly reduces the number of fork/exec calls, and allows ls to do its normal sorting. Of course if the argument list would be too long, there would be more than one invocation of ls and the output would only be sorted within each group. The maximum length of the argument list is defined in /usr/include/linux/limits.h, which on my system has:
Code:
#define ARG_MAX 131072  /* # bytes of args + environ for exec() */
xargs would be subject to the same limitation, but you could achieve an overall sort of even a very large list by sorting the output from find before xargs sees it:
Code:
find ... | sort | xargs ls
That is something you could not do for "-exec".
 
  


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
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 09:08 AM
[SOLVED] find in bash script returns "missing argument to `-exec'" while cmd runs fine zhjim Programming 11 01-31-2012 09:56 AM
[SOLVED] locate cmd couldnt search file but find did skaushal_lk Linux - Newbie 8 07-08-2010 07:44 AM
Other than top command which cmd is used to find the cpu usage and disk space sathish80 Linux - Newbie 2 11-15-2006 01:43 AM
determine whether 'find' cmd found the file or not mufy Linux - General 7 03-16-2005 08:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 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