Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-17-2009, 11:19 AM   #1
Registered: Jun 2009
Posts: 56

Rep: Reputation: 15
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?
Old 11-17-2009, 11:24 AM   #2
LQ Newbie
Registered: Sep 2009
Location: Berlin, Germany
Distribution: Gentoo
Posts: 3

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

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

man find
for more.
Old 11-17-2009, 11:25 AM   #3
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ 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..


Last edited by GrapefruiTgirl; 11-17-2009 at 11:26 AM.
Old 11-17-2009, 11:27 AM   #4
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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.
Old 11-17-2009, 11:35 AM   #5
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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:

find /path/to -iname foo_\*.mp3 | while read file; do mv "$file" "${file/foo_/}"; done
Old 11-17-2009, 01:17 PM   #6
Registered: Jun 2009
Posts: 56

Original Poster
Rep: Reputation: 15
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...
Old 11-17-2009, 01:36 PM   #7
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
listing folders recursivley jukebox55 Linux - Newbie 8 02-29-2008 11:13 AM
How to Make a command run at stratup custangro Linux - Newbie 1 03-03-2007 11:11 AM
How do I make a command run as root at boot up under Fedora 3? versaulis Linux - Software 5 02-10-2005 06:22 PM
make a command run at startup? cheater1034 Linux - General 2 09-12-2004 05:51 AM
where do i add a command to make it run on boot stuart Linux - Newbie 4 06-21-2004 06:03 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration