简体   繁体   English

如何根据 linux 中的名称将文件移动到新目录?

[英]How do I move files to a new directory based on their name in linux?

I am new to linux and need to solve a problem where I have a directory with many files in the following pattern:我是 linux 的新手,需要解决一个问题,即我的目录包含以下模式的许多文件:

./data/xaa_basic
./data/xaa_extra
./data/xab_basic
./data/xab_extra
./data/xac_basic
./data/xac_extra
... (+400)

How do I create a script to organize them into subdirectories as follows?如何创建脚本将它们组织到子目录中,如下所示?

./data/1/xaa_basic
./data/1/xaa_extra
./data/2/xab_basic
./data/2/xab_extra
./data/3/xac_basic
./data/3/xac_extra
... (+400)

You might try something like (assuming your current working directory is the toplevel data ):您可以尝试类似(假设您当前的工作目录是顶级data ):

i=0; for f in x{a..d}{a..d}; do mkdir -p $((++i)); mv ${f}_{extra,basic} $i; done

Tweak the bounds to match the files you have.调整边界以匹配您拥有的文件。 You might also just use a more shotgun approach and do:您也可以只使用更霰弹枪的方法并执行以下操作:

 i=0; for f in x*_extra; do mkdir -p $((++i)); mv "${f%_extra}"_{extra,basic} $i; done

If you have any files of the form xxx_basic that do not have a matching _extra file, they will get skipped.如果您有任何 xxx_basic 形式的文件没有匹配的_extra文件,它们将被跳过。 Modify as needed.根据需要进行修改。

暂无
暂无

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

相关问题 根据目录名称移动文件(Linux / Debian /#!) - Move files based on directory name (Linux / Debian / #!) 如何在Linux中根据名称在具有相应名称的文件夹中移动文件? - How to move files in Linux based on its name in a folder with a corresponding name? 如何在Linux的目录中合并多个文件,以使每个文件数据都放在新列中? - How do I combine multiple files in a directory in linux such that each files data is placed in a new column? 如何对目录中的所有文件执行命令,并将每个文件的输出移至新文件夹? - How do I conduct a command on all the files in a directory and move the output for each file to a new folder? 我如何从另一个目录访问目录中的文件(linux) - how do i access files in directory from another directory (linux) 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 如何通过Linux中的shell脚本创建新目录? - How do I create a new directory through a shell script in Linux? 使用Linux脚本根据文件名模式查找和移动文件 - find and move files based on file name pattern using linux script 如何根据特定字符长度范围移动文件? - How do I move files based on a certain character length range? 如何通过查看文件名的一部分来移动文件 - How do I move files by looking at part of a file name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM