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
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
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?