3

I am receiving video files with different names but the final 8 characters of all are numbers (also different every time). This is the pattern of the file name:

custom text 00030402.avi

I have to make file name looks like this:

00\:03\:04\:02

So my question is how to rename the last 8 characters and preserve them to achieve this pattern.

4

Using rename (the one from perl sometimes called prename, not from util-linux):

rename 's/(\d{2})(\d{2})(\d{2})(\d{2})/$1:$2:$3:$4/'  *.avi

This would rename a filename like custom text 00030402.avi to custom text 00:03:04:02.avi.

If you backslashes in the filename (before the :), say:

rename 's/(\d{2})(\d{2})(\d{2})(\d{2})/$1\\:$2\\:$3\\:$4/' *.avi

In order to remove the custom text from the filenames, say:

rename 's/.* (\d{2})(\d{2})(\d{2})(\d{2})(.*)/$1\\:$2\\:$3\\:$4$5/' *.avi
  • Maybe something is wrong because I get the following error in terminal: Can't rename zlatin 00364220.avi zlatin 00:36:42:20.avi: Invalid argument – ispasov Sep 28 '13 at 5:49
  • @ispasov What does rename -n 's/(\d{2})(\d{2})(\d{2})(\d{2})/$1:$2:$3:$4/' *.avi say? – devnull Sep 28 '13 at 5:52
  • @ispasov And do you want backslashes (``) in the resulting filename? – devnull Sep 28 '13 at 5:53
  • zlatin 00364220.avi renamed as zlatin 00:36:42:20.avi – ispasov Sep 28 '13 at 5:56
  • 1
    @ispasov You can say * as well. – devnull Sep 28 '13 at 13:14
2

With zsh:

$ autoload zmv
$ zmv -v '(*)(??)(??)(??).avi' '$1\:$2\:$3\:$4.avi'
mv -- custom\ text\ 00030402.avi custom\ text\ 00\\:03\\:04\\:02.avi

(-v for verbose). To remove the custom text:

$ zmv -v '*(??)(??)(??)(??).avi' '$1\:$2\:$3\:$4.avi'
mv -- custom\ text\ 00030402.avi 00\\:03\\:04\\:02.avi
1

Here's a way to do it with Perl regex lookahead:

perl -e '
    while($name=shift){
        @components = split /(?=(?:[0-9]{2})+[^0-9]+$)/,$name;
        shift @components;
        $new_name = join '\:',@components;
        rename $name => $new_name;
    }
' list_of_avi_files

This will work for any number of digit pairs, not just 4. You can also change {2} to {3}, for example, and have it split on digit triples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.