Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:

Directories:

  • /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.

Results:

  • /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 ...
done

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.