Question: Linux: move/copy same name files to one folder
0
gravatar for 3335098459
26 days ago by
333509845920
333509845920 wrote:

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.

snp sequence • 140 views
ADD COMMENTlink modified 26 days ago by Pierre Lindenbaum124k • written 26 days ago by 333509845920
2
gravatar for Pierre Lindenbaum
26 days ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum124k wrote:

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
ADD COMMENTlink modified 26 days ago • written 26 days ago by Pierre Lindenbaum124k
2
gravatar for ATpoint
26 days ago by
ATpoint25k
Germany
ATpoint25k wrote:

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.

ADD COMMENTlink modified 26 days ago • written 26 days ago by ATpoint25k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 946 users visited in the last hour