Rename file which uses the first 2 letters of every word

I'm currently using a simple script for renaming:

for f in *.mp4; do echo mv $f $RANDOM$RANDOM$RANDOM.mp4; mv $f $RANDOM$RANDOM$RANDOM.mp4; done 

But instead of using the $random, I would like to take the first 2 letters of each word.

Example

before renaming:

having the best day.jpeg 

afterr renaming:

hathbeda.jpeg 

Ok, a bit trickier :) My filenames mostly have . to seperate the words. Is it still possible to do it than?

1

2 Answers

You can do this using a single rename command (i.e. you don't need a bash script for this, or you can use this command within your bash script in place of the for loop):

rename -n 's/(^| )([^ ]{1,2})[^ ]*/$2/g; s/$/.mp4/' *.mp4 

This will just mimic the behavior of echo mv [...], so that you can see the result without actually renaming anything.

If the result is the expected one, then run:

rename 's/(^| )([^ ]{1,2})[^ ]*/$2/g; s/$/.mp4/' *.mp4 

Command breakdown:

  • rename: renames multiple files using PCREs
  • *.mp4: renames only files with extension .mp4

Regex #1 breakdown:

  • s: performs a substitution
  • /: starts the regex
  • (: starts grouping the allowed strings
  • ^: matches the start of the line
  • |: separates the second allowed string
  • : matches a character
  • ): stops grouping the allowed strings
  • (: starts the second capturing group
  • [^ ]{1,2}: matches from 1 to 2 occurrences of any character not
  • ): stops the second capturing group
  • [^ ]*: matches any number of characters not
  • /: stops the regex / starts the replacement
  • $2: replaces with the second capturing group
  • /: stops the replacement
  • g: replaces all the pattern occurences in the line

Regex #2 breakdown:

  • s: performs a substitution
  • /: starts the regex
  • $: matches the end of the line
  • /: stops the regex / starts the replacement
  • .mp4: adds a .mp4 string
  • /: stops the replacement
2

Using python:

#!/usr/bin/env python2 import glob, os for filename in glob.glob('*.jpeg'): first = filename.split('.') second = first[0].split(' ') name = '' for i in range(0, len(second)): name += second[i][:2] os.rename(filename, name + '.' + first[1] 
  • first is the list containing the parts of filename splitted on the . (assuming you have only one . in the filename)

  • second is the list having the filename upto the . splitted on spaces.

  • Next we go though the contents of the second to take out the first two characters out of it and then adding them together to get the first part of our new filename

  • Then we have used os.rename to rename the file accordingly by adding .jpeg at the end of name.

Also note that, the destination file must not exist beforehand, if it exists it will be overwritten.

Test:

Before:

$ ls -1 foo.jpeg having the best day.jpeg hello kitty cat spam.jpeg this is a doggy.jpeg 

After:

$ ls -1 fo.jpeg hathbeda.jpeg hekicasp.jpeg thisado.jpeg 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like