-   Programming (
-   -   Bash question (loop though folders and run a command) (

takayama 02-13-2011 12: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):



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 12: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 12:09 PM

This (untested) snippet should get you close.

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

--- rod.

takayama 02-13-2011 12:17 PM


vikas027 02-14-2011 05: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 07:16 AM.