LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2011, 05:26 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Unhappy find -exec option and xargs


How to display only the "found" file names in this scenario:

Code:
[demo@localhost Perl]$ find . -mtime -17 -exec "file" \{\} \;
.: directory
./hi: empty
./try.pl: a /usr/bin/perl script text executable
./hey.pl: a /usr/bin/perl script text executable
Code:
[demo@localhost Perl]$ find . -mtime -17 -exec "ls -l" \{\} \;
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
Code:
[demo@localhost Perl]$ find . -mtime -17 -exec 'ls -l' \{\} \;
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
find: `ls -l': No such file or directory
[demo@localhost Perl]$
?


I am trying to long-list the names of the files which have been created or otherwise modified / changed during the last 17 days.

Code:
[demo@localhost Perl]$ find . -mtime -17 -print | xargs 'file'
.:        directory
./hi:     empty
./try.pl: a /usr/bin/perl script text executable
./hey.pl: a /usr/bin/perl script text executable
Code:
[demo@localhost Perl]$ find . -mtime -17 -print | xargs 'ls -l'
xargs: ls -l: No such file or directory
I seem to have forgotten the correct syntax.

I do have a way to do that, though:


Code:
[demo@localhost Perl]$ ls -ltr | grep -w Sep | awk '{if($7 >=2) print $0}'
-rw-rw-r--. 1 demo demo 125 Sep  9 10:25 hey.pl
-rwxr-xr-x. 1 demo demo  72 Sep  9 14:55 try.pl
-rw-rw-r--. 1 demo demo   0 Sep 26 04:56 hi
[demo@localhost Perl]$
 
Old 09-26-2011, 06:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you embed the command in quotes, it will be interpreted literally as ls<space><minus>l and obviuosly you don't have an executable whit such a name. Just remove the enclosing quotes and it should be fine.
 
Old 09-26-2011, 09:09 AM   #3
tange
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 9
Have a look at GNU Parallel. It should behave less surprising than -exec and xargs.

Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ
 
Old 09-26-2011, 10:34 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Also note that some implementations of "find" are not exactly the same. Linux and OS/X (Darwin) vary in subtle ways.
 
Old 09-27-2011, 07:06 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by colucix View Post
If you embed the command in quotes, it will be interpreted literally as ls<space><minus>l and obviuosly you don't have an executable whit such a name. Just remove the enclosing quotes and it should be fine.
Code:
[demo@localhost Perl]$ find . -mtime -17 -exec ls -l \{\} \;
total 88
-rwxr-xr-x. 1 demo demo    75 Aug 19 14:51 array.pl
-rw-rw-r--. 1 demo demo 12121 Sep 27 07:20 data.log
-rwxr-xr-x. 1 demo demo   204 Aug 21 13:58 dbmopen.pl
...
<Output Truncated>
...
-rwxr-xr-x. 1 demo demo   140 Aug 22 04:45 sentence_case_regex.pl
-rwxr-xr-x. 1 demo demo   169 Aug 19 07:11 sub_1.pl
-rwxr-xr-x. 1 demo demo   203 Aug 19 16:11 sub_2.pl
-rwxr-xr-x. 1 demo demo    57 Aug 21 13:41 time.pl
-rwxr-xr-x. 1 demo demo   160 Aug 19 16:03 tr.pl
-rwxr-xr-x. 1 demo demo    72 Sep  9 14:55 try.pl
-rwxr-xr-x. 1 demo demo   148 Aug 19 07:32 while.pl
-rwxr-xr-x. 1 demo demo    43 Aug 19 14:42 withoutSemicolon.pl
-rwxr-xr-x. 1 demo demo    63 Aug 22 09:57 x.pl
-rw-rw-r--. 1 demo demo 64 Sep 27 07:40 ./hello
-rw-rw-r--. 1 demo demo 0 Sep 26 04:56 ./hi
-rw-rw-r--. 1 demo demo 12121 Sep 27 07:20 ./data.log
-rw-rw-r--. 1 demo demo 23 Sep 26 08:05 ./hel
[demo@localhost Perl]$
You see ls -l is not working as expected or at least as desired.
 
Old 09-27-2011, 09:31 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The first thing your 'find' command will see is ".", and "ls -l ." will list contents of the current directory. This would be more obvious if you added the "-d" option to the 'ls' command:
Code:
find . -mtime -17 -exec ls -ld \{\} \;

Last edited by rknichols; 09-27-2011 at 09:32 AM. Reason: bad tag
 
Old 09-27-2011, 10:31 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As rknichols pointed out, the ls -l command works as expected. It lists files in the current working directory and list the content of every directory (the same as you can obtain running ls -l dirname from the command line). What do you want to get actually? Do you know that find has the -ls action?
Code:
find . -mtime -17 -ls
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Xargs with output of find thund3rstruck Programming 6 07-17-2011 08:26 AM
[SOLVED] Backup One Liner find xargs cp metallica1973 Linux - Server 7 05-30-2011 08:50 PM
Several "find -exec" and "find | xargs" questions thanhvn Programming 4 12-02-2005 01:04 PM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 11:22 AM
What's difference btw. -exec and xargs rytrom Linux - Newbie 3 09-14-2004 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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