LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-17-2009, 12:19 PM   #1
dsollen
Member
 
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, 12:24 PM   #2
sping
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:

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

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

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

Last edited by GrapefruiTgirl; 11-17-2009 at 12:26 PM.
 
Old 11-17-2009, 12:27 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,087

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
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, 12:35 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
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
 
Old 11-17-2009, 02:17 PM   #6
dsollen
Member
 
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, 02:36 PM   #7
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,087

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


Reply


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 12:13 PM
How to Make a command run at stratup custangro Linux - Newbie 1 03-03-2007 12:11 PM
How do I make a command run as root at boot up under Fedora 3? versaulis Linux - Software 5 02-10-2005 07:22 PM
make a command run at startup? cheater1034 Linux - General 2 09-12-2004 06:51 AM
where do i add a command to make it run on boot stuart Linux - Newbie 4 06-21-2004 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:31 PM.

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