Remove the last 15 characters of a filename with respect to leave file extension

how can i remove numbers of characters from the last name of file with respect to not remove the files extension

example

VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4

i want to rename this to

VFX-Official-Trailer-(HD)-Shhh -by-Freddy-Chavez-Olmos&Shervin-Shoghian.mp4 

i have many files with different names but all has _ and -[YT-f22][11 diff characters]

Thanks

There is a lot more going on here than removing the last 15 characters before the filename's .mp4 extension. You didn't say anything about changing all single occurrences of underscore ( _ ) to hyphen ( - ), changing the 1st occurrence of double underscore ( __ ) to a single hyphen, and changing the 2nd occurrence of double underscore to a single space character.

Do all of the files you want to rename end with .mp4 ?

Were all of the changes noted above intentional?

Are there any other name change peculiarities we need to know about?

Thanks for your replay
i want all spaces between words in file name to be -
like
thanks-for-replay

all my files now is .mp4
if you can also give me a solution if i have multiple files format that will be great

thanks

So, just to be clear, you want to rename all files with filenames that match the pattern:

*-[[]YT-f22][[]???????????].*

by:

  1. Saving the filename extension (matched by the .* at the end of the filename),
  2. deleting the filename extension from the end of the filename,
  3. deleting the last 22 (note 22; not 15) characters from the remainder of the filename,
  4. changing every sequence of one or more non-alphanumeric characters in the remainder of the filename to a single hyphen, and then
  5. add the filename extension back to the end of the filename.

This would rename your sample filename:

VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4

to the filename:

VFX-Official-Trailer-HD-Shhh-by-Freddy-Chavez-Olmos-Shervin-Shoghian.mp4

instead of the filename:

VFX-Official-Trailer-(HD)-Shhh -by-Freddy-Chavez-Olmos&Shervin-Shoghian.mp4

that you originally requested.

Is this correct?

yes
yes

thanks

The following shell and awk script can search for files matching your pattern in a given directory (by uncommenting the ls command on the 3rd line of the script), in a file hierarchy rooted in one or more given directories (by uncommenting the find command on the 5th line of the script), or by piping a list of pathnames to be processed into the script on standard input. This script will not work if any pathname to be processed contains a <newline> character or a <double-quote> character; other than that it should handle just about regular file you throw at it. This turned out to be more complex than I expected when I first started. (Mostly getting the args right when printing commands to print informational messages.) It uses ls, find, or some other source you provide to get a list of pathnames to be processed, awk to massage the last filename in each pathname and create mv statements to be executed by a shell, and a shell to actually rename the files. (Currently it just prints the mv commands to be executed. When you have convinced yourself that it does what you want it to do, remove the echo marked in red to actually move the files.

#!/bin/ksh
# Uncomment the following line to process files in the current directory:
# ls *-[[]YT-f22][[]???????????].* |
# Uncomment the following line to process files in and under the current directory:
# find . name '*-[[]YT-f22][[]???????????].*' |
awk '
{       # Save the directory name and remove it from the input line:
        if(match($0, /.*\//)) {
                dir = substr($0, 1, RLENGTH)
                $0 = substr($0, RLENGTH + 1)
        } else  dir = ""
        if(!match($0, /-[[]YT[-]f22][[]...........]/)) {
                printf("printf \"unexpected filename: \\\"%%s\\\" skipped\\n\" \"%s\"\n",
                        dir $0)
                next
        }
        # Save the filename extension and the base filename
        base = substr($0, 1, RSTART - 1)
        ext = substr($0, RSTART + RLENGTH)
        # Split base on strings of non-alphanumeric characters:
        n = split(base, ans, /[^[:alnum:]]*/)
        # Construct the new pathname:
        new = dir ans[1]
        for(i = 2; i <= n; i++) 
                new = new "-" ans
        new = new ext
        printf("echo mv \"%s\" \"%s\"\n", dir $0, new)
}' | ksh

I tested it with bash and ksh. It should also work with an old Bourne shell or any other shell that recognizes basic Bourne shell syntax.

If you want to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default /usr/bin/awk .

Really Thanks many times
the script working very good

thanks

we can do this

echo $(echo "VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4" | rev | awk -F. '{print $NF}' | cut -c 23- | rev).mp4

How about this ?

 echo "VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4" | tr -c -s "[[:alnum:]]" "-"  | awk '{a=substr($0,length($0)-3,3);print substr($0,1,length($0)-27)"."a}'
# echo $mp4
VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4

# echo "$mp4"| ruby -e 's=gets; puts s.gsub(/[&-]+/,"_").gsub(/_+/,"-").split("-")[0..-4].join("-") + ".mp4" '
VFX-Official-Trailer-(HD)-Shhh-by-Freddy-Chavez-Olmos-Shervin-Shoghian.mp4

how can i remove none English letters from renamed file name

Is this related to the initial problem introduced in this thread? (If not, please start a new thread with a title related to what you are trying to do and provide a better description along with examples of the file name transformations you want to perform.)

What do you mean by "remove none English letters"? Are you saying that you want to remove everything from a file's name except the 52 uppercase and lowercase letters available in the C Locale? Do you mean that you want to change a filename like VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4 into VFXOfficialTrailerHDShhhbyFreddyChavezOlmosShervinShoghianYTfHtaZLfqmp

sometimes the video name contains no English letters (non English language)
like
korian
Japaneses
arabic

So, if you have a filename like <arabic string>-[YT-f23][Ht3aZLf9q_7].mp4 , you want the entire filename to be turned into .mp4 ? Assuming this isn't what you want, please give us clear directions of the changes you want made to the filenames you're processing (similar to what I provided in message #4 in this thread with your new requirements added) and show us examples of what you do want to happen.