Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

removing the filename extension

Shell Programming and Scripting


Tags
file extensions

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
daflore's Unix or Linux Image
daflore daflore is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 13 April 2011, 12:55 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
removing the filename extension

Is there an easy way to strip off a filename's extension?

For example, here's a filename:

blahblahblah.thisisok.thisisnotok

I want to get rid of .thisisnotok from the filename, so that what's left is

blahblahblah.thisisok

Thanks. I have a directory full of filenames that need to be manipulated in this way.
Sponsored Links
    #2  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
methyl's Unix or Linux Image
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
What Operating System and version do you have?
What Shell do you use?

Are the filename extensions random?
Do all files have an extension which is to be removed?
Sponsored Links
    #3  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
daflore's Unix or Linux Image
daflore daflore is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 13 April 2011, 12:55 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
ubuntu 10.10 maverick meerkat

bash shell

all of the files have the same extension that needs to be removed

---------- Post updated at 11:04 AM ---------- Previous update was at 11:02 AM ----------

sorry, and the filenames have spaces in them
    #4  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 9 October 2016, 12:41 PM EDT
Location: East Coast
Posts: 621
Thanks: 1
Thanked 177 Times in 163 Posts
Hope this helps point you in the right direction:

Code:
for x in *;do mv $x $(echo ${x%*.*});done

If you want to test it first, remove the 'mv' command:

Code:
for x in *;do echo ${x%*.*};done

Should list the files with the extension removed. If not, then previous command my require some tweaking. Google "bash shell scripting parameter substitution" for additional help. Good luck.

Last edited by Franklin52; 03-03-2011 at 05:49 PM.. Reason: Please use code tags, thank you
Sponsored Links
    #5  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
kshji's Unix or Linux Image
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 23 November 2017, 1:08 PM EST
Location: Finland
Posts: 650
Thanks: 0
Thanked 60 Times in 59 Posts
Posix compatible shells ksh93, bash, ..


Code:
filename="some part.some some.filetype"
notlast=${filename%.*}
last=${filename##*.}
first=${filename%%.*}
notfirst=${filename#*.}

Sponsored Links
    #6  
Old Unix and Linux 03-03-2011   -   Original Discussion by daflore
alister's Unix or Linux Image
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 977 Times in 791 Posts
Quote:
Originally Posted by in2nix4life View Post
for x in *;do mv $x $(echo ${x%*.*});done
There's no need for the echo command substitution and the first asterisk in your parameter substitution is meaningless since it's a shortest match (% instead of %%). Also, the filenames in question have spaces (perhaps that info was added after you had read the post), so the expansions need to be double quoted to protect them from field splitting.

A slightly simpler version of your approach:

Code:
for x in *; do
    mv "$x" "${x%.*}"
done

---------- Post updated at 04:43 PM ---------- Previous update was at 04:40 PM ----------

Quote:
Originally Posted by daflore View Post
Is there an easy way to strip off a filename's extension?
...<snip>...
Thanks. I have a directory full of filenames that need to be manipulated in this way.
If it's possible that striping the file extension from file1 can match an existing file, file2, and if you don't want file2 clobbered, you'll want to test for the existence of file2 before doing the mv.

Regards,
Alister
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting filename with entire path without extension amicableperson Shell Programming and Scripting 3 01-16-2011 09:13 PM
cut filename extension Essbaumer Shell Programming and Scripting 4 07-29-2010 06:18 AM
script takes the whole filename instead of just extension wolverine999 UNIX for Dummies Questions & Answers 1 05-20-2009 03:49 PM
changing filename extension Hiso Shell Programming and Scripting 3 01-24-2009 09:02 AM
separating filename and extension lucaspewkas Shell Programming and Scripting 2 04-06-2007 08:07 AM



All times are GMT -4. The time now is 06:42 PM.