Module 8 basename

Previous Table of Contents Next


Module 8
basename

DESCRIPTION

The external basename command parses the given path and returns only the filename. Any prefix ending in / (slash) is deleted from the path . If a second argument is present, it is used as a suffix to be deleted from the filename. For instance, the command

 cj> basename /u1/ts/mylogin/junk.c \.c      junk 

returns junk. The entire path (/u1/tech/mylogin/) and the .c suffix are removed.

The longest possible prefix is removed while the shortest possible suffix is deleted.

COMMAND FORMAT

Following is the format of the basename command.

 basename path [ suffix ] 

Arguments

Only two arguments may be passed to the basename command.

path The path that is parsed with only the filename being returned.
suffix A suffix string that is removed from the end of the filename.

RELATED COMMANDS

Refer to the dirname command described in Module 35.

RELATED FILES

The basename command writes to the standard output.

APPLICATIONS

The basename command is normally used as a substitution command within a shell script. It can be used to reduce a full path parameter or variable down to a single filename. It can also remove suffixes so a single parameter variable can be used to perform a task that uses the same filename with different suffixes.

TYPICAL OPERATION

In this activity you use the basename command to copy a file to a different directory and name . Begin at the shell prompt.

1.    Type cd /tmp and press Return . This puts you in the /tmp directory just to force the use of paths in the following command.
2.    Type cp $HOME/file1 ˜basename $HOME/file1 1 ˜.bak and press Return .
 cj> cp $HOME/file1 


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net