Get Filename From Given File Path in Linux

Get Filename From Given File Path in Linux

When working with files processing in Linux, may be needed to get filename from given file path. This tutorial shows how to do that in Linux.

Linux has basename command that allows to retrieve filename from file path while removing the directories if any.

basename /var/log/auth.log

Output:

auth.log

The -a option can be used to provide multiple arguments:

basename -a /var/log/auth.log /var/log/syslog /etc/hosts

Output:

auth.log
syslog
hosts

Leave a Comment

Cancel reply

Your email address will not be published.