Home Man
Search
Today's Posts
Register

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

Tags
file extensions, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-03-2011
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.
# 2  
Old 03-03-2011
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?
# 3  
Old 03-03-2011
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 03-03-2011
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 04:49 PM.. Reason: Please use code tags, thank you
# 5  
Old 03-03-2011
Posix compatible shells ksh93, bash, ..

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

# 6  
Old 03-03-2011
Quote:
Originally Posted by in2nix4life
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
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
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[BASH] Getting a filename its extension sea Shell Programming and Scripting 7 05-07-2015 04:58 AM
Awk-sed help: removing extension name. rveri Shell Programming and Scripting 11 06-06-2013 06:01 PM
Get latest filename without extension sumang24 Shell Programming and Scripting 7 12-11-2012 02:38 PM
BASH - Removing the very last character(s) extension of a filename jcdole UNIX for Dummies Questions & Answers 2 07-03-2012 01:43 PM
Script to add extension to filename repiv Shell Programming and Scripting 2 07-22-2011 05:30 PM
Getting filename with entire path without extension amicableperson Shell Programming and Scripting 3 01-16-2011 08:13 PM
cut filename extension Essbaumer Shell Programming and Scripting 4 07-29-2010 05:18 AM
script takes the whole filename instead of just extension wolverine999 UNIX for Dummies Questions & Answers 1 05-20-2009 02:49 PM
changing filename extension Hiso Shell Programming and Scripting 3 01-24-2009 08:02 AM
separating filename and extension lucaspewkas Shell Programming and Scripting 2 04-06-2007 07:07 AM


All times are GMT -4. The time now is 05:24 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password