Using `basename` command to read filename
`basename` command is used to read the file name without extension from a directory or file path.
Here, NAME can contain the filename or filename with full path. SUFFIX is optional and it contains the file extension part that the user wants to remove. `basename` command has some options which are described below.
|-a||It is used to pass multiple filenames with path or without path as command arguments.|
|-s||It is used to pass the extension as suffix that needs to remove.|
|-z||It is used to display the multiple filenames by separating each file with null.|
|–help||It is used to display the information of using `basename` command.|
|–version||It is used to display the version information.|
Example-1: Using NAME and SUFFIX
The following `basename` command will retrieve the filename with extension. SUFFIX is omitted from this command. Here, the output is ‘product.txt’.
If you want to retrieve the filename without extension, then you have to provide the file extension as SUFFIX with `basename` command. Here, the extension is “.txt”. Run the following command to remove the extension from the file.
Example-2: Using ‘-a’ option and NAME
The use of ‘-a’ option of `basename` command is shown in this example. Here, two file paths are passed as arguments with `basename` command. Each filename with extension will retrieve from the path and print by newline.
Example-3: Using ‘-z’ option and NAME
‘-z’ option is used with `basename` command to print the multiple filenames with null value instead of newline. The following command uses two options together, ‘-a’ and ‘-z’. Here, two filenames, index.html and emp.txt will print without any space or newline.
Example-4: Using ‘-s’ option and NAME
The following command can be used as an alternative of SUFFIX with `basename`. File extension needs to pass with ‘-sh’ option to remove the file extension from the file. The following example will remove the extension, ‘-sh’ from the file, ‘addition.sh’.
Example-5: Remove file extension without SUFFIX
If you don’t know the extension of the file that you want to remove from the filename, then this example will help you to solve the problem. Create a file named read_file.sh with the following code to retrieve filename of any extension. `sed` command is used in this example to remove any type of extension from the filename. If you run the script, the output will be ‘average’ after removing the extension ‘py’.
# Set the filename with path
# Read the filename without extension by using ‘basname’ and `sed` command
echo "$(basename "$filename" | sed 's/\(.*\)\..*/\1/')"
Run the script.
Example-6: Convert file extension from txt to docx
Filename without extension needs to convert the file from one extension to another. This example shows that how you can change the extension of all text files (.txt) into the word files (.docx) by using `basename` command in the bash script. Create a file named, convert_file.sh with the following code. Here, a for-in loop is used to read all the text files with “.txt” extension from the current directory. The filename without extension is read by `basename` command and renamed by adding “.docx” extension in each iteration of the loop.
# the loop will read each text file from the current directory
for filename in `ls *.txt`
# Print the text filename before conversion
echo "Filename before conversion : $filename"
# Change the extension of the file txt to docx
mv -- "$filename" "$(basename -- "$filename" .txt).docx"
Run the script.
Check the text files are converted or not by using `ls` command.
Example-7: Read filename without extension using Shell parameter expansion
Shell parameter expansion is another way to read the filename without extension in bash. This example shows the uses of shell parameter expansion. The following command will store the file pathname in the variable, $filename.
The following command will remove all types of extension from the path and store the file path without extension in the variable, $file1.
The following command will print the filename only from the path. Here, the output will ‘myfile’.
If the filename contains two extensions with two dot(.) and you want to read the filename by removing the last extension of the file then you have to use the following command. Run the following command that store the file path into the variable, $file2 by removing the last extension of the file.
Now, run the following command to print the filename with one dot (.) extension. Here, the output will be “myfile.tar”.
Filename without extension is required for various purposes. Some uses of filename without extension are explained in this tutorial by using some examples such as file conversion. This tutorial will help those users who are interested to learn the ways to separate the file name and extension from the file path. Two ways are explained here. The user can follow any of these ways to extract the filename only from the file path.