Link to home
Start Free TrialLog in
Avatar of witty
witty

asked on

execute command recursively in subdirs

How can I rename alle files named "*.x" to "*.y" in all subdirs?

I'm looking for a general solution!
I know mmv - but I don't know, if I could use it here.

a solution would be a command that executes the same command in every subdir ...

so I would call:
run_rec mmv "*.x" "#1.y"

thanks
michi
Avatar of ahoffmann
ahoffmann
Flag of Germany image

find ./ -name "*.x" | xargs -i basename {} ".x" | xargs -i mv {}.x {}.y
# or a more UNIXisch solution
find ./ -name \*.x | sed -e 's/\(.*\)\.x/mv & \1.y/'|sh
Avatar of witty
witty

ASKER

well, this is exactly, what I do all the time - but there must exist some better solution - more related to recursivity!!!

thanks
witty
KISS - keep it small and simple

why do you warry about the find command? It does exactly what you asked for, not more and not less, exactly.
This with just a few key hits, if you do it recursively you need a script/program with a few hundred bytes. Why?
Find still is recursively, somehow.
An d with a simple  -type f  you can restrict to files. What do you need more?
Avatar of witty

ASKER

I know what you mean - but there MUST exist something without "find -exec" or "xargs"!

nevertheless your command-line wouldn't work (the pwd doesn't change) :-)

thanks
michi
> .. MUST exist ..
well, replace find by
    ls -lR
but it's slower, not as flexible etc. etc.
Still do not understand why you need to make things more complicated.

> .. your command-line wouldn't work ..
which did not work?
what did not work?
what is the problem?
both work for me since years, 1'st one only if there is a basename, 2'nd on any UNIX I've ever seen
Keep in mind, that depending on your shell you need to quote parameters and/or escape some special charactzers.

> ..(the pwd doesn't change)
true. Why should it change? There is no reason for it (it just wastes time, or think of directories which a links:)

I gave you 2 perfect solutions to your question, either accept them or give a detailled description with example what you want to do.
Avatar of witty

ASKER

ok - why the first line doesn't work:

"basename" is the name without directory.
so the "mv"-command can't work, because it won't find a file with this name in the current directory (because it is in a subdir!)!

and why I don't like it:
xargs is sooo slow, if you have a lot of (and I mean A LOT OF) files!!!

thanks though
michi
agree, basename is not for recursive use, sorry didn't test again.
Did you try my 2'nd example? Probably, depending on shell and/or sed version, needs to be changed to:
  find ./ -name \*.x | sed -e 's#\(.*\)\.x#mv & \1.y#'|sh
Avatar of The--Captain
This may be easier to understand - always works for me

find -name "*.x" | sed -n -e 's/^\(.*\)\.x$/\1/p' | awk '{print "mv -f " $0 ".x " $0 ".y"}' | sh -x

-Jon
Avatar of witty

ASKER

First of all: thanks to all your comments!

BUT:
these solution are all specific ones - the same I use today!!
(as you can read in my question) I'm looking for a *general solution* to do something like:
run_rec command -options parameters

thanks again
michi
ASKER CERTIFIED SOLUTION
Avatar of The--Captain
The--Captain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
witty:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.