[英]Move different files to new, individual directories based on list
I have a directory containing >500 files styled like this:我有一个包含 >500 个文件的目录,其样式如下:
GCA_000007345.1.fa.gz
GCA_000681355.2.fa.gz
GCA_000802095.1.fa.gz
I also have a tab-delimited txt file genomes_retrieved.txt where in column 1 is the file name and column 2 is the name of the directory that should be made and where the file should be moved to.我还有一个制表符分隔的 txt 文件基因组_retrieved.txt ,其中第 1 列是文件名,第 2 列是应该创建的目录的名称以及文件应该移动到的位置。
GCA_000007345.1.fa.gz Methanosarcina_acetivorans_C2A
GCA_000681355.2.fa.gz Peptococcaceae_bacterium_SCADC1_2_3
GCA_000802095.1.fa.gz Peptococcaceae_bacterium_BICA1-7
I tried to follow this post here and use awk and xargs to execute in the directory where my files are.我尝试在此处关注此帖子并使用 awk 和 xargs 在我的文件所在的目录中执行。 I create the directories
我创建目录
awk 'NR > 1{ print $2 }' ../genomes_retrieved.txt | xargs -I {} mkdir {}
but I am not sure how to handle the mv part correctly.但我不确定如何正确处理 mv 部分。
awk 'NR > 1{ print $1 }' ../genomes_retrieved.txt | xargs -I {} mv
Any advice?有什么建议吗?
You can use below trick to run a "scriplet" that uses positional arguments ( $1
, $2
, ...) for the each-line words:您可以使用下面的技巧来运行使用位置 arguments (
$1
, $2
, ...) 的每行单词的“scriplet”:
xargs -n2 sh -c 'mkdir -p "${2}" && mv "${1}" "${2}"' -- < genomes_retrieved.txt
Update: an easy way to see what you're actually running is replacing sh -c
by sh -xc
.更新:查看您实际运行的内容的一种简单方法是将
sh -c
替换为sh -xc
。
Note also that above CLI requires "sane" files
and directories
names (in source txt file), ie with no special characters like quotes ( '
, "
), backtics, $
, (){}
and any other character that would be interpreted by the shell.还要注意,上面的 CLI 需要“正常”的
files
和directories
名称(在源 txt 文件中),即没有特殊字符,如引号( '
、 "
)、反引号、 $
、 (){}
和任何其他将被解释的字符shell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.