Re: "find" feature request

From:

Paul Jackson

Subject:

Re: "find" feature request

Date:

2 Oct 2000 23:33:19 GMT

Adam wrote:
|> I recently used GNU find to rename a buch of files by prepending
|> a string to the filename. It was a pain in the ass to say the least.
|> ...
|> I wanted to say
|> $ find . -name '*' -exec mv {} Moby-{}
I think you're trying to hammer a nail with a screwdriver.
That is, the "find ... -exec" option is not the best way
(nor even one of the tolerably ok ways) to skin this cat.
In DOS terms (if you're allergic to DOS, ignore this sentence)
you wanted to: move * Moby-*
That is, more generically, you wanted to rename all files in
the _current_ directory to have a "Moby-" prefix, right?
The 'find' command descends an entire directory subtree.
So using it like you were trying would fail if you had any
subdirectories. And the "-exec" option of find is seldom
used with pleasure. I'd recommend you pretend it doesn't
exist.
For more simple tasks (not this one) see instead the 'xargs'
command, and instead of doing something like:
find . -name '*.deleteme.junk' -exec rm
rather do:
find . -name '*.deleteme.junk' | xargs rm
Here are some alternative ways that I would consider doing
the particular task at hand (prefixing filenames with "Moby-").
These are all in Bourne/Korn/Bash shell syntax, not necessarily
csh syntax.
[1] If the task is for files in current directory only (do not
descend to subdirectories)
a] ls -1 | while read i
do
mv $i Moby-$i
done
b] ls -1 | sed 's/.*/mv & Moby-&/' | sh
c] for i in *
do
mv $i Moby-$i
done
[2] If the task is for all files in the current subtree (_do_
descend to subdirectories)
a] find . -depth -print | while read i
do
mv $i `dirname $i`/Moby-`basename $i`
done
b] find . -depth -print | sed 's;\(.*/\)\(.*\);mv \1\2 \1Moby-\2;' | sh
Method [1c] is the simplest, in my view, to solve problem [1],
but methods [a] and [b] are more parallel, independent of which
of the two problems you are solving.
Many users have aliases for the 'ls' command, so methods [1] [a]
and [1] [b] might better use "/bin/ls", instead of "ls", to avoid
the aliases.
--
--
I won't rest till it's the best ... Software Production Engineer
Paul Jackson (address@hidden; address@hidden) 3x1373 http://sam.engr.sgi.com/pj