Mac OS –根据文件名的一部分创建文件夹 [英] mac OS – Creating folders based on part of a filename

查看:331
本文介绍了Mac OS –根据文件名的一部分创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行macOS,正在寻找一种快速对成千上万个jpg文件进行排序的方法.我需要根据部分文件名创建文件夹,然后将那些文件移入其中.

I'm running macOS and looking for a way to quickly sort thousands of jpg files. I need to create folders based on part of filenames and then move those files into it.

简而言之,我想放置以下文件:

Simply, I want to put these files:

x_not_relevant_part_of_name.jpg
x_not_relevant_part_of_name.jpg

y_not_relevant_part_of_name.jpg
y_not_relevant_part_of_name.jpg

进入这些文件夹:

x
y

请记住,名称的"x"和"y"部分的长度可能不同.

Keep in mind that length of "x" and "y" part of name may be different.

maxOS中是否有针对此的自动解决方案? 我已经尝试过使用Automator和Terminal,但是我不是程序员,所以做得不好.

Is there an automatic solution for that in maxOS? I've tried using Automator and Terminal but i'm not a programmer so I haven't done well.

推荐答案

在终端中将find与bash参数替换一起使用可能会起作用:

Using find with bash Parameter Substitution in Terminal would likely work:

find . -type f -name "*jpg" -maxdepth 1 -exec bash -c 'mkdir -p "${0%%_*}"' {} \; \
-exec bash -c 'mv "$0" "${0%%_*}"' {} \;

这使用bash参数替换和find来使用与jpg匹配的任何文件名的前缀来递归创建目录(如果尚不存在).它使用第一个下划线(_)之前的字符,然后将匹配的文件移动到适当的目录中.要使用该命令,只需cd进入要组织的目录.请记住,在不使用maxdepth选项的情况下多次运行该命令会产生更多文件夹.使用maxdepth选项限制命令可以操作的深度".

This uses bash Parameter Substitution with find to recursively create directories (if they don't already exist) using the prefix of any filenames matching jpg. It takes the characters before the first underscore (_), then moves the matching files into the appropriate directory. To use the command simply cd into the directory you would like to organize. Keep in mind that without using the maxdepth option running the command multiple times can produce more folders; limit the "depth" at which the command can operate using the maxdepth option.

${parameter%word}
${parameter%%word}

该单词被扩展以产生一个模式,就像文件名扩展一样.如果该模式与扩展后的尾部匹配 参数的值,则扩展的结果为 匹配模式最短的参数(%"大小写)或 最长的匹配模式("%%"大小写)已删除.

The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted.

↳GNU Bash: Shell参数扩展

↳ GNU Bash : Shell Parameter Expansion

这篇关于Mac OS –根据文件名的一部分创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆