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?
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 .
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
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.