Sponsored Content
Top Forums Shell Programming and Scripting Bash to move specific files to directory based on match to file Post 303032900 by cmccabe on Tuesday 26th of March 2019 10:52:58 AM
Old 03-26-2019
Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a folder in /home/cmccabe/f2. There may be multiple folders in /home/cmccabe/f2 but will only have one match in file. There also may be mulitple id's but always only one .vcf in /home/cmccabe/f1/variants.

When a match is found between the folder in /home/cmccabe/f2 and the R_ in file, then the id(s) in $2 will be found in /home/cmccabe/f1/variants as a .vcf. Each .vcf is then moved to the matching folder in /home/cmccabe/f2 in a the sub-folder variants. This is the last step of a procedure that I am stuck on. I have included an attempt in bash and included comments, but im sure there is a better way. Thank you Smilie.


file in /home/cmccabe/f1

Code:
IonCode_0007 19-0004-La-Fi
IonCode_0009 19-0005-Last-Firs
IonCode_0011 19-0008-LastN-FirstN
IonCode_0013 190320-Control
R_2019_03_12_13_59_54_user_S5-0271-100-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions

IonCode_0005 19-0000-LastName-FirstName
IonCode_0001 19-0001-Las-Fir
IonCode_0003 190319-Control
R_2019_03_12_11_10_20_user_S5-0271-99-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions

variants folder in /home/cmccabe/f1
Code:
19-0000-LastName-FirstName.vcf
19-0001-Las-Fir.vcf
190319-Control.vcf
19-0004-La-Fi.vcf
19-0005-Last-Firs.vcf
19-0008-LastN-FirstN.vcf
190320-Control.vcf

current structure of /home/cmccabe/f2

Code:
R_2019_03_12_11_10_20_user_S5-0271-99   ---parent directory ---
     - bam    --- sub-folder ---
     - qc     --- sub-folder ---
     - 19-0000-LastName-FirstName
             - variants
     - 19-0001-Last-Firs
             - variants
    - 190319-Control
             - variants
R_2019_03_12_13_59_54_user_S5-0271-100   ---parent directory ---
     - bam    --- sub-folder ---
     - qc     --- sub-folder ---
     19-0004-La-Fi
         - variants
     - 19-0005-Last-Firs
        - variants
     - 19-0008-LastN-FirstN
        - variants
     - 190320-Control.vcf
        -variants

desired structure of /home/cmccabe/f2

Code:
R_2019_03_12_11_10_20_user_S5-0271-99   ---parent directory ---
     - bam    --- sub-folder ---
     - qc     --- sub-folder
     - 19-0000-LastName-FirstName
              - variants
                   19-0000-LastName-FirstName.vcf
     - 19-0001-Last-Firs
             - variants
                  19-0001-Last-Firs.vcf
     - 190319-Control
             - variants
                   190319-Control.vcf
R_2019_03_12_13_59_54_user_S5-0271-100   ---parent directory ---
     - bam    --- sub-folder ---
     - qc     --- sub-folder ---
     - 19-0004-La-Fi
         - variants
            19-0004-La-Fi.vcf
     - 19-0005-Last-Firs
        - variants
          19-0005-Last-Firs.vcf
     - 19-0008-LastN-FirstN
        - variants
           19-0008-LastN-FirstN.vcf
     - 190320-Control.vcf
        -variants
            190320-Control.vcf

possible bash

Code:
for file in /home/cmccabe/f1/variants/*.vcf ; do
  bname=$(basename $file) # strip of path
  VCF="$(echo $bname|cut -d. -f1)" # remove .vcf extension
     f=$(printf '%s' /home/cmccabe/f1/file/${VCF})  ## # Find matching id
       FILE2=$(awk '{print $2}' $f') # set VCF lookup to column
          for RDIR in "$DIR"/R_2019* ; do FOLDER=${RDIR%%-v5.6*}; done  ## trim folder match in RDIR from -v5.6 and store in FOLDER
          if [[ $VCF = $FILE2 ]] # only execute file on match
                 then
                    mkdir -p /home/cmccabe/f2/$FOLDER/variants  ## create variants sub-folder
                   mv /home/cmccabe/f1/file/$VCF /home/cmccabe/f2/$FOLDER/$VCF/variants  ## move vcf to folder/id/variants
          fi  ## end if
done  ## close loop


Last edited by cmccabe; 03-27-2019 at 05:02 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

2. UNIX for Advanced & Expert Users

Watch directory and move specific file extensions

Hi all, This is actually more for my lazyness then anything else, but I think others might find it useful to use as well. Basically this is what I am trying to achieve... In my ubuntu home dir under Downloads is where firefox saves everything by default, now I know that you can manually... (3 Replies)
Discussion started by: STOIE
3 Replies

3. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

4. Shell Programming and Scripting

Move files to another directory based on name

Hi Folks, I have different type of file in my current directory. From my current directory i need to move the file which is start with csp_rules and if the file is having the string payg , then I need to move all this files to another directory /output/record. Please help me how to do this? ... (3 Replies)
Discussion started by: suresh01_apk
3 Replies

5. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

6. UNIX for Dummies Questions & Answers

move files that match specific conditions

Hi all, I'm new to this forum and bash scripting. I have the following problem, I need to copy some files (from one dir. to another) whose first 5 numbers (subjects' ID) match the directory names. Here a shortened version of what I'm trying to do: names=(32983_f 35416_f 43579_f) # these are... (6 Replies)
Discussion started by: ada1983
6 Replies

7. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

8. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

9. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

10. Shell Programming and Scripting

Bash to add portion of text to files in directory using numerical match

In the below bash I am trying to rename eachof the 3 text files in /home/cmccabe/Desktop/percent by matching the numerical portion of each file to lines 3,4, or 5 in /home/cmccabe/Desktop/analysis.txt. There will always be a match between the files. When a match is found each text file in... (2 Replies)
Discussion started by: cmccabe
2 Replies
All times are GMT -4. The time now is 03:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy