[NTLUG:Discuss] "find -exec" -vs xargs -OR- subshells in -exec

Stuart Johnston saj at thecommune.net
Fri May 26 09:53:44 CDT 2006


Richard Geoffrion wrote:
> I'm trying to rename a bunch of mangled files.  I want to replace all of 
> the tilde (~) characters with dashes (-).
> 
> Find can find the files, but I can't figure out how to rename the file 
> once I have it on the command line.
> (and for the purposes of this issue...on this particular machine,  I 
> don't have the rename command ---- even though I tried using rename on 
> another machine with no luck.)
> (I hate it when machines have no luck)
> 
> rtcg at bobpc:/tmp# mkdir test
> rtcg at bobpc:/tmp# cd test
> rtcg at bobpc:/tmp/test# ls
> rtcg at bobpc:/tmp/test# touch 1~1.jpg
> rtcg at bobpc:/tmp/test# find . -name *~*
> ./1~1.jpg
> rtcg at bobpc:/tmp/test# find . -name *~* -exec mv {} `echo {} | sed y#~#-#` \;
> mv: `./1~1.jpg' and `./1~1.jpg' are the same file
> rtcg at bobpc:/tmp/test# TESTFILE=1~1.jpg
> rtcg at bobpc:/tmp/test# mv $TESTFILE `echo $TESTFILE | sed y#~#-#`
> rtcg at bobpc:/tmp/test# ls
> 1-1.jpg
> rtcg at bobpc:/tmp/test#
> 
> 
> I've looked at xargs, but xargs just wants to run a command on the 
> results.  I can't find how to manipulate the ??stout/stin'? that was 
> passed to xargs.
> 
> 
> Suggestions?

rename works well for me.

$ touch 1~1.jpg
$ find . -name *~* -exec rename 'y/~/-/' {}  \;
$ ls
1-1.jpg

rename is just a perl script so you could probably copy it over to your 
system.  Or, move your mv and sed commands into a shell script and run 
that from find:

$ cat ren
#!/bin/bash

mv $1 `echo $1 | sed y#~#-#`

find . -name *~* -exec ./ren {}  \;



More information about the Discuss mailing list