basename Command in Linux with examples
Last Updated :
18 Jul, 2025
The 'basename' command in Linux is a fundamental utility used in file manipulation and script writing. It simplifies file paths by stripping directory information and optional suffixes from file names. Here is a detailed overview of the 'basename' command, including its syntax, options, and practical usage examples.
What is the 'basename' Command?
'basename' is used to extract the base file name from a given path by removing the path up to and including the last slash ('/'). This command is particularly useful in scripting and programming where file names need to be handled separately from their directory paths.
Syntax of the 'basename' command
basename [path] [suffix]
where,
- Path: Refers to the location of a file or directory in the filesystem. It shows how to navigate from the root(/) or current directory to the target file.
- Suffix: Suffix refers to a specific string (usually a file extension) that you want to remove from the end of the filename.
The 'basename' command is used for extracting the file name.
Syntax:
basename /home/user/filename.txt
In case you want to strip off the suffix of a file, you can give the filename followed by the SUFFIX name you want to get rid of.
Syntax:
basename /home/user/filenmame.txt .txt
Options for basename Command
The following basename
command options are specific to the GNU coreutils version, commonly found on most Linux distributions.
Syntax:
basename [OPTION]... NAME [SUFFIX]
where,
- OPTION: refers to the options compatible with the
- NAME: refers to the file name or file full pathname
Option 1: '-a, - -multiple':
This option lets you support multiple arguments and treat each as a NAME i.e you can give multiple file names or full path names with the use of -a option.
Syntax:
basename -a /path/filename1.txt /anotherpath/filename2.log
Option 2: '-s, - -suffix = SUFFIX'
This option removes a trailing suffix SUFFIX,such as a file extension.
Syntax:
basename - .txt /path/filename.txt
Option 3: '-z' option:
This separates the output with NULL rather than a newline.
Syntax:
basename -z /path/filename.txt
Option 4: '--help':
This command help to prints a message explaining the usage of the command and available options.
Syntax:
basename --help
Option 5: '--version'
It shows the version information of the basename
command.
Syntax:
basename --version
Purpose of the command "basename"
- Displays just the file name portion of a full directory path.
- Helps isolate the file name from its full path for easier processing.
- Facilitates path manipulation in shell scripts by returning only the filename component.
- Commonly used in shell scripts to extract and manage file names from full paths.
Use Cases of the Command 'basename'
- Extracting filenames from full file paths within scripts
- Stripping file extensions to work with base filenames
- Presenting clean, simplified output by omitting directory paths
Conclusion:
The basename
command is a lightweight yet highly useful tool in Linux for working with file paths. It is used to retrieve only the filename portion from a complete path and can also remove a specified suffix like a file extension. This functionality is particularly handy in shell scripting, where accurate and tidy handling of file names is essential. By separating the filename from its directory path, basename
improves the clarity, adaptability, and effectiveness of scripts that involve file processing.
Similar Reads
ar command in Linux with examples The 'ar' command in Linux is a versatile tool used for creating, modifying, and extracting files from archives. An archive is essentially a collection of files bundled together in a specific structure, making it easy to manage multiple files as a single entity. Each file within the archive is referr
5 min read
arch command in Linux with examples The arch command is a simple yet powerful utility in Linux used to display the architecture of the system's hardware. By running this command, users can quickly determine the type of processor their system is running on, such as i386, x86_64, or arm. Knowing the architecture is crucial for installin
2 min read
arp command in Linux with examples arp command manipulates the System's ARP cache. It also allows a complete dump of the ARP cache. ARP stands for Address Resolution Protocol. The primary function of this protocol is to resolve the IP address of a system to its mac address, and hence it works between level 2(Data link layer) and leve
2 min read
aspell command in Linux with examples aspell command is used as a spell checker in Linux. Generally, it will scan the given files or anything from standard input then it check for misspellings. Finally, it allows the user to correct the words interactively. Spell checking is crucial when working with large documents, coding, or writing
3 min read
atd command in Linux with examples atd is a job scheduler daemon that runs jobs scheduled for later execution. These jobs are one-time task(not recurring) at a specific time scheduled using 'at' or 'batch' utility. Syntax: atd [-l load_avg] [-b batch_interval] [-d] [-f] [-s] Options: -l : Specifies a limiting load factor, over which
2 min read
atrm command in Linux with examples The command "atrm" is used to delete scheduled at jobs by specifying their job number. Users can only remove their own jobs, while only the superuser (root) has the privilege to delete jobs scheduled by other users.Syntax:atrm <job_id>For Example:Example 1: Schedule the jobs by using the "at"
2 min read
atq command in linux with examples atq displays the list of pending jobs which are scheduled by the user. If the user is the superuser then the pending jobs of all the users will be displayed. The output lines display Job number, date, hour, queue, and username for each job. Syntax: atq [-V] [-q queue] Options: -V: It will display th
2 min read
autoconf command in Linux with examples autoconf command is used in Linux to generate configuration scripts. Generate a configuration script from a TEMPLATE-FILE if given, or from âconfigure.acâ if present, or 'configure.in'. The output is sent to the standard output if TEMPLATE-FILE is given, otherwise, it is sent into âconfigureâ. To us
1 min read
autoheader command in Linux with Examples autoheader command in Linux is used to create a template file of C â#defineâ or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This comma
3 min read
automake command in Linux with Examples automake is a tool used for automatically generating Makefile.in files compliant with the set GNU Coding Standards. autoconf is required for the use of automake. automake manual can either be read on-line or downloaded in the PDF format. More formats are also offered for download or on-line reading.
1 min read