You are viewing lmohanphy

Tired of ads? Upgrade to paid account and never see ads again!

Previous Entry | Next Entry

Name 
basename - Strip directory and suffix from a file name

Synopsis
basename NAME [SUFFIX]

What is basename?
When you provide a string that contains a filename with full path, basename will remove only the directory portion of it, and return only the filename portion of that string. It can also remove the file extension, and return only the filename without an extension (i.e., basename removes any leading directory components from NAME. If SUFFIX is specified and is identical to the end of NAME, it is removed from NAME as well).

I). Examples

i). Basename with no file extension
Basename removes the path and displays only the filename. 

$ basename /etc/shadow
shadow

$ basename /etc/passwd
passwd

ii). Display with file extension
When the filename contains an extension, basename by default will remove the path and return the filename with the extension.

$ basename /etc/apache2/httpd.conf 
httpd.conf

$ basename  /usr/include/stdio.h 
stdio.h

iii). Display only the filename without extension or remove directory and suffix from filenames
If you want to get only the filename without the extension, you should pass the extension as 2nd argument to the basename command as shown below.

$ basename /usr/include/stdio.h .h
stdio

$ basename /etc/httpd/conf/httpd.conf .conf
httpd

$ basename /etc/httpd/conf/httpd.conf d.conf
http

$ basename /usr/bin/perlscript script
perl

iv). You can remove multiple extensions:

$ basename /etc/syslog.conf.old .conf.old
syslog

v). You can remove everything up to the first letter of a file's name

$ basename /etc/syslog.conf.old  yslog.conf.old
s

vi). strip off everything up to and including the final slash (/).

$ basename 1/2
2

$ basename "I want to buy 1/4 cat"
4 cat

vii). basename command is mostly used in shell scripts to get the name of the shell script file you are running. Sample shell script code is shown below:

$ cat shellscript.sh
#!/bin/bash
cmd=$(which basename)
filename1=`$cmd $0`
#prints filename with extention
echo $filename1
#remove extention
filename2=`$cmd $0 .sh`
echo $filename2

$ chmod x shellscript.sh

$ ./shellscript.sh
shellscript.sh
shellscript

(OR)
$ /home/cassandra/learning/shellscript.sh
shellscript.sh
shellscript

II). Implementation of simple basename command using C



/*
 * =====================================================================================
 *
 *       Filename:  mybasename.c
 *
 *    Description: basename - strip directory and suffix from filenames 
 *
 *        Version:  1.0
 *        Created:  Sunday 07 October 2012 10:54:34  IST
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Mohan L (mohanphy), L.mohanphy@gmail.com
 *   Organization:  http://lmohanphy.livejournal.com/ 
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*-----------------------------------------------------------------------------
 * This function returns a pointer to the last occurrence of the character 
 * '/' in the string filename.  
 *-----------------------------------------------------------------------------*/
char * return_basename (char *filename)
{
  char *p = strrchr (filename, '/');
  return p ? p + 1 : (char *) filename;
}

/*-----------------------------------------------------------------------------
 * This function remove a trailing SUFFIX from name.
 *-----------------------------------------------------------------------------*/
void remove_suffix(char *name,char *suffix) 
{
    char *pn;
    char *ps;
    pn = name + strlen(name);
    ps = suffix + strlen(suffix);
    while ( pn > name && ps > suffix )
        if ( *--pn != *--ps )
            return ;
    if ( pn > name )
        *pn = '\0';
}

/*-----------------------------------------------------------------------------
 *  main function of mybasename starts here
 *-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
    char *bp;
    switch (argc) 
    {
        default:
            printf("Improper Number of arguments\n");
            exit(1);
        case 2:
            bp = return_basename(argv[1]);
            printf("%s\n",bp);    
            break;
        case 3:
            bp = return_basename(argv[1]);
            remove_suffix(bp,argv[2]);
            printf("%s\n",bp);
            break;
    }
    return (0);
}



Output of all the above examples using mybasename

$ ./mybasename /etc/shadow
shadow
$ ./mybasename /etc/passwd
passwd
$ ./mybasename /etc/apache2/httpd.conf 
httpd.conf
$ ./mybasename  /usr/include/stdio.h 
stdio.h
$ ./mybasename  /usr/include/stdio.h .h
stdio
$ ./mybasename  /etc/httpd/conf/httpd.conf .conf
httpd
$ ./mybasename  /etc/httpd/conf/httpd.conf d.conf
http
$ ./mybasename  /usr/bin/perlscript script
perl
$ ./mybasename  /etc/syslog.conf.old .conf.old
syslog
$ ./mybasename  /etc/syslog.conf.old  yslog.conf.old
s
$ ./mybasename  1/2
2
$ ./mybasename  "I want to buy 1/4 cat"
4 cat

Profile

lmohanphy
lmohanphy

Latest Month

December 2014
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
28293031   
Powered by LiveJournal.com
Designed by Lilia Ahner