在bash中重命名大量文件

时间:2022-02-07 10:37:02

I have a large list of files that I want to rename.

我有一大堆要重命名的文件。

Much like this

很像这样

So this is what my files look like

所以这就是我的文件的样子

something.pcap1
something.pcap10
something.pcap11
something.pcap12
...
something.pcap111
something.pcap1111

essentially I want to rename all of the files so that the numbers get padded with 0's and they are 5 digit numbers.

本质上我想重命名所有文件,以便数字填充为0,它们是5位数字。

something.pcap00001

3 个解决方案

#1


3  

A simple for loop should do the trick (can be script file):

一个简单的for循环应该可以做到(可以是脚本文件):

for file in $(ls -1 something.pcap*); do
    [[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
    newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
    mv ${file} ${newfile}
done

#2


1  

Something like this?

像这样的东西?

rename 's/\d+$/sprintf("%05d",$&)/e' soemthing.pcap*

Note: this works with the rename as found in debian and its derivates.

注意:这与debian及其派生中的重命名一起使用。

#3


0  

What about something like this?

这样的事情怎么样?

#!/bin/bash
for i in $(ls something.pcap*); do
 q=$(echo $i|sed -e 's/pcap/pcap00000/;s/pcap0*\([0-9]\{6,\}\)$/pcap\1/')
 mv $i $q
done

I hope this will help

我希望这个能帮上忙

#1


3  

A simple for loop should do the trick (can be script file):

一个简单的for循环应该可以做到(可以是脚本文件):

for file in $(ls -1 something.pcap*); do
    [[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
    newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
    mv ${file} ${newfile}
done

#2


1  

Something like this?

像这样的东西?

rename 's/\d+$/sprintf("%05d",$&)/e' soemthing.pcap*

Note: this works with the rename as found in debian and its derivates.

注意:这与debian及其派生中的重命名一起使用。

#3


0  

What about something like this?

这样的事情怎么样?

#!/bin/bash
for i in $(ls something.pcap*); do
 q=$(echo $i|sed -e 's/pcap/pcap00000/;s/pcap0*\([0-9]\{6,\}\)$/pcap\1/')
 mv $i $q
done

I hope this will help

我希望这个能帮上忙