0

I have the directory and file structures like below.

path/A/1/a/11.txt
path/A/1/a/12.txt
path/A/2/a/21.txt
path/A/3/a/31.txt

I want to copy these files to another path but one level above. Please note sub-directory "a" is not available in the new path.

path2/A/1/11.txt
path2/A/1/12.txt
path2/A/2/21.txt
path2/A/3/31.txt

Since i have multiple directories and under which i have multiple files like these, i cannot simply use the command given below

cp -R path/A/1/a/*.txt path2/A/1/

Looking for inputs. Thanks

1
  • path2/A/[1-3]/ directories do not exist. We need to create them while doing copy itself. The note is just highlighting the difference between path and path2, thats all. In path, we have sub-directory called "a" which we dont have it in path2
    – Madhavi
    Mar 29, 2021 at 13:12

1 Answer 1

1

I'd use a loop:

from=some/path
to=some/other/path2

for dir in "$from"/A/*/; do
  dest=${dir/#$from/$to}
  mkdir -p "$dest"
  cp -v "$dir"/*/*.txt "$dest"
done

Note the trailing slash in the pattern in the for command: that restricts the results to directories only.

You must log in to answer this question.

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