1

I have a file with this format name Pocket_???_????_?.pdb_OUTPUT.txt and some directory with this format ????_?. I need to copy some of this file. For example, I have a list of file and I want to copy only this file in my final directory.

For example:

file.txt:

Pocket_001_1b47_A.pdb_OUTPUT.txt Pocket_002_1b47_A.pdb_OUTPUT.txt Pocket_001_2cl5_B.pdb_OUTPUT.txt Pocket_001_4rtl_A.pdb_OUTPUT.txt

Each of these files is within its own directory (1b47_A, 2cl5_B and 4rt1_A) and I would like to enter in each directory and copy them to another destination. The problem is that I do not know how to indicate the directory on the basis of the file name.

I try to use this scripting and this well works but it process all the directory ????_? and since they are many it takes a long time.

for dir in ????_?; 
    do cd $dir/; 
       while read file; 
       do cp $file /home/tommaso/cp_file/; done < /home/tommaso/file.txt ; 
    cd .. ; cd .. ; done

I would like to tell the script to search for files only in direcotry contained in the file name. How can I do that?

2 Answers2

2

This may be an option:

while IFS= read -r file; do 
  dir="${file%%.*}"
  cp "${dir#*_*_}/$file" /home/tommaso/cp_file/
done < file.txt
1

You can use awk to print directory/filename together with xargs and cp -t DIRECTORY SOURCE...:

awk -F"[_.]"  '{printf "%s_%s/%s\n",$3,$4,$0}' file.txt \
| xargs -d '\n' cp -t /home/tommaso/cp_file/
pLumo
  • 22,565