Go n directories down

directory

I would like to perform some processing on files that are 6 directories down in my tree (for example copying). The directory names on each directory level are entirely irregular (random numbers and letters), and at the last level there are something like 20 different directories where my files are.

An example of one case:

cp /000157/DZW123/AHG345/DFR987/000RE7/0025RTZ/file.xxx  /destination/file.xxx

Same case with a different last level (among 19 others):

cp /000157/DZW123/AHG345/DFR987/000RE7/1298FGT/file.xxx  /destination/file.xxx

But entirely different for many other cases:

cp /001154/CVS456/SAQ452/FRO921/000VG5/0032RRT/file.xxx  /destination/file.xxx

Because of the different cases, a loop would not help. It appears best if there were a solution that allows me to go directly 6 directories down in the tree for every single branch (irrespective of the naming). I tried cd +n but this does not work.

Best Answer

Use find for this job

find / -mindepth 7 -maxdepth 7 -iname '*.txt' -exec echo cp {} /destination \;

Modify *.txt pattern to your needs, and remove echo if you like what you see on the screen.

Related Question