Hi I have multiple files in different folders with the same name and extension.
For example: There are 460 folders and each folder has one file with the name of snps.vcf. I want to copy/move these files to one folder and later on, I will do some analysis that I need to do.
I have tried:
find -type f -name "*.vcf" -exec cp {} /home/AWAN/try
but this code overwrites the files and only one file remains there in the end.
I have tried rename but I don't know how to select multiple files by find command then rename. Even with the mmv I couldn't find the possible solution.
2 answers
use file's inode id to create a new name
find dir1/dir2 -type f -name "snps.vcf" -printf "cp %p /home/AWAN/%i.vcf\n" | bash
Use a while loop that iterates through all files found by find:
#!/bin/bash
## counter starting at 1
COUNTER=1
find . -name "snps.vcf" \
| while read p
do
Basename=$(basename ${p%.vcf})
mv $p ./targetdirectory/${Basename}${COUNTER}.vcf
COUNTER=$((COUNTER + 1))
done
This will scan for snps.vcf in the directories and then move them to targetdirectory appending an increasing number like snps1.vcf... until all files have been moved. Try to understand the code and ask if there are things unclear. If you are not familiar with | this is a pipe symbol, very important basic operator, google it if unknown.
Log in to answer this question.