LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-13-2011, 01:01 PM   #1
takayama
Member
 
Registered: Sep 2009
Posts: 93

Rep: Reputation: 0
Bash question (loop though folders and run a command)


I have the following scenario:
First i have a file in a directory (lets call the file temp)
It look like this(sort of):

Code:
apa
banan
nisse
bertil
In the same dir that i have this file, i also have directorys called
apa, banan, nisse, bertil (in other word, for every entry in the file tmp there are a folder with the same name).
In all of this folder there is a file (lets call it test) that i want to run a command on
(in this case just say i want to make a cat of each file

So what i want is to run some sort of loop that goes through everyone of the folders specified in the file and run a command on a file in each folder.
 
Old 02-13-2011, 01:05 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
If you want to run a command on files only, you do some thing like this


find /your/dir -type f -exec ls -ld '{}' \;

Replace the bold part with your command.
 
1 members found this post helpful.
Old 02-13-2011, 01:09 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
This (untested) snippet should get you close.
Code:
while read folderName; do
    cat ${folderName}/test
done < temp
--- rod.

Last edited by theNbomr; 02-13-2011 at 01:11 PM.
 
1 members found this post helpful.
Old 02-13-2011, 01:17 PM   #4
takayama
Member
 
Registered: Sep 2009
Posts: 93

Original Poster
Rep: Reputation: 0
thx
 
Old 02-14-2011, 06:20 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
Hi Takayama,

When your query has been resolved, go to top of your thread and in the Thread Tools click on "Mark this thread as solved". This is very useful for others.
 
  


Reply

Tags
shell script


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
Bash question fhsm Programming 8 06-12-2009 09:33 AM
bash question? kalleanka Programming 2 02-13-2006 10:57 AM
Bash question lnxduck Programming 3 09-30-2005 09:50 AM
BASH question cxel91a Programming 6 12-31-2003 04:42 AM
Bash question J_Szucs Programming 9 01-13-2003 04:19 AM


All times are GMT -5. The time now is 04:46 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration