LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-07-2005, 09:04 AM   #1
harmster
LQ Newbie
 
Registered: Feb 2004
Posts: 25

Rep: Reputation: 15
xargs and my own script


Hi all,

I'm experiencing a really annoying problem. I have this directory structure, pulled from an svn server, which contains in some directories a zip file. This needs to be unzipped. I came up with this:
Code:
ls -R | grep zip | xargs locate | grep zip$ | xargs ./unzip.sh
The contents of my own unzip.sh script:
Code:
unzip -f -d `dirname $1` $1
What it is meant to do is get all zip files and unpack them in the directory where you found them. What it DOES is get the first found zip file and correctly unpacks it and then STOPS! It is driving me mad fast.
This just works:
Code:
ls -R | grep zip | xargs locate | grep zip$ | xargs
Replacing the content of the unzip.sh file to echo $1 just echos the first argument again!
Can somebody help me?
 
Old 05-07-2005, 10:10 AM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
Code:
find `pwd` -type f -name "*.zip" -print0 | xargs -iXX -0 ./unzip.sh XX
 
Old 05-07-2005, 10:12 AM   #3
mcosta
Member
 
Registered: Jan 2005
Location: Spain
Distribution: Debian
Posts: 44

Rep: Reputation: 16
This is:

Code:
find . -name '*.zip'  -exec ./unzip.sh '{}' \;
Or
Code:
find . -name '*.zip' | xargs ./unzip-all.sh
with unzip-all.sh:
Code:
while [ -z "$1" ]; do
    unzip -f -d `dirname $1` $1
    shift
done
the -exec option of 'find' calls the command one at once, say 5 files -> 5 execs . xargs calls with the full input, say 5 files -> 5 args. That's the cause the unzip.sh only decompress the first one, only looks for $1, and the rest are in $2, $3...etc. The shift command makes $1=$2, $2=$3...etc. So you can process a variable number of args, just shift until $1 is empty.

Have you understanded the issue? do not hesitate to ask
 
Old 05-07-2005, 11:14 AM   #4
harmster
LQ Newbie
 
Registered: Feb 2004
Posts: 25

Original Poster
Rep: Reputation: 15
I don't quite get the first reply (why the -iXX?) or the first snibblet of code from the second ('{}'?) but it all makes sense now! Thank you both for you fast replies.
 
Old 05-07-2005, 11:35 AM   #5
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
-iXX says to xargs: replace XX with real argument
{} says to find: replace {} with result of find utility
 
  


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
xargs: unmatched single quote akudewan Linux - Newbie 5 07-28-2014 01:16 PM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 12:22 PM
What's difference btw. -exec and xargs rytrom Linux - Newbie 3 09-14-2004 04:42 PM
xargs question reitzell Linux - Newbie 4 07-04-2004 03:36 AM
xargs: unmatched single quote fillatre Linux - Software 2 06-05-2004 11:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:53 AM.

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