简体   繁体   English

根据列表将不同的文件移动到新的单个目录

[英]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 需要“正常”的filesdirectories名称(在源 txt 文件中),即没有特殊字符,如引号( '" )、反引号、 $(){}和任何其他将被解释的字符shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据文件名标记将文件移动到其他目录 - Move files to different directories based on file name tokens 根据扩展名将文件移动到目录 - Move files to directories based on extension 基于文件名的目录,然后将关联文件移动到正确的目录以获取随机名称 - directories based on the filename and then move the associated files to the correct directories for random name 将多个文件从文件夹移动到目录列表(撤消移动命令) - Move multiple files from a folder to list of directories (Undo a move command) 将文件移动到自己的目录中 - Move files into their own directories 如何在多个不同目录中移动许多文件(在Linux上) - How to move many files in multiple different directories (on Linux) 如何在从多个目录/文件编译内核模块的同时创建新目录并向其中移动.o文件 - How to create new directories and move .o files to it while compiling a kernel module from multiple directories/files linux创建目录并将相应文件移动到目录 - linux create directories and move corresponding files to the directories 将多个文件移动到 linux 中的目录 - Move multiple files to directories in linux 根据包含和排除特定文件列出Linux中的目录 - List directories in Linux based on inclusion and exclusion of specific files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM