Linux basename command

The basename command in Linux is used to extract the base name or file name component from a given path or file name. It returns the file name without the directory portion.

Here’s an overview of how to use the basename command:

  1. Basic Usage:
    To extract the base name or file name from a path or file name, simply type basename followed by the path or file name. For example:
   basename /path/to/file.txt

This command will return the file name component of the path, in this case, file.txt.

  1. Examples:
  • Extracting the base name from a file path: basename /home/user/Documents/file.txt This command will return file.txt, which is the base name of the given file path.
  • Extracting the base name from a relative path: basename ../directory/file.txt This command will return file.txt, which is the base name of the given relative path.
  • Extracting the base name from a file name: basename file.txt This command will return file.txt, as file.txt does not contain a directory portion.
  • Removing a specific file extension:
    basename file.txt .txt
    This command will return file, as it removes the specified .txt extension from the file name.

The basename command is useful when you need to extract the file name component from a given path or file name. It can be used in scripting or when working with file manipulation tasks.

For more information about the basename command and its options, you can refer to the manual page by typing man basename in your terminal.