Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a bash script that is iterating through sub directories where parts of the path are identical. Maybe best is to show an example:


  • /path/same/
  • /path2/same/
  • /path3/same/

Then I want to "do something" to all files contained in those directories and save them in another of those sub directories.


  • /path/another/
  • /path2/another/
  • /path3/another/

I guess it has to be done with some kind of

for i in /*/same/* ; do
    ... do something with $i ...

loop, but somehow all I try ends up not returning anything in $i.

any tips? thanks :)

share|improve this question
for i in /*/same/*; do searches only for directories two levels from the system root (/var/same, /home/same etc) - you probably want */same/* or ./*/same/*. – chronitis Feb 6 '13 at 14:08

Your example code looks good to me. How many files do you have in these directories? There is a limit for wildcard expansion. In this case you should use find, for example like that:

find /path*/same | while read i ; do echo "do something with file: $i" ; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.