Tuesday, August 14, 2007

Unix - `basename` & `dirname`

Unix command.

`basename` - strip directory and suffix from filenames

Print NAME with any leading directory components removed. If specified, also remove a trailing SUFFIX.
The basename utility deletes any prefix ending in / and the suffix (if present in string) from string, and prints the result on the standard output. It is normally used inside substitution marks (``) within shell procedures.

`dirname` - deliver portions of path names strip non-directory suffix from file name.
Print NAME with its trailing /component removed; if NAME contains no /’s, output ‘.’ (meaning the current directory).



EXAMPLES:

Example 1: Examples of the basename command.
...
bash-2.03$ NAME=`basename $HOME/personal/mail`;
bash-2.03$ echo $NAME;
bash-2.03$ mail
......

Example 2: Examples of the dirname command.
...
bash-2.03$ DIRNAME=`dirname $HOME/personal/mail`;
bash-2.03$ echo $DIRNAME;
bash-2.03$ $HOME/personal

......

No comments: