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    users      2089224 Oct 16 15:50 MPGGSN02_20131007234041_24282.20131007
-rw-r--r--   1 emmuser    users      2088032 Oct 16 15:50 MPGGSN02_20131007233017_24262.20131007
-rw-r--r--   1 emmuser    users      2096991 Oct 16 15:50 MPGGSN02_20131007235026_24301.20131007

I need to remove the last 9 characters of each file name, I have been trying to use

sed

, but so far, I am struglling. Can someone help?

I could use

mv

, but there thousands of files..

if your OS supports it, try running this command:

rename "s/.{9}$//" MPGGSN02*

Alternatively you can run this:

for f in MPGGSN02* ; do mv $f ${f%%\.*} ; done

Make a backup first!

1 Like

Hi

I am using HP-UX, shall I change your

rename

command to

mv

?

if the rename command is not supported, try the for loop I posted

Hi!

The

for

loop did work fine, but please if you dont minf explain to me every line of your script, as I am still learning shell script.

Read your shell's man page, esp. Parameter Expansion / Remove matching suffix pattern.

And it's not necessary to use the double %% as proposed above, single % will do. After reading, you'll know why.

EDIT: And, to be utterly exact, that will NOT remove 9 chars from the end of file names, as requested, but it will remove the dot and suffix regardless of its length. To remove 9 chars, use ${f:0:${#f}-9} (you may need to do the arithmetics outside the expression, depending on your shell version).

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

---------- Post updated 12-27-13 at 02:55 AM ---------- Previous update was 12-26-13 at 05:37 PM ----------

any replay ???

Please open a new thread for a new question!
And - applying the proposed solutions, you should be able to find one by yourself...