Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Issue:

I have a txt file that looks like this:

SRA1202321.sra
SRA123221.sra
SRA1209312.sra

I have a directory that looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading