4

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.

2
  • 1
    What about for file in /*/*/*/*/*/*/file.xxx; do...? Sep 19, 2014 at 15:05
  • I tried this with both cd and cp. For cd it gives an error "ambiguous", and cp does not work. Btw.: the files (file.xxx) are also entirely irregular. Sep 19, 2014 at 15:31

2 Answers 2

5

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.

3

Assuming you have files only in the final level of your directory structure, you can use the find with xargs to copy the files as below.

find . -type f -mindepth 7 -maxdepth 7 -print0 | xargs -0 cp -t /some/destination/directory

This one works fine with spaces in the sub directories names and the spaces in file names too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .