[英]Is there a grep or shell script that uses a regex to change filenames?
如何将xxx-xxx_[a-zA-Z]+_\\d+_(\\d+)\\.jpg
递归更改为$1.jpg
?
#!/bin/bash
find . | while read OLD; do
NEW="`sed -E 's/(.*\/)?xxx-xxx_[a-zA-Z]+_[0-9]+_([0-9]+)\.jpg/\1\2.jpg/' <<< "$OLD"`"
[ "$OLD" != "$NEW" ] && mv "$OLD" "$NEW"
done
Some notes on this: 一些注意事项:
Piping the output of find
to a while read
loop is a neat way of reading the output of find
one line at a time. 将
find
的输出发送到while read
循环是一种一次读取find
的输出的巧妙方法。 It's nice because it'll process the files as find
finds them without having to build up the whole list of files first, as would happen if you did `find`
in backticks. 很好,因为它会像
find
那样处理文件,而不`find`
建立整个文件列表,就像您在反引号中`find`
那样。
If you have tons of unrelated files then you can add the -regex
option to find
as per soulmerge's answer, but if not eh, no need. 如果您有大量不相关的文件,则可以添加
-regex
选项,以根据soulmerge的答案进行find
,但是如果没有,则不需要。
I modified your regex to allow for directory names at the front. 我修改了您的正则表达式,以允许目录名称位于最前面。 They'll get captured in
\\1
and the number you're looking for will be \\2
. 它们将被捕获到
\\1
,而您要查找的数字将是\\2
。
sed <<< "$OLD"
is the same thing as echo "$OLD" | sed
sed <<< "$OLD"
与echo "$OLD" | sed
echo "$OLD" | sed
, just a little fancier... echo "$OLD" | sed
,只是一点点幻想...
[ "$OLD" != "$NEW" ] && mv
is the same thing as if [ "$OLD" != "$NEW" ]; then mv; fi
[ "$OLD" != "$NEW" ] && mv
与if [ "$OLD" != "$NEW" ]; then mv; fi
if [ "$OLD" != "$NEW" ]; then mv; fi
if [ "$OLD" != "$NEW" ]; then mv; fi
, just a little fancier... if [ "$OLD" != "$NEW" ]; then mv; fi
,只是一点点幻想...
Edit : Changed \\d
to [0-9]
for compatibility. 编辑 :为兼容性将
\\d
更改为[0-9]
。 I tested it on my Mac and it works. 我在Mac上对其进行了测试,并且可以正常工作。 Here's what happened in a test directory I set up:
这是我设置的测试目录中发生的事情:
mv ./1/xxx-xxx_ERR_19_02.jpg ./1/02.jpg
mv ./2/xxx-xxx_BLAH_266_14.jpg ./2/14.jpg
mv ./xxx-xxx_ERR_19_01.jpg ./01.jpg
I am giving you the more verbose version that can handle any file name (even those that are in folders with white-space contaminated names): 我给您提供了一个更详细的版本,可以处理任何文件名(甚至是那些带有空格污染名称的文件夹中的文件名):
# file rename.sh
old="$1"
new="$(dirname "$1")/$(echo "$(basename "$1")"|sed 's/^xxx-xxx_[a-zA-Z]+_[0-9]+_//')"
mv "$old" "$new"
# execute this in the shell:
find . -regex "xxx-xxx_[a-zA-Z]+_[0-9]+_[0-9]+\.jpg$" -exec ./rename.sh "{}" ";"
如果您具有rename
(或prename
)命令(这是Perl随附的Perl脚本):
find -type d -name dir -exec rename 's/xxx-xxx_[a-zA-Z]+_\d+_(\d+)\.jpg/$1.jpg/' {}/xxx-xxx_[a-zA-Z]*[0-9].jpg \;
See the various answers to this question on SuperUser.com . 在SuperUser.com上查看有关此问题的各种答案。 It is dealing with the same issue (renaming files using a regex).
它正在处理相同的问题(使用正则表达式重命名文件)。 [ And it took me ages to find it on StackOverflow - because it wasn't on SO but on SU!
[ 并且花了我很长时间才能在StackOverflow上找到它-因为它不在SO上,而是在SU上! :( ]
:(]
find /path -type f -name "???-???_[a-zA-Z]*_[0-9]*_*.???" | while read FILE
do
f=${FILE%%.???}
number=${f##*_}
extension=${FILE##*.}
path=${FILE%/*}
echo "mv '$FILE' '$path/$number.$extension"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.