Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

basename Command in Linux with examples

Last Updated : 01 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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 'basename' command

$basename NAME [SUFFIX]         or $basename OPTION NAME 

where,

  • NAME: refers to the file name or file full pathname and
  • OPTION: refers to the options compatible with the

'basename' command and in case, if 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 off.

Example :

/* basename removes directory path and     returns the file name, kt */ $basename /usr/local/bin/kt kt 

In the first case, the 'basename' command removes the directories name from the full path name given of file 'kt' and in the second case 'basename' removes the 'suffix.html' from 'kt.html'.

Options for basename command :

  • '-a, - -multiple' option : 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.
    /* -a is used for multiple inputs */ $basename -a /usr/local/bin/kt /usr/local/bin/kt.html kt kt.html 
  • '-s, - -suffix = SUFFIX' option : This option removes a trailing suffix SUFFIX,such as a file extension.
     /*.html suffix is removed from kt.html     when followed by -s option */ $basename -s .html kt.html kt 
  • '-z' option : This separates the output with NULL rather than a newline.
    /* The outputs of the two arguments passed    are not separated by a newline*/ $basename -az /usr/local/bin/kt kt.html ktkt.html 
  • '- -help' option : This displays the help information and exit.
  • '- -version' option : This displays the version information and exit.

Application of basename command :

There are many utilities available which are only useful when it comes to shell scripts and doesn't have any practical application when used by itself and 'basename' comes under this category.

Example of a shell script which converts gif image files into png image files with the help of basename command:

/*shell script to convert the gif image file format into png image file format */  #!/bin/sh for file in *.gif;do     #exit if there are no files     if [! -f $file];then         exit     fi     b='basename $file .gif'     echo NOW $b.gif is $b.png     giftopnm $b.gif | pnmtopng >$b.png done 

In the above shell script 'basename' command is used to extract the file names with extension '.gif' without the extension so that they can be converted into '.png' files.

References: basename command


D

Dimpy Varshni
Improve
Article Tags :
  • Misc
  • Linux-Unix
  • linux-command
  • Linux-file-commands
Practice Tags :
  • Misc

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
    atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version
    1 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences