LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 07-15-2011, 08:57 AM   #1
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Rep: Reputation: 38
Question Xargs with output of find


Hi guys,

I'm testing some multi-plat java code and I'm getting a bit frustrated with the Linux tests. I need to run the command:

Code:
$ java -jar /home/developer/TCO/TabletComicOptimizer.jar <file> <args[]>
against all the files that match a specific criteria. I've tried various find syntax and I can't seem to get it right.

Normally I would just create a bash script and populate the results of find into an array and then just enumerate the collection but in this specific case I want to demonstrate this operation at the bash terminal.

I've tried things like:

Code:
~/TCO $ find . -type f -iname "*.cb[rz]" | xargs java -jar TabletComicOptimizer.jar {} 1200x1800 \;
Thinking that the {} is the substitution for each file returned by find but it's not working.

How do I execute my java program against each result in the find operation?

Thanks!
 
Old 07-15-2011, 09:08 AM   #2
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Original Poster
Rep: Reputation: 38
Wink

Ugh...

-exec is what I actually need:

Code:
 ~/AndriodDev/Comics/Batman - Arkham Reborn $ find . -iname "*.cb[rz]" -exec java -jar ~/Downloads/jComicOptimizer/TabletComicOptimizer.jar "{}" \;
Got it... sheesh... that was a bit embarrassing
 
Old 07-15-2011, 09:09 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Have you tried not using xargs, but find's -exec operand?
Code:
find . -type f -iname "*.cb[rz]" -exec java -jar TabletComicOptimizer.jar {} 1200x1800 \;
Hope this helps.

EDIT: Answer was found while I was typing
 
Old 07-15-2011, 10:02 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by thund3rstruck View Post
Ugh...

-exec is what I actually need:

Code:
 ~/AndriodDev/Comics/Batman - Arkham Reborn $ find . -iname "*.cb[rz]" -exec java -jar ~/Downloads/jComicOptimizer/TabletComicOptimizer.jar "{}" \;
Hi,

if using 'find' then -exec is the way to go. If you pipe the output of another program to 'xargs' then this is how you can handle the substitutions explicitly:
Code:
... | xargs -I '{}' echo '{} and some other text'
If you do not specify the -I option then the piped input will just be appended at the end of the command that 'xargs' executes.
So the 'xargs' equivalent 'find -exec' in your initial post would be:
Code:
~/TCO $ find . -type f -iname "*.cb[rz]" | xargs -I '{}' java -jar TabletComicOptimizer.jar {} 1200x1800
BTW, you do not have to use curly braces. You can specify almost any other String as placeholder.
 
1 members found this post helpful.
Old 07-15-2011, 01:28 PM   #5
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by crts View Post
If you pipe the output of another program to 'xargs' then this is how you can handle the substitutions explicitly:
Code:
... | xargs -I '{}' echo '{} and some other text'
If you do not specify the -I option then the piped input will just be appended at the end of the command that 'xargs' executes.
Excellent feedback! Thank you. I was a little baffled as to why xargs was not working and it was that I switch.

Outstanding!
 
Old 07-17-2011, 03:20 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
By the way, you don't need to use '{}' with -I; any character string will do*. I prefer using '@' myself, as it's not a reserved shell character and so doesn't need quoting.

Also remember to use the null separator when working with filenames that can contain spaces.
Code:
~/TCO $ find . -type f -iname "*.cb[rz]" -print0 | xargs -I @ -0 java -jar TabletComicOptimizer.jar @ 1200x1800
As for find with -exec, replacing the final ';' with '+' tells it to run the operation in xargs-like batch mode, on all the filenames at once**.
Code:
find . -f -exec foo "{}" \+

#Executes as "foo file1 file2 file3 ..."
Notice that the "{}" must now appear exactly once, at the end of the command, and will expand to the entire list of filenames. So be sure that your command can handle that kind of input.

--
* Using gnu xargs and find, of course. Other implementations may not have the same features.
** Or as many as the command buffer will hold at a time, if the list is large.
 
1 members found this post helpful.
Old 07-17-2011, 09:26 AM   #7
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by David the H. View Post
As for find with -exec, replacing the final ';' with '+' tells it to run the operation in xargs-like batch mode, on all the filenames at once**.
Code:
find . -f -exec foo "{}" \+

#Executes as "foo file1 file2 file3 ..."
Again, outstanding response! Thank you. Yea, this specific Java/Andriod application doesn't use named parameters so it expects a filepath as the first argument and an optional set of dimensions as the second argument. It doesn't understand a space delimited sequence of files and at first glance that's exactly what find was appearing to send in its output. Before I got your excellent response I was going to try a printf %s\n in the find to see if it functioned differently. Thankfully, I was just employing xargs incorrectly.

Thanks again!
 
  


Reply

Tags
find, java


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
[SOLVED] Backup One Liner find xargs cp metallica1973 Linux - Server 7 05-30-2011 09:50 PM
find | xargs | shred seems really slow Bertical Linux - Software 3 11-19-2010 10:36 AM
When I use xargs with echo, why does the output contain an extra space? web_janitor Linux - General 2 08-20-2009 05:18 PM
clarification on find + xargs rm command dtra Linux - General 2 05-17-2006 06:56 AM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 12:22 PM


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