How to get a Filename from the Absolute Path in Linux  

When I give you an absolute path to a specific file such as /etc/ssh/sshd_config and from that, if I ask you to tell me the file name, then you can easily tell me that the sshd_config is the file and the rest are supportive directories where the file exists.

The same thing you can do in your script, where it will automatically find and tell you the exact file name from the given absolute or full path.

So let’s see how to achieve this task using basename utilities and bash parameter substitution in this article, and after reading this article, you will be able to enforce the following steps on your bash script.

Extract the filename using the basename utility

When you want to extract a filename from a given path, then you should think about the basename command, which is part of Coreutils, so you will find the basename preinstalled on your system, and hence, you just need to run the following command to extract the filename from the full path in Linux.

To explain how the basename command will show you the output results, let us take an example that will give us a clear view of the command’s capabilities.

Let me take the full path of the sshd_config file with the basename command in the below snippet.

$ basename /etc/ssh/sshd_config

The output of the above command

trendoceans@shen:~$ basename /etc/ssh/sshd_config
sshd_config

And as I said, the basename command will show you just the filename by removing the directory information from the full path of the respective file.

Let’s take another example where you will see how to remove the suffix or extension from this manual file: /usr/share/man/man1/basename.1.gz.

If we use the above command, then it will just remove the file path and not the suffix that is attached to the filename.To remove it, you need to run the following command on your terminal.

$ basename --suffix=.1.gz /usr/share/man/man1/basename.1.gz
or
$ basename -s .1.gz /usr/share/man/man1/basename.1.gz

The output of the above command.

trendoceans@shen:~$ basename -s .1.gz /usr/share/man/man1/basename.1.gz
basename

Another interesting option is to use -a to accept multiple file paths to get a filename on the terminal. However, you can also achieve the same result when you are using the -s or –suffix option.

$ basename -a /usr/share/man/man1/basename.1.gz /usr/share/man/man1/mawk.1.gz

Extract Filename using bash parameter substitution

Alternatively, you can also get the file name from the full path of the file without using basename. Oh, that sounds cool, but how can we achieve this? Simply follow the steps given below.

To start with this, first you should know the fullpath of the file, which you want to store in a variable, and later you will print the file name using echo with a pattern to strip out the filepath.

Let’s say I do have the path of /usr/share/man/man1/basename.1.gz on which I’ll use to execute the following command to get the filename.

$ FILEPATH=/usr/share/man/man1/basename.1.gz
$ echo ${FILEPATH##*/}

The output of the above commands

trendoceans@shen:~$ FILEPATH=/usr/share/man/man1/basename.1.gz
trendoceans@shen:~$ echo ${FILEPATH##*/}
basename.1.gz

Apart from that, you can also extract the extension of the file by running the below command, which will strip the file path till it’s found the first period, and then it will display the extension of the file.

You can refer to the below code snippet to get the extension of the file name.

FILEPATH=/usr/share/man/man1/basename.1.gz 
echo "${FILEPATH#*.}"            # It will print 1.gz 
echo "${FILEPATH##*.}"           # It will print gz

Quite simple, isn’t it?

Cheat sheet to get the filename from the full path

  • Extract the file name from the full path using the basename command
    • $ basename /usr/share/man/man1/basename.1.gz
  • Extract file names from the full path without extension or suffix using the basename command
    • $ basename --suffix=.1.gz /usr/share/man/man1/basename.1.gz
  • Extract the filename from the multiple full paths using the basename command
    • $ basename -a /usr/share/man/man1/basename.1.gz /usr/share/man/man1/mawk.1.gz
  • Extract the filename from the full path using the bash parameter substitution
    • $ FILEPATH=/usr/share/man/man1/basename.1.gz
    • $ echo ${FILEPATH##*/}
  • Extract extension from the full path using bash parameter substitution
    • FILEPATH=/usr/share/man/man1/basename.1.gz
    • echo "${FILEPATH##*.}"

That’s all for now, and make sure to note down the above cheat sheet for later use.

Leave a Reply