Saturday 2 November 2019

Dirname Command Examples in Unix / Linux

Dirname Command, LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning

The unix dirname command strips non-directory suffix from a file name.

The syntax of dirname command is

dirname NAME

The dirname command removes the trailing / component from the NAME and prints the remaining portion. If the NAME does not contain / component then it prints '.' (means current directory).

Dirname command is useful when dealing with directory paths in unix or linux operating systems. Some examples on dirname command are shown below:

Dirname Command Examples:


1. Remove the file name from absolute path.

Let say my directory path is /usr/local/bin/add.sh. Now i want to remove /add.sh and display only /usr/local/bin, then we can use the dirname command.

> dirname /usr/local/bin/add.sh
/usr/local/bin

2. dirname sum.pl

Here you can see that the NAME does not contain the / component. In this case the dirname produces '.' as the output.

> dirname sum.pl
.

Dirname Command, LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning

Note: The directories and filename which i have passed as arguments to dirname command in the above examples are just strings. There is no need of these directories or files to exist in the unix machine.

Related Posts

0 comments:

Post a Comment