Friday, November 30, 2007

HowTo: Strip directory path and filename suffix

Here's another quick howto on stripping or removing directory folder path and/or filename suffix or filename extensions of a given complete or partial filename using the linux basename command.

Basename strips directory and suffix from filenames by definition. Basename binary is part of coreutils package. This basename command can be useful on handling filename strings or directory path on multiple counts. Instead of issuing a command or combinations of of shell string functions, awk, cut, tr, or sed command and other linux command, stripping leading directory path locations and filename suffixes can easily be done using basename command.

From the complete path location of /var/named/chroot/etc/named.conf , /var/named/chroot/etc/ and .conf can be removed by this basename command.

Basename command is designed to strip and remove those leading directory folder/path and filename extensions.

Here are ways on how to remove or strip leading folder path (directory name) and filename extensions (suffix filenames).

Example 1:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cd /etc
# basename named.conf .conf
output:
named
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 2:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# basename /var/log/messages
output:
messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 3:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# basename /etc/named.conf
output:
named.conf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 4:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# basename /etc/named.conf .conf
output:
named
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 5:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# basename /etc/named.conf conf
output:
named.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Get the pattern, alright.

Try it, basename can be extremely useful on long lines of shell scripts manipulating filenames.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy