LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash question (loop though folders and run a command) (http://www.linuxquestions.org/questions/programming-9/bash-question-loop-though-folders-and-run-a-command-862428/)

takayama 02-13-2011 01:01 PM

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.

vikas027 02-13-2011 01:05 PM

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.

theNbomr 02-13-2011 01:09 PM

This (untested) snippet should get you close.
Code:

while read folderName; do
    cat ${folderName}/test
done < temp

--- rod.

takayama 02-13-2011 01:17 PM

thx

vikas027 02-14-2011 06:20 AM

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.


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