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;
doneBut instead of using the $random, I would like to take the first 2 letters of each word.
Example
before renaming:
having the best day.jpegafterr renaming:
hathbeda.jpegOk, a bit trickier :) My filenames mostly have . to seperate the words. Is it still possible to do it than?
12 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/' *.mp4This 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/' *.mp4Command breakdown:
rename: renames multiple files usingPCREs*.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 replacementg: 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.mp4string/: stops the replacement
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]firstis the list containing the parts of filename splitted on the.(assuming you have only one.in the filename)secondis the list having the filename upto the.splitted on spaces.Next we go though the contents of the
secondto take out the first two characters out of it and then adding them together to get the first part of our new filenameThen we have used
os.renameto rename the file accordingly by adding.jpegat the end ofname.
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.jpegAfter:
$ ls -1
fo.jpeg
hathbeda.jpeg
hekicasp.jpeg
thisado.jpeg More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"