如何解析 .txt 文件并将所有文件移动到另一个目录

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

问题:

我有一个如下所示的 txt 文件:

SRA1202321.sra
SRA123221.sra
SRA1209312.sra

我有一个如下所示的目录:

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

我想将所有文件从 .txt 移动到另一个目录,并留下我不想要的文件。我是 bash 的新手,所以我在做这件事时遇到了一些麻烦。

我试过:

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

但似乎有新行字符被插入,所以它说不存在目录。我目前正在查看 bash 脚本并且也有这个想法:

#!/bin/bash

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

但是我不确定如何删除新行?部分原因是我很困惑为什么最后给出输入文件,以及我是否可以像 python 中那样调整变量。对不起,如果它有点混乱。

感谢您的帮助

可能是CRLF换行的问题。您可以使用 (in bash):

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

只要 metasamples.txt 中的文件名不包含任何有问题的字符

,它就可以工作