Saturday, December 1, 2007

HowTo: Strip Non-Directory Path Suffix from FileName

As recently blogged, here's the actual reverse process of stripping and removing non-directory suffix from a complete path filename. This blog entry is here to discuss and show on how to remove non-directory suffix of a path filename and retain only the directory name.

In short, stripping the non-directory suffix from /var/cache/named/etc/named.conf would only return the string value of /var/cache/named/etc/ .

Here's how to strip and remove the non-directory strings or suffix of a filename using the linux binary command dirname.

Man DirName
Dirname strips non-directory suffix from file name. If the specified input filename contains no /’s characters, dirname outputs ‘.’ (meaning the current directory) only. Dirname is part of coreutils linux package.

DirName Usage

Dirname Example 1:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# dirname test.txt
output:
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dirname Example 2:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# dirname /var/backup/vertito/Favorites/Links/www.domain.conf.url
output:
/var/backup/vertito/Favorites/Links
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dirname Example 3:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# dirname /etc
output:
/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Get the pattern alright.

This dirname command can be handy on retrieving only the directory path of a complete filename with directory path strings. You can combine he command with other linux shell and command line commands to achieve your other desired output specially on string manipulation.

Enjoy!

0 comments:

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