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? |
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 {}'" \; Code:
man find |
@ 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 |
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. |
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 |
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... :) |
Quote:
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. |