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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the last 15 characters of a filename with respect to leave file extension
# 1  
Old 12-27-2013
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

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

i want to rename this to

Code:
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

Last edited by Don Cragun; 12-27-2013 at 08:27 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-27-2013
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?
# 3  
Old 12-27-2013
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
# 4  
Old 12-28-2013
So, just to be clear, you want to rename all files with filenames that match the pattern:
Code:
*-[[]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:
Code:
VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4

to the filename:
Code:
VFX-Official-Trailer-HD-Shhh-by-Freddy-Chavez-Olmos-Shervin-Shoghian.mp4

instead of the filename:
Code:
VFX-Official-Trailer-(HD)-Shhh -by-Freddy-Chavez-Olmos&Shervin-Shoghian.mp4

that you originally requested.

Is this correct?
# 5  
Old 12-28-2013
Quote:
Originally Posted by Don Cragun
So, just to be clear, you want to rename all files with filenames that match the pattern:
Code:
*-[[]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:
Code:
VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-[YT-f22][Ht2aZLf8q_8].mp4

to the filename:
Code:
VFX-Official-Trailer-HD-Shhh-by-Freddy-Chavez-Olmos-Shervin-Shoghian.mp4

instead of the filename:
Code:
VFX-Official-Trailer-(HD)-Shhh -by-Freddy-Chavez-Olmos&Shervin-Shoghian.mp4

that you originally requested.

Is this correct?
yes
yes

thanks
# 6  
Old 12-28-2013
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.
Code:
#!/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[i]
        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.
# 7  
Old 12-28-2013
Really Thanks many times
the script working very good

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove filename from output file?

Hello, I am trying to print searched multiple keywords in multiple files. It is almost okay with the code but the code puts filename in front of each line. How may I get rid of it? -grep -A1 'word1' *.txt | grep -A1 'word2' | grep -A1 'word3' I expect: Real outcome: How may I... (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

Remove the last 9 characters of a filename

Hi All! Please can someone help, I have a dir with the following files: ~-rw-r--r-- 1 emmuser users 2087361 Oct 16 15:50 MPGGSN02_20131007234519_24291.20131007 -rw-r--r-- 1 emmuser users 2086837 Oct 16 15:50 MPGGSN02_20131007233529_24272.20131007 -rw-r--r-- 1 emmuser ... (7 Replies)
Discussion started by: fretagi
7 Replies

3. Shell Programming and Scripting

Remove the file except with particular extension

Hi all i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone... (5 Replies)
Discussion started by: vikatakavi
5 Replies

4. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

5. Shell Programming and Scripting

[Solved] remove file extension

Hi, I have some files with some extension e.g. abc.xml.REMOVE,xyz.xml,efg.xml.REMOVE . I have to remove the .REMOVE extension. I can display it using the below script but cannot rename it. ls -l|sed 's/\.REMOVE//' How can I rename this? Thanks in advance (7 Replies)
Discussion started by: babom
7 Replies

6. Shell Programming and Scripting

Remove filename is text file

Hello, I got files full path in a text file like that /main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb /main/k/kdelibs-experimental/libknotificationitem-dev_4.3.2-0ubuntu1_i386.deb /main/k/kdemultimedia/dragonplayer_4.3.2-0ubuntu1_i386.deb... (13 Replies)
Discussion started by: davidkhan
13 Replies

7. Shell Programming and Scripting

sed script to remove nth characters from end of filename

Hi all, I have this basic script to remove, in this case 9 characters from the end of a file name. This is what I have so far, for file in *.mov do newname=`echo $file | sed 's/\(.*\)........./\1/' ` mv "$file" "$newname" done The problem is that it removes the file extension as well.... (2 Replies)
Discussion started by: Monkey Dean
2 Replies

8. Shell Programming and Scripting

remove special characters from filename recursively

hi: i have several thousand files from users and of course they use all kind of characters on filenames. I have things like: My special report (1999 ) Lisa & Jack's work.doc crazy. How do I remove all this characters in the current dir and subdirs too? Thanks. (3 Replies)
Discussion started by: jason7
3 Replies

9. Shell Programming and Scripting

remove file extension

Hi ALL, I'm new to this forum. Thanks and congrats to all for their great efforts building this site simply superb for all unix administrators. My requirement is to remove extensions of the files in the current directory. I'm doing it using below script which is working but i think it is... (12 Replies)
Discussion started by: prvnrk
12 Replies

10. Shell Programming and Scripting

Need to only remove parenthesis and : leave the rest

Hi all, I'm stuck on this last part...am running a simple script under AIX to extract NetView host IP addresses. The line below returns the IP address in parenthesis with a trailing colon, i.e. ping -c 1 $name |grep \( | awk '{ print $3 }' --------> returns (a.b.c.d): How can I only... (10 Replies)
Discussion started by: livinthedream
10 Replies
Login or Register to Ask a Question