ryanprayogo
ryanprayogo

Reputation: 11817

Get file name before the extension

I have some files in the same directory (in UNIX filesystem) that looks like:

a.txt.name
b.xml.name
c.properties.name

a.txt.name2
b.xml.name2
c.properties.name2

How do I get the string before the name or name2 part using some shell command?

ie. the a.txt, b.xml, c.properties part?

Upvotes: 7

Views: 5292

Answers (4)

DaveG
DaveG

Reputation: 301

If naming convention is always foo.bar.other , then this is simple enough:

ls * | cut -d. -f1,2

Upvotes: 2

Steve Robillard
Steve Robillard

Reputation: 13471

Take a look at the bash string manipulation functions. This page should get you most of the way there:

http://tldp.org/LDP/abs/html/string-manipulation.html

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360345

$ file="a.txt.name"
$ file="${file%.*}"
$ echo "$file"
a.txt

Upvotes: 4

Thomas
Thomas

Reputation: 182000

$ basename a.txt.name .name
a.txt

Upvotes: 9

Related Questions