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:



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?


This may be an option:

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

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.