0

I have filenames like /dir/dir2/file.ext1.ext2 and I want to get filename without one extension:

/dir/dir2/file.xml.zip.sgn  => file.xml.zip
/dir/dir2/file.xml.zip      => file.xml
/dir/dir2/file.doc          => file
/dir/dir2/file              => file
file                        => file

would be great by expr , may be grep -oP

OK, I can start by basename to rip off dirs, but how to rip end-only optional extension ?

perror
  • 7,071
  • 16
  • 58
  • 85
xoid
  • 1,114
  • 1
  • 10
  • 24

1 Answers1

1
x=/etc/lolf/logl/shells.ext1.ext2
y=${x/\/*\//} # rips dir ('basename' do the same)
echo ${y%.*}  # rips last ext

if lacks dirs or exts, it works anyway!

Anybody, could you do all in one pass?

thanx to Andrew Henle, see more at How do I remove the file suffix and path portion from a path string in Bash?

Community
  • 1
  • 1
xoid
  • 1,114
  • 1
  • 10
  • 24