How to Get Filename From Path in Bash?

In Linux, bash script is a shell in which commands are written in sequence. Users can write anything in the bash script, such as programs and commands, and run it to get the file’s output. Using this user can also get file names from the path. In this post, you will learn to get file names from the path using the bash script. The post’s content is as follows:

Let’s get started.

Method 1: How to Get File Name From the Path Using Bash script?

The file name can easily be obtained using the Bash script. Create a bash script file and type the script into the file, make the file executable, and run that file to get the output. Let’s implement and learn this method in the following steps.

Step 1: Create the Bash Script File

Create the file with the .sh extension using the touch command. To create the file is obtained as follows:

$ touch B-Script.sh

The file will be created, as can be seen in the above image.

Step 2: Make the File Executable

Make executable using the “chmod” command. To do so, the following command will be used:

$ chmod +x B-Script.sh

Bash file is executable.

To verify that file is executable, run the following command in the terminal:

$ ls -l B-Script.sh

The above image shows the executable permission for the user.

Step 3: Type the Script

Now, you need to type the script inside a bash shell of the file by typing the below command and pressing enter:

$ nano B-Script.sh

After entering, type the below script and save the file:

#!/bin/bash
filepath="/home/linuxfoss.txt.gz"
filename=${filepath##*/}
echo $filename

You can also open the file manually for typing the script:

Let’s move to the next step.

Step 4: Run the Bash Script File

Run the Bash file in the terminal to check the output. To do so, the following command is useful:

$ ./B-Script.sh

The above image has displayed the file name.

Note: File name can also be used by the terminal through bash script parameters. To do so, execute the following command one by one:

$ filepath="/home/linuxfoss.txt.gz"
$ filename=${filepath##*/}
$ echo $filename

Let’s move to the second method to get the file name.

Method 2: How to Get File Name From the Path Using Basename Command?

The Basename command is used to print the base path (excluding the parent path), which is useful in the bash script of the file where the user can extract the file name. The syntax for the basename command is given below:

Syntax:

$ Basename /Path/to/File

Let’s check the base command in the bash script:

Open your bash file with nano editor. To do so, execute this command in the terminal:

$ nano B-script.sh

Once you enter the above command, this will take you to the editor. Now, add the command in the script:

#!/bin/bash
Basename /home/linuxfoss.txt.gz

Save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X”.

Run the bash script to check the file output:

$ ./B-script.sh

The output shows that the file name has been retrieved from the path.

For multiple paths, the following command will be used; we are using three paths to get file names:

#!/bin/bash basename -a /home/foss.txt /home/linuxfoss.txt.gz /home/get-pip.py

Put this command in the bash script file:

Save and run the file to check the output:

$ ./B-script.sh

All three file names have been displayed, as shown in the above image.

Bonus Tip: How to Get and Remove Extensions From the File Name?

You can also get the extension from the bash script from the filename. For this feature, type the following script into the bash file:

#!/bin/bash
filepath="/home/linuxfoss.txt.gz"
echo "${filepath#*.}"

After that, run the bash file using the following command:

$ ./B-Script.sh

The above command will display only the extension of the file name, which can be seen in the above image.

Conclusion

In Linux, file names can be obtained using the bash file through a bash script, bash script parameters, or the basename command. In this article, all the possible ways to get file names from the path have been demonstrated. Apart from that, methods to get an extension from the file name or remove the extension from the file name have also been illustrated in this post.