简体   繁体   English

BASH使用for,mv和grep的单行字母大容量文件排序

[英]BASH one-line alphabetical mass file sort using for, mv, and grep

Problem 问题

I've got thousands of files with the format "^[[:digit:]]\\{4\\} - [[:alpha:]].*" , for exampe: 7958 - a3ykof zyimeo3.txt. 我有成千上万个文件,格式为"^[[:digit:]]\\{4\\} - [[:alpha:]].*" ::: "^[[:digit:]]\\{4\\} - [[:alpha:]].*" ,例如:7958-a3ykof zyimeo3.txt。 I'm trying to simply move them into folders alphabetically beginning with the first alpha-character after the hyphen. 我试图将它们简单地从连字符后的第一个字母字符按字母顺序移动到文件夹中。

I feel like I'm so close to getting this to happen the way I want but there's a (hopefully simple) problem. 我觉得我已经很接近要按照我想要的方式来实现它了,但是有一个(希望很简单)的问题。

I tested the commmand with echo first to make sure it grabs the correct information. 我先用echo测试了该命令,以确保它可以获取正确的信息。 Then I tried to execute it for real with mv. 然后我尝试用mv真正执行它。 I've included some examples below based on this list of files: 基于此文件列表,我在下面提供了一些示例:

1439 - a74389 josifj3oj.txt
3589 - Bfoei 839982 3il.txt
4719 - an38n8f n839mm20 mi02.txt
6398 - b39ji oij3o8 j2o.txt
9287 - A2984 j289jj9 oiw.txt
.... several thousand more files

Examples 例子

This works 这有效

This lists all the files starting with the letter "a" (after the 4 digits-space-hyphen-space pattern in the beginning): 这列出了所有以字母“ a”开头的文件(在开头的4位数字-空格-连字符-空格模式之后):

for i in "$(ls | grep -i "^[[:digit:]]\{4\} - a")"; do echo "$i"; done

This fails 这失败了

This doesn't put all the files starting with the letter "a" (after the 4 digits-space-hyphen-space pattern) in the "A" folder: 这不会将所有以字母“ a”开头(在4位数字-连字符-空格模式之后)的文件放入“ A”文件夹中:

for i in "$(ls | grep -i "^[[:digit:]]\{4\} - a")"; do mv "$i" A; done

I expected this second command to move each file named "#### - a*" or "#### - A*" to the folder named A. But it sees it as one big string/filename joined by "\\n". 我期望第二个命令将每个名为“ ####-a *”或“ ####-A *”的文件移动到名为A的文件夹中。但是它将其视为一个由“ \\ n”连接的大字符串/文件名”。

Here's an example error message: 这是示例错误消息:

mv: cannot stat '1439 - a74389 josifj3oj.txt\n9287 - A2984 j289jj9 oiw.txt\n2719 - an38n8f n839mm20 mi02.txt': No such file or directory

Does anybody know what I'm missing? 有人知道我在想什么吗?

Edit 编辑

Between @alvits's answer and @chepner's and @courtlandj comments, what worked flawless for me was this: 在@alvits的答案与@chepner和@courtlandj的评论之间,对我而言完美无缺的是:

for directory in {A..Z}; do
    mkdir -p "$directory" &&
    find . -iregex "./[0-9]* - ${directory}.*" -exec mv -t "$directory" {} +; 
done

Here's the simplest way to do it. 这是最简单的方法。

for directory in {A..Z}; do
    mkdir "$directory" &&
    find . -iregex "./[0-9]* - ${directory}.*" -exec mv "{}" "$directory" \;
done

The for loop will query for filenames according to each directory they belong. for循环将根据文件名所属的每个目录查询文件名。

The find command will find the files and move them to the directory. find命令将查找文件并将其移动到目录。

BASH has RE-like globbing, and sequence creation, built-in. BASH内置了类似RE的glob和序列创建功能。 You can make use of it something like this: 您可以像这样使用它:

for i in {{A..Z},{a..z}}; do
  mkdir "${i}" && mv [0-9][0-9][0-9][0-9]" - ${i}"*" "${i}"
done

You notice the four repetitions of the digits, and yeah it looks clumsier than a normal RE like [0-9]{4} . 您会注意到数字的四个重复,是的,看起来比[0-9]{4}类的常规RE更加笨拙。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM