Remove the last 9 characters of a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the last 9 characters of a filename
# 1  
Old 11-08-2013
Remove the last 9 characters of a filename

Hi All!

Please can someone help, I have a dir with the following files:

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

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

I could use
Code:
mv

, but there thousands of files..
# 2  
Old 11-08-2013
if your OS supports it, try running this command:
Code:
rename "s/.{9}$//" MPGGSN02*

Alternatively you can run this:
Code:
for f in MPGGSN02* ; do mv $f ${f%%\.*} ; done

Make a backup first!
This User Gave Thanks to Subbeh For This Post:
# 3  
Old 11-08-2013
Remove the last 9 characters of a filename

Hi

I am using HP-UX, shall I change your
Code:
rename

command to
Code:
mv

?
# 4  
Old 11-08-2013
if the rename command is not supported, try the for loop I posted
# 5  
Old 11-08-2013
Hi!

The
Code:
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.
# 6  
Old 11-08-2013
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).

Last edited by RudiC; 11-08-2013 at 07:19 AM..
# 7  
Old 12-27-2013
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 ???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Linux

How can I keep certain characters from appearing in a filename?

hi i know this is irrelevant to the question above but i was wondering how to pt a restriction in the filename in linux. I want that it is impossible to add numbers into the filename, help will be rely great , thanx! This posting was initially given as a reply to an unrelated thread. It has been... (2 Replies)
Discussion started by: samirboss
2 Replies

3. Shell Programming and Scripting

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-.mp4 i want to rename this to VFX-Official-Trailer-(HD)-Shhh... (13 Replies)
Discussion started by: ateya
13 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

Filter last 2 characters of a filename

I have following script to 'archive' some logfiles: for APPHOME in `cat $HOME/archive/apps.cfg` do . $APPHOME/archive/parms.cfg LOGFILES=$(grep "^LOGFILE=" $APPHOME/archive/parms.cfg) for I in $LOGFILES do LOGPATH=$(echo $I |awk -F'=' '{... (2 Replies)
Discussion started by: oliware
2 Replies

6. Shell Programming and Scripting

removing a range of characters in a filename

hi, I have quite a bunch of files with annoyingly long filenames. I wanted to cut the range of characters from 9-18 and just retain the first 8 characters and the .extension. any suggestion how to do it. thanks much. original filename: 20000105_20000105_20100503.nc.asc output filename:... (4 Replies)
Discussion started by: ida1215
4 Replies

7. UNIX for Dummies Questions & Answers

Help with Removing extra characters in Filename

Hi, It's my first time here... anyways, I have a simple problem with these filenames. This is probably too easy for you guys: ABC_20101.2A.2010_01 ABD_20103.2E.2010_04 ABE_20107.2R.2010_08 Expected Output: ABC_20101 ABD_20103 ABE_20107 The only pattern available are the ff: 1) All... (9 Replies)
Discussion started by: Joule
9 Replies

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

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

10. UNIX for Dummies Questions & Answers

Strange Characters in Filename

Hi folks. None of the conventional methods are working for my dilemma: I have a file in my root directory that has a name comprised of strange characters. When I do an ls, it just hangs at that file until I do a Cntrl-C. rm ./filename & rm \filename do not work. I am entering the... (4 Replies)
Discussion started by: kristy
4 Replies
Login or Register to Ask a Question