help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] hi, doubt about xargs


From: Greg Wooledge
Subject: Re: [Help-bash] hi, doubt about xargs
Date: Mon, 13 May 2013 09:04:46 -0400
User-agent: Mutt/1.4.2.3i

On Mon, May 13, 2013 at 12:09:13PM +0200, Joaquin Villanova wrote:
> Hi, i want to convert all my existing pdf documents to djvu (just to 
> give a brief intro) then i decided to use bash shell and find / xargs, 
> so i executed the following
> find ~/ -name '*pdf*' -print0 | xargs -0  -n 1 pdf2djvu -i '{}' -o '{}'
> 
> In pdf2djvu -i goes for input and -o for output. The issue here is that 
> i want to get the output to the same directories to the inputfile, but 
> with this order all what i get is one output file only, thanks in advance!

Since you have -print0, you may also have -execdir.  If so, then this
becomes somewhat simpler:

address@hidden:/tmp/greg$ mkdir -p a/b/c
address@hidden:/tmp/greg$ touch a/b/c/foo.txt
address@hidden:/tmp/greg$ find . -name '*.txt' -execdir sh -c 'for f; do cp 
"$f" "${f%.txt}.bak"; done' _ {} +
address@hidden:/tmp/greg$ ls -l a/b/c
total 0
-rw-r--r-- 1 wooledg wooledg 0 May 13 08:59 foo.bak
-rw-r--r-- 1 wooledg wooledg 0 May 13 08:59 foo.txt

Without -execdir, your script would have to extract the directory name
and filename of each path argument, cd into the directory, and then do
its work.  Something like this:

find . -name '*.txt' -exec sh -c 'for f; do dir=${f%/*} file=${f##*/}; cd 
"$dir" || continue; cp "$file" "${file%.txt}.bak"; cd -; done' _ {} +



reply via email to

[Prev in Thread] Current Thread [Next in Thread]