0

I'm not sure how to handle directories of directory scenario in shell.

I have folder structure as below.
Directory structure:

/DirA/DirA1/DirA11/*.txt  
/DirA2/DirA21/*.txt
/DirA3/DIrA31/*.txt'

I'm new to shell scripting, not able to figure out how to read these text files.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Krish
  • 321
  • 2
  • 9
  • 3
    What do you mean by "read these text files" ? what do you want to do with them ? – Nir Alfasi Oct 02 '15 at 09:20
  • It's better to ask a question when you have a problem than before trying.. As a tip, using two `find` should get you going: `find . -type d -iname DirA* -exec find {} -iname *.txt \;`. This command gives you all the `*.txt` files under a path that has `DirA*` in it. – Baris Demiray Oct 02 '15 at 09:21
  • @BarisDemiray You need to quote the patterns to prevent the shell from expanding them. – chepner Oct 02 '15 at 13:17
  • Thanks for your response. I'm able to resolve it on looking http://stackoverflow.com/questions/2107945/how-to-loop-over-directories-in-linux – Krish Oct 02 '15 at 19:40

2 Answers2

2

You can use the find command to process all files with certain properties in a directory tree. For example,

find /DirA* -name '*.txt' 2>/dev/null

would list all files named *.txt inside the trees you are mentioning. Note that if you use wildcards in the name mask, you need to single-quote them in order to protect them from the shell.

Zloj
  • 2,235
  • 2
  • 18
  • 28
Hellmar Becker
  • 2,824
  • 12
  • 18
  • Thanks for your response. I'm able to resolve it, on looking http://stackoverflow.com/questions/2107945/how-to-loop-over-directories-in-linux – Krish Oct 02 '15 at 19:42
2
for f in /DirA/DirA1/DirA11/*.txt /DirA2/DirA21/*.txt /DirA3/DIrA31/*.txt; do
   # do stuff with $f
done
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks for response. I'm able to resolve it, on looking at http://stackoverflow.com/questions/2107945/how-to-loop-over-directories-in-linux – Krish Oct 02 '15 at 19:42