Linux: move/copy same name files to one folder
2
0
Entering edit mode
4.5 years ago
3335098459 ▴ 30

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 • 1.5k views
ADD COMMENT
2
Entering edit mode
4.5 years ago

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 COMMENT
2
Entering edit mode
4.5 years ago
ATpoint 82k

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 COMMENT

Login before adding your answer.

Traffic: 2563 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6