[英]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.