By Waseem Akram on 9/28/2024
The basename command in Linux is used to extract the last element of a file path. This is particularly helpful in bash scripts where you only need the...
The basename
command in Linux is used to extract the last element of a file path. This is particularly helpful in bash scripts where you only need the filename or directory name from a full path. The basename
command can be used in various ways to manipulate file paths and extract specific parts of the path. Additionally, it allows you to remove file extensions or suffixes.
In this guide, we'll explore how to use the basename
command with examples, showcasing its options and capabilities.
The basename command supports the following syntax:
NAME: The full file path or string to extract the base name from.
SUFFIX: An optional string that will be removed from the output.
The command prints the last component of the file path, and if needed, removes the specified suffix.
To get the last part of a file path, simply pass the file path to basename
:
In this example, the basename
command extracts the filename ls
from the path /usr/bin/ls
.
If the file path ends with a trailing slash, basename
will return an empty string. To handle this, you can use the -a
option to remove trailing slashes:
The -a
option removes trailing slashes from the input before processing it.
Without the trailing slash:
In both cases, basename returns the last directory in the path.
You can also use basename
with multiple file paths. The -a
or --multiple
option allows you to process multiple file paths in one go:
This command prints the base name of each file path provided.
If you want to remove a specific suffix from the file name, you can provide it as a second argument:
Here, basename
strips the trailing ctl from the file name.
You can also use this feature to strip common file extensions like .conf:
Alternatively, you can use the -s
or --suffix
option to achieve the same result:
Stripping Suffixes from Multiple Files
The -s
option can be used with multiple files to remove the same suffix:
The basename command is a simple yet powerful tool for handling file paths in Linux. It’s especially useful in bash scripting for extracting file names and removing extensions. With the options to process multiple files and strip suffixes, it's a handy command to have in your toolbox.
That’s all for this guide! If you found it useful, make sure to share it with others and subscribe for more Linux, sysadmin, and DevOps content.
Happy scripting!
Thanks for reading this post, if you like my work you can support by Become a Patron! Read other posts
Today we’ll dump LSASS.EXE process memory to obtain credentials and we also will be using some evasion techniques. During red team...
Read MoreToday we’re gonna see an effective technique to mantain access in Windows systems during red team operations just by modifying a registry key...
Read MoreToday we’ll learn an advanced shellcode injection technique used by Lazarus group which uses UuidFromStringA API call. In this technique, the malware..
Read MoreInstahack is a security tool officially designed to test the password strength of Instagram accounts using termux and kali with a brute force attack...
Read MoreToday we’re gonna see a simple malware development technique, Shellcode injection via CreateRemoteThread in Golang...
Read MoreToday we'll see how we can use Golang internal functions from syscall and golang.org/x/sys/windows packages to call Windows API. Other languages like C o C++ are specifically
Read More