AntiGMO
AntiGMO

Reputation: 1587

shell create new folder

I have many files' path, but I need to copy all files into other location /sample, and I want to copy files into different folders:

/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59329/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz
/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59329/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_2.fq.gz
/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59329/clean_111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz.total.info

I want to copy those files into AS34_59329 folder inside /sample

/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59328/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz
/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59328/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_2.fq.gz
/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59328/clean_111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz.total.info

I want to copy those file into AS34_59328 folder inside /sample

I write codes to scp all file into /sample folder, but I don't know how to put each files into different sub-directory, like:

/ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59328/clean_111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz.total.info

put into AS34_59328

  #! /bin/bash
   while read myline  
    do  
     for i in $myline
        do 
        if [ -f $i]; then
              #how to put different files into different sub-directory
              scp -r $i [email protected]:/sample
        fi
        done
    done < data.list

new changed part

      #! /bin/bash
      while read myline
      do
            for i in $myline
            do
              if [ -f $i ]
              then
              relname=$(echo $i | sed 's%\(/[^/][^/]*\)\{5\}/%%')
              echo $relname
              fi
            done
      done < /home/jesse/T11073_all_3254.fq.list

Upvotes: 0

Views: 402

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755054

It appears you need to strip the leading 5 components of the pathname off the filename. Since you don't have spaces in your names (the way you're using for i in $myline precludes that possibility), you can use:

#! /bin/bash
while read myline  
do  
    for i in $myline
    do 
        if [ -f $i ]
        then
            relname=$(echo $i | sed 's%\(/[^/][^/]*\)\{5\}/%%')
            scp -r $i [email protected]:/sample/$relname
        fi
    done
done < data.list

The regex is just a way of looking for a sequence of five sets of slash followed by one or more non-slashes plus one more slash and deleting them. Since slashes figure prominently in the search, I used % to mark the sections of the s/// operation instead.

For example, given the input:

/a/b/c/d/e/f/g

the output from the sed is:

f/g

Note that this code does not explicitly create directories on the remote machine; it just specifies where the file is to go. If you need to create them too, you will have to investigate ssh, probably, to run mkdir -p /sample/$(dirname $relname) on the remote machine (where the dirname operation can be run either locally or remotely).

Note that scp has a recursive copy mode (-r) which would simplify things considerably if you knew you needed to copy all the files from the local directory to the remote.

Upvotes: 1

Related Questions