C Shell Wiki
Posts (Latest 10 updated) :
Read all
Contents:
  1. [Unix] C Shell (csh) basename Uso: Extract the filename from a path
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Unix] C Shell (csh) basename Uso: Extract the filename from a path

Overview

The basename command is used to strip the directory and suffix from file names, leaving only the base file name. This is particularly useful when you need to manipulate or display just the file name without its path or extension.

Usage

The basic syntax of the basename command is as follows:

basename [options] [arguments]

Common Options

  • -s, --suffix: Remove a specified suffix from the file name.
  • -a, --multiple: Process multiple files and return their base names.

Common Examples

Here are some practical examples of using the basename command:

  1. Extracting the base name from a file path:

    basename /usr/local/bin/script.sh
    

    Output:

    script.sh
    
  2. Removing a suffix from a file name:

    basename report.txt .txt
    

    Output:

    report
    
  3. Processing multiple files:

    basename -a /home/user/file1.txt /home/user/file2.txt
    

    Output:

    file1.txt
    file2.txt
    
  4. Using basename with a custom suffix:

    basename /var/log/syslog.log .log
    

    Output:

    syslog
    

Tips

  • Always specify the suffix if you want to remove it; otherwise, basename will return the full file name.
  • Use the -a option to handle multiple files in a single command, which can save time and effort.
  • Consider using basename in scripts where you need to extract file names for logging or processing purposes.