Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

execute command recursively in subdirs

Posted on 2002-04-11
11
Medium Priority
?
672 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:witty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6933465
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
0
 
LVL 1

Author Comment

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

thanks
witty
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6934117
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?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 1

Author Comment

by:witty
ID: 6934833
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6934901
> .. 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.
0
 
LVL 1

Author Comment

by:witty
ID: 6935088
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6935109
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
0
 
LVL 16

Expert Comment

by:The--Captain
ID: 6949423
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
0
 
LVL 1

Author Comment

by:witty
ID: 6950111
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
0
 
LVL 16

Accepted Solution

by:
The--Captain earned 150 total points
ID: 6950224
I think I know what you're saying - you want to just be able to run a cmdline within each directory recursively?

Why not:


#!/bin/bash
find -type d | awk "{print \"export STCD=`pwd` ; cd \" \$0 \" ; $1 ; cd $STCD\"}" | sh -x

Just use the first argument as the command to run and it will run it in every directory recursively - don't forget the quotes if your cmdline has spaces in it...

I called the script rec_cmd and typed:

rec_cmd "touch 'my balls'"

and 'my balls' appeared in every directory recursively...

-Jon

0
 

Expert Comment

by:CleanupPing
ID: 9089092
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.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question