-1

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.

something.pcap00001
Community
  • 1
  • 1
Cripto
  • 3,581
  • 7
  • 41
  • 65

3 Answers3

3

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

for file in $(ls -1 something.pcap*); do
    [[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
    newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
    mv ${file} ${newfile}
done
cforbish
  • 8,567
  • 3
  • 28
  • 32
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.

tink
  • 14,342
  • 4
  • 46
  • 50
  • what about centos. It attempted it and it did not work. And by did not work, I mean the file names remained the same. – Cripto Dec 02 '13 at 22:01
  • Good question. In deb rename is a symlink to /usr/bin/prename, which seems to be part of deb's perl installation. Maybe check whether Centos' perl has a script executable with rename in the name? If you can't - the script is tiny; here's a URL to the paste http://pastebin.com/7QkWtPSd – tink Dec 02 '13 at 23:17
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

Jekyll
  • 1,434
  • 11
  • 13