basename

Strip directory and suffix from filenames

Syntax
      basename NAME [SUFFIX]

      basename OPTION

Key
   --help
        Display help

   --version
        Output version information and exit

basename will print NAME with any leading directory components removed. If specified, it will also remove a trailing SUFFIX (typically a file extention).

Examples

Get the name of the home folder:

$ basename ~

Extract the file name from the variable pathnamevar and store in the variable result using parameter expansion $( )

$ result=$(basename "$pathnamevar")

For each of the files found in directoryA delete the matching files from directoryB:

$ for file in directoryA /*; do rm directoryB /`basename $file`; done

A script to rename file extensions:

#BatchRenameExt
for file in *.$1; do
mv $file 'basename $file $1'.$2
done

$ BatchRenameExt htm html

“Then I came up with this crazy idea just to walk out on the stage with no band at all and just start singing whatever came to mind. I actually fought the idea for a while because it seemed almost too radical, but it became obvious what I was supposed to be doing” ~ Bobby McFerrin

Related linux commands

dir - Briefly list directory contents.
dirname - Convert a full pathname to just a path.
ls - List information about file(s).
Equivalent Windows command: Variable Substring


 
Copyright © 1999-2024 SS64.com
Some rights reserved