Home > Net >  how to parse through a .txt file and move all files into another directory
how to parse through a .txt file and move all files into another directory

Time:05-19

Issue:

I have a txt file that looks like this:

SRA1202321.sra
SRA123221.sra
SRA1209312.sra

I have a directory that looks like this:

SRA1202321.sra
SRA123221.sra
SRA1209312.sra
random.sra
random.sra

I want to move all of the files from the .txt to a a different directory and leave behind the ones that I do not want. I am new to bash so Im having a bit of trouble doing this.

I have tried:

cat ~working/metasamples.txt |
xargs mv ~/ncbi/public/sra/metasamples/

but it seems there is new lines characters being catted in so it says no directory exists. I am currently looking at bash scripts and have this idea also:

#!/bin/bash

while read p; do
  mv "$p" ~/ncbi/public/sra/metasamples/
done <~/working/metaSRAfromjsoncorrected.txt

However Im not sure how to remove new lines? Part of that has to do with im confused why input file is given last and if I can just adjust the variable like in python. Sorry if its a bit confusing.

Thanks for any help

CodePudding user response:

It's probably a problem of CRLF line endings. Instead of cat you can use (in bash):

sed $'s/\r$//' ~working/metasamples.txt |
xargs -I {} mv {} ~/ncbi/public/sra/metasamples/

That will work as long as the filenames in metasamples.txt don't contain any problematic character

  •  Tags:  
  • bash
  • Related