1

I have tar.gz files like below in a directory df:

A.tar.gz
B.tar.gz
C.tar.gz
D.tar.gz
E.tar.gz
F.tar.gz
G.tar.gz

I also have text file move.txt with following columns information:

ID  Status      Status2     Status3     Status4     Status5         tar   sample
ID1 Negative    Negative    Negative    Negative    Negative    D.tar.gz    Sam1
ID2 Negative    Negative    Negative    Negative    Negative    A.tar.gz    Sam2
ID3 Negative    Negative    Negative    Negative    Negative    C.tar.gz    Sam3
ID4 Negative    Negative    Negative    Negative    Negative    F.tar.gz    Sam4

I want to move files in directory df to another directory based on their matching in move.txt file

I tried this way but didn't work:

for file in $(cat move.txt)
do 
    mv "$file" ~/destination 
done

Output should be in ~/destination directory:

D.tar.gz
A.tar.gz
C.tar.gz
F.tar.gz

Looks like I'm missing the column in the text file. Any help?

2

bash + awk solution:

for f in $(awk 'NR > 1{ print $7 }' move.txt); do 
    [[ -f "$f" ]] && mv "$f" ~/destination
done

Or with xargs:

awk 'NR > 1{ print $7 }' move.txt | xargs -I {} echo mv {} ~/destination

The crucial awk operation implies:

  • NR > 1 - start processing from the 2nd line (skip the 1st one as header)
  • print $7 - print the 7th field value $7 (tar column)
2

Answering my own question

Inside directory "df" I gave the following command. And it worked.

cat move.txt | xargs mv -t destination/

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.