简体   繁体   English

重命名多个文件--linux / ubuntu

[英]Rename multiple files - linux/ubuntu

I need to remove the "_1331045422" from image files in my directory. 我需要从我的目录中的图像文件中删除“_1331045422”。

for eg., my image file name looks like: message-16-error_1331045422.png 例如,我的图像文件名如下: message-16-error_1331045422.png

I actually ran a script which rename all image files this way. 我实际上运行了一个脚本,以这种方式重命名所有图像文件。 Also I have other files (image files with correct names, js files and css etc. which have correct names) 我还有其他文件(具有正确名称的图像文件,具有正确名称的js文件和css等)

Please help me with a command to rename all image files with the "_1331045422" , without affecting others. 请帮我一个命令,用“_1331045422”重命名所有图像文件,而不影响其他人。

EDIT: I not only have .png files with the wrong filename. 编辑:我不仅有.png文件错误的文件名。 There are gifs and jpegs too. 还有GIF和jpegs。

You can use rename command: 您可以使用rename命令:

rename 's/_\\d+(\\..{1,3})/$1/g' *

You can change the range between {} if you have files with extension longer than three chars. 如果您的扩展名超过三个字符的文件,则可以更改{}之间的范围。

Be carefull that on some system the rename command is a bit different. 请注意,在某些系统上,重命名命令有点不同。 Have a look here: https://superuser.com/questions/70217/is-there-a-linux-command-like-mv-but-with-regex 看看这里: https//superuser.com/questions/70217/is-there-a-linux-command-like-mv-but-with-regex

Make a backup of your files before trying this!! 在尝试这个之前备份你的文件!!

#!/bin/bash
for i in *.png; 
    do mv $i `echo $i | sed  "s/_[0-9]\+\.png^/\.png/"`
done
#!/usr/bin/python
# message-16-error_1331045422.png --> message-16-error.png
# Usage: python foo.py dir_to_change
import os, sys
dir=sys.argv[1]
for file in os.listdir(dir):
    if not file.endswith('.png'):
        continue
    new, end = file.rsplit('_', 1)
    new=u'%s.png' % new
    file_old=os.path.join(dir, file)
    file_new=os.path.join(dir, new)
    os.rename(file_old, file_new)

With rnm : 随着rnm

rnm -rs '/_\d+(\.)(png|gif|jpg|jpeg)/\1\2/' *

More examples here . 这里有更多例子。

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

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