LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   is there a way to make a generic command run recursivley? (https://www.linuxquestions.org/questions/linux-newbie-8/is-there-a-way-to-make-a-generic-command-run-recursivley-769715/)

dsollen 11-17-2009 11:19 AM

is there a way to make a generic command run recursivley?
 
This isn't too important, it's just a thought I had today. I was messing with some software where every file had an extra suffix I needed removed. I didn't know of any program that would automatically truncate a filename so I wrote a basic script to do it. The only problem is my script works on all files in a directory, but not on all directories in my software.

I'm sure I could get around that easily enough, but it left me thinking that it would be convenient if there was a way to run the script recursively without having to modify the script. It seems to me that the need would come up often enough that a method should already exist, but I don’t know what it is. So for the sake of my own curiosity, what would be the standard method for making a generic command run recursively on all subfolders?

sping 11-17-2009 11:24 AM

You can use find to run over the file system and a custom command that is executed for each file:

Code:

find -type f -exec bash -c "echo 'Filename is {}'" \;
see

Code:

man find
for more.

GrapefruiTgirl 11-17-2009 11:25 AM

@ dsollen,

why not show us the script you're using, and someone might be more accurately able to suggest a modification on what you're currently doing, to get the results you seek? It sounds like, if you don't want to modify the script, you'd need a for or while loop..

Sasha

tredegar 11-17-2009 11:27 AM

See this thread about using find to find files (or even directories, the process is easily adapted) to remove them.

Then, instead of rm'ing them apply your script to them.

i92guboj 11-17-2009 11:35 AM

There's no generic way to do recursions, it wouldn't be too complex to do so with bash though, but for that we would first need to know a few details on how exactly is the recursion to be implemented.

However, it's really not the simplest way around this case, you only need find to do this, for example:

Code:

find /path/to -iname foo_\*.mp3 | while read file; do mv "$file" "${file/foo_/}"; done

dsollen 11-17-2009 01:17 PM

Thank you for the prompt answer. It looks like find was the command I was looking for. I'm trying to learn all the little programs that come packaged with Linux that are useful for basic scripting, and I figure this one is probably pretty important.

Incidentally I changed my script and it worked correctly. Of course now that I fixed that little problem I have a new compilation issue. It seems every time I stat complaining about "rpm hell" I run into a program I have to install without a RPM and get a new appreciation for them. One day I may be so lucky as to install a package with fancy tools like yum. Yep that would be nice... :)

tredegar 11-17-2009 01:36 PM

Quote:

It looks like find was the command I was looking for.
Hmmm. You need to know what is available (and is are a LOT) and how to combine linux / unix simple commands (That "do simple things, but do them well") into scripts with truly awesome usefulness. It takes time, but I expect you'll get there. One of my personal favourites is here (Read on down in the link for a much improved script by Disillusionist)

yum ? RPM ? Been there, tried that :(

apt-get (on all debian-based distros) = Bliss :) [tredegar ducks incoming missiles].

Pleased you got your problem solved.


All times are GMT -5. The time now is 01:28 PM.