How to copy particular files from a multiple directories and paste in a new directory?

Dear all
I have a multiple directories, say for example org1, org2, org3 ..... org100 and each directory having a file namely dnaG.fasta . I need to copy all the dnaG.fasta file from each directory and paste in another directory fastconcatg. Therefore, my script has to copy dnaG.fasta file from each directory, add prefix to the file name like 1_dnaG.fasta, 2_dnaG.fasta ..... 100_dnaG.fasta and paste into the fastconcatg .
I have tried the following script but the script is not serving my purpose, It copy and paste only one file and rest is not copied and renamed finally end up with error. Kindly help me to do the same.

a=1
for i in **/dnaG.fasta
do
cp "$i" "$a"_"$i" fastcancatg
a=`expr $a + 1`
done

Thank you in advance.

Hello dineshkumarsrk,

A few general comments first:-

  • Your for loop has an extra * which might be confusing when you read this later on.
  • It would be better to use longer variable names to make it clear and to reduce the risk of other things that might call this getting confused.
  • The use of backticks ` is deprecated and the way you do it is wasteful too. You add extra processes where the shell can do integer calculations for you.
  • It would be nice to indent your code for clarity

I have made a few tweaks based on these and your code looks like this:-

count=1
for source_file in */dnaG.fasta
do
   cp "${source_file}" "${count}"_"${source_file}" fastcancatg
   ((count=$count+1))
done

The cp command you have is saying "Copy the source file and the source file again, but with a prefix of the counter and an underscore writing both files to the target directory fastcancatg" which doesn't make sense. The source file with the prefix does not exist. Can I presume that you want to copy the source file to the target directory with a prefix? Perhaps try this instead:-

count=1
for source_file in */dnaG.fasta
do
   cp "${source_file}" "fastcancatg/${count}"_"${source_file}"
   ((count=$count+1))
done

Is that nearer to what you are after?

Kind regards,
Robin

Thank you Robin for your detailed explanation. Here, I would like to repeat my question in clear manner.
I have a multiple directories, say for example org1, org2, org3 ..... org100 and each directory having a file namely dnaG.fasta . I need to copy all the dnaG.fasta file from each directory and paste in another directory fastconcatg . Therefore, my script has to copy dnaG.fasta file from each directory, add prefix to the file name like 1_dnaG.fasta, 2_dnaG.fasta ..... 100_dnaG.fasta and paste into the fastconcatg folder.

Okay, I'm sure we can help with that. A few questions for clarity though:-

  • Must the filename in the fastconcatg directory be the prefix of the directory that the source was found in?
  • What do we do for a directory that doesn't have a file of that name in it? We could leave an empty marker file, miss it completely or perhaps something else.

If your source files would be found be the command ls -l org*/dnaG.fasta then that's good. The problem about so many files is that you might exceed the command line length, but we can work on that if it become a problem.

For every directory/file found, the logic could be something like:-

  • Get the directory part
  • Get the number from the directory part
  • Copy the file to the target directory with the source directory number as a prefix

Does that match your need or have I missed the point?

Do you have any further attempts yourself that we can work through?

Kind regards,
Robin

1 Like

Your problem is the wrong destination in the cp command; it must be dir/file.
Together with a few improvements:

a=1
for i in */dnaG.fasta
do
  cp "$i" fastcancatg/"$a"_"$i" 
  a=$(( a + 1 ))
done

Previous suggestion should work as well. But a Posix count=$((count+1)) is better portable.

1 Like

Wouldn't it make more sense to use the directories' suffixes for the target files' prefixes?

for FP in org*/dnaG.fasta; do TMP=${FP#org}; echo cp $FP fastconcatg/${TMP%%/*}_${FP##*/}; done
cp org100/dnaG.fasta fastconcatg/100_dnaG.fasta
cp org1/dnaG.fasta fastconcatg/1_dnaG.fasta
cp org2/dnaG.fasta fastconcatg/2_dnaG.fasta
cp org3/dnaG.fasta fastconcatg/3_dnaG.fasta
cp org4/dnaG.fasta fastconcatg/4_dnaG.fasta

Remove the echo if happy with what it presents.

1 Like