strip suffix from full path filename

Posted on 1998-06-16
Last Modified: 2012-06-27
as far as I know command  basename can strip directory and suffix from filename.
But I want to strip only suffix from filename but keep leading directory components. Is it possible? How?
Question by:tian

Accepted Solution

blowfish earned 100 total points
ID: 2008083
basename strips off the full path leaving only the filename.

dirname strips off the filename leaving only the path

Expert Comment

ID: 2008084
If you are using ksh, you can use the following feature:
You can strip any suffix sequence from a variable called myname with the syntax like ${myname#abc} where abc is the suffix you want to get rid of.  You can use wildcards here, like '.*' to strip any trailing sequence after (and including) the dot.
  echo ${myname#.*}
will print out the string "/a/b/c/file".
The feature is more general, you can use ## to match and strip the largest match, or use % or %% to strip prefix sequences.

Author Comment

ID: 2008085
dirname is just what I'm looking for.
I have tried  
         echo ${myname#.*}
but it does not work.
LVL 51

Expert Comment

ID: 2008086
assuming you're using any kind of shell with myname=/aaa.b/b/c/file.d.ext :

csh#   echo $myname:r
sh#    csh -f -c "set m=$myname; echo \$m:r"
sh#    echo $myname|sed  -e  's/\(.*\)\..*$/\1/'
sh#    echo $myname|gawk -F. '{for (i=1;i<NF-1;i++) {printf("%s.",$i);} print $i}'
ksh#   echo ${myname%.*}


Expert Comment

ID: 2008087
Sorry, I seem to have reversed the % and # signification  (Even worse, I seem to be doing this all the time).  Still, remember the syntax exists because it can be very useful in shell programming.
FYI: bash (popular on Linux systems) also supports this syntax.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 90
Unix How to Bulk Remove Multiple Directories from FileSystem 10 77
Unix Question 19 50
How to clone solaris 10 machine 33 131
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question