Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing extension from file without knowing it

Tags:

bash

I know how to remove the extension of a file, when I know it as:

nameis=$(basename $dataset .csv)

but I want to remove any extension without knowing it beforehand, anyone know how to do this?

Any help appreciated, Ted

like image 717
Flethuseo Avatar asked Mar 19 '11 21:03

Flethuseo


People also ask

How do you remove an extension from an Excel file?

Click the File tab, click Options, and then click the Add-Ins category. In the Manage box, click COM Add-ins, and then click Go. The COM Add-Ins dialog box appears. In the Add-Ins available box, clear the check box next to the add-in that you want to remove, and then click OK.

How do I remove a zip file extension?

Open File Explorer and click View tab, Options. In Folder Options dialog, move to View tab, untick Hide extensions for known file types option, OK. Then you will se file's extension after its name, remove it.

How do you remove a filename extension in Unix?

You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '. ')


1 Answers

In bash you can do the following:

nameis=${dataset%.*}

... e.g.:

$ dataset=foo.txt
$ nameis=${dataset%.*}
$ echo $nameis
foo

This syntax is described in the bash man page as:

${parameter%word}

${parameter%%word}

Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the "%" case) or the longest matching pattern (the "%%" case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

like image 148
Mark Longair Avatar answered Nov 06 '22 01:11

Mark Longair