1

I am still a beginner for bash scripting.I am trying to loop copy command to run through multiple directories. I have set of directories, P1, P2, P3,..P60. Inside each directory there are three folders 'A', 'M' and 'out'. I want to copy files from A and M folders and paste them into "out" folder. Then repeat it through all the 60 directories.

'AL' has a file called PIL_CBR.nii and 'M' has a file called PIL_MO.nii.

Thank you!

2 Answers 2

0

I assume that P1, P2, ..., P60 directories are in the same folder. Then, you just need to loop through all directories in this folder and run cp. Running the following script in this folder should do.

for d in P*/ ; do
  echo "$d"
  cp $d/A/* $d/M/* $d/out/
done

You're probably aware of it that here * expands to file and directory names in current directory.

7
  • 1
    Thank you so much. I should have mentioned that in the folder A there are other files and I want to copy the particular file I was listed in the question. I am so sorry about that. How should I mention the that particular file with the particular directory. /A/PIL_CBR.nii. Thank you
    – Dona
    Jun 29, 2022 at 16:00
  • You are welcome. If my answer helps you, could you accept my answer to give me some reputation? The other answer is similar but you can see that I really need some reputation. Jun 29, 2022 at 16:28
  • 1
    I am sorry I was trying to work on it. Your answer works. But I cannot copy the file. I am sorry bringing out the the issue before accepting it.
    – Dona
    Jun 29, 2022 at 16:32
  • Why can't you copy the file? What issue are you facing? Jun 29, 2022 at 18:38
  • I need to copy that one particular file in A folder. It has other files. So I have to specify the file name along with the folder. M folder has only one file and it copies it. I do not want to copy the entire A folder, just the file I mentioned. Thank you
    – Dona
    Jun 29, 2022 at 18:47
0

Assuming that your directories are named exactly as you say, are in the current directory, and don't contain thousands of files each, something like this should work:

for dir in P{1..60}/{A,M} ; do
  [ -d "$P" ] || continue
  cp "$dir"/* "${dir%[AM]}out/"
done

Using the * wildcard to select your directories may work but risks over matching and changing things you don’t expect.

This should only match the exact set of items you describe, but as such it is quite specific to the case you describe, and the approach may need to be modified for different input.

If you have file name clashes in your two directories the contents from M will replace that from A. This could be handled, but wasn't in your original specification.

1
  • Thank you so much. I forgot to mention that in the folder A there are other files and I want to copy the particular file I was listed in the question. I am so sorry about that. How should I mention the that particular file with the particular directory. /A/PIL_CBR.nii. Thank you
    – Dona
    Jun 29, 2022 at 16:02

You must log in to answer this question.

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