将文件夹中的第一个文件从大写替换为较低的情况
我正在尝试将3,000左右的.svg文件从Capledcase转换为骆驼。
当前:
-Folder
--FileName1
--FileName2
--FileName3
目标:
-Folder
--fileName1
--fileName2
--fileName3
如何使用终端将第一个字符上的壳体更改为小写?
目前,我一直在尝试这些行:for *.svg中的f code>;做mv -v“ $ f”“ $ {f:1}”;完成
文件夹中的所有文件以字母或号码开始。
I'm trying to convert 3,000 or so .svg files from CapitalCase to camelCase.
Current:
-Folder
--FileName1
--FileName2
--FileName3
Goal:
-Folder
--fileName1
--fileName2
--fileName3
How can I use terminal to change the casing on the first character with to lowercase?
Currently I've been trying something along these lines: for f in *.svg; do mv -v "$f" "${f:1}"; done
All files in the folder start with a letter or number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在
ZSH
中使用ZMV
:这将通过任意数量的目录级别进行复发,并且可以处理名称碰撞和其他边缘案例。
其中一些作品:
-n
:无执行。使用此选项,ZMV
将只报告其将进行的更改。这是一种干燥的运行,可用于测试模式。当您准备实际更改名称时,请删除
-n
。-v
:冗长。-Q
:预选赛。表示源模式具有Glob限定符(在我们的情况下(。)
)。'(**/)(?)(*。svg)(。)'
:源模式。这只是一个常规的ZSH
球模式,分为带有括号的组。基础模式是**/?*。svg(。)
。碎片:(**/)
:目录和子目录。这将匹配任意数量的目录级别(仅影响当前目录,请参见下文)。(?)
:在文件名的开始时匹配一个字符。我们稍后将其转换为小写。(*。svg)
:匹配文件名的其余部分。(。)
:仅常规文件。这是一个ZSH
Glob预选赛;ZMV
由于-Q
选项而将其识别为预选赛,而不是分组。。
限定符将匹配限制为常规文件,因此我们不会尝试重命名目录。'$ 1 $ {(l)2} $ 3'
:目标模式。源模式中的每个分组以$ 1,2美元等的速度订购。$ 1
:目录。这可能包含多个级别。$ {(l)2}
:文件名中的第一个字母转换为小写。这使用l
参数扩展标志来更改案例。l
扩展修饰符也将工作:$ 2:L
。转换可以处理非ASCII字符,例如
éxito
将成为
éxito
。$ 3
:文件名的其余部分,包括扩展名。变化
这只会在当前目录中更改文件:
以下版本中的源模式仅匹配以大写字母开头的文件。由于
ZMV
实用程序如果源和目标匹配不重命名文件,这不是严格必要的,但是它会稍微更高:更多信息
ZMV
文档:https:///zsh.sourceforge.io/doc/release/ user-contributions.html#index-zmv
zsh
参数扩展标志:https://zsh.sourceforge.io oio.io/doc/release/扩展
https://grml.org/zsh/zsh/zsh/zsh-lovers.html
This can be done very succinctly in
zsh
withzmv
:This will recurse through any number of directory levels, and can handle name collisions and other edge cases.
Some of the pieces:
-n
: no execution. With this option,zmv
will only report what changes it would make. It's a dry run that can be used to test out the patterns.Remove the
-n
when you're ready to actually change the names.-v
: verbose.-Q
: qualifiers. Indicates that the source pattern has a glob qualifier (in our case(.)
).'(**/)(?)(*.svg)(.)'
: source pattern. This is simply a regularzsh
glob pattern, divided into groups with parentheses. The underlying pattern is**/?*.svg(.)
. The pieces:(**/)
: directories and subdirectories. This will match any number of directory levels (to only affect the current directory, see below).(?)
: matches a single character at the start of the file name. We'll convert this to lowercase later.(*.svg)
: matches the rest of the file name.(.)
: regular files only. This is azsh
glob qualifier;zmv
recognizes it as a qualifier instead of a grouping because of the-Q
option. The.
qualifier limits the matching to regular files so that we don't try to rename directories.'$1${(L)2}$3'
: destination pattern. Each of the groupings in the source pattern is referenced in order with $1, $2, etc.$1
: the directory. This could contain multiple levels.${(L)2}
: The first letter in the file name, converted to lowercase. This uses theL
parameter expansion flag to change the case.The
l
expansion modifier will also work:$2:l
.The conversion can handle non-ASCII characters, e.g.
Éxito
wouldbecome
éxito
.$3
: the rest of the file name, including the extension.Variations
This will only change files in the current directory:
The source pattern in the following version will only match files that start with an uppercase letter. Since the
zmv
utility won't rename files if the source and destination match, this isn't strictly necessary, but it will be slightly more efficient:More information
zmv
documentation:https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#index-zmv
zsh
parameter expansion flags:https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion-Flags
zsh
notes, including a bunch ofzmv
examples:https://grml.org/zsh/zsh-lovers.html
在bash中求解,测试和工作正常,请小心您处理的文件。
在当前目录中重命名该脚本所在的文件(然后是第1 arg
。
)或提供路径,如果是大写的,则是对第一个字母的较低命名,但如果它是少数字母是一个数字,必须提供参数:使用:
bash script.sh folder .txt
注意:现在在运行脚本和重命名后,在这里,名称在某些文件中,可能会重合,在这种情况下可能会发生冲突。以后可以修复并更新此脚本。
Solving in bash, tested and working fine, be careful though with your files you working on.
Renaming files in current directory where this script is (1st arg then'd be
.
) or provide a path, it's do lower-casing of the first letter, if it was uppercase, and yet nothing if it was a number, argument must be provided:Use:
bash script.sh Folder .txt
ATTENTION: Now here after running script and renaming, names of some files may coincide and there would be a conflict in this case. Can later fix it and update this script.