To extract the filename and extension from a file path in a shell script, you can use the `basename` command.
Here is an example of how you can use basename to extract the filename and extension from a given file path:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env bash # Define a file path filepath="/var/log/mail.log" # Extract the filename and extension from the file path filename=$(basename "$filepath") # Extract the extension from file path extension="${filename##*.}" # Extract the filename without the extension filename="${filename%.*}" |
In this example, `$filepath` is the file path that you want to extract the filename and extension from. The basename command extracts the filename and extension from the file path and stores it in the filename variable.
The extension variable is then set to the string after the last . character in the filename variable, using the ## parameter expansion operator.
The filename variable is then set to the string before the last . character in the filename variable, using the %
parameter expansion operator.
This will extract the filename and extension from the file path and store them in the filename and extension variables, respectively.
You can then use these variables in your script as needed. For example, you might use the filename and extension variables to check the type of a file or to rename it.
1. Get filename without Path
First, remove the full file path from the input filename. For example, if the filename input is as “/var/log/mail.log” then extract the full filename mail.log only.
1 2 3 4 5 6 | #!/usr/bin/env bash filepath="/var/log/mail.log" filename=$(basename "$filepath") echo "Full filename: $filename" |
The output will be:
Full filename: mail.log
2. Filename without Extension
Now extract the filename without an extension from extracted full filename without a path as above.
1 2 3 4 5 6 | #!/usr/bin/env bash filepath="/var/log/mail.log" filename=$(basename "$filepath") filename="${filename%.*}" echo "Filename is: $filename" |
The output will be:
Filename is: mail
3. Get Extension Only
Now extract the file extension without name from extracted full filename without path.
1 2 3 4 5 6 7 | #!/usr/bin/env bash filepath="/var/log/mail.log" filename=$(basename "$filepath") extension="${filename##*.}" echo "Extension: $extension" |
The output will be:
Extension: log
4. Extract Directory Path
You can also use the `dirname` command to extract the directory path from the full file path. For example:
1 2 3 4 5 6 | #!/usr/bin/env bash filepath="/var/log/mail.log" dir=$(dirname "$filepath") echo "Directory: $dir" |
The output will be:
Directory: /var/log/
Conclusion
This quick how-to guide helps you to understand how to extract a filename and extension from the file path. Keep in mind that these commands only work with the bash shell. If you are using a different shell, you may need to use a different syntax.
4 Comments
What is the specified command name was a link name, and you want the actual file name?
it is got failed if the file is having space. can you fix and hell us
Enable full directory path in double quote if any of the directory or filename have space.
Fails if no ‘.’ is present in $filename. The result for $ext should be nil.
E.g.
path=”/etc/apache2/noext”
filename=$(basename “$path”)
ext=”${filename##*.}”
echo $ext
noext