Skip to Content
gnux

basenamex

Strip the directory and suffix from file names.

Mirrors GNU basename. Run it as basenamex, or as gnux basenamex.

Synopsis

basenamex [OPTION]... NAME [SUFFIX]

GNU reference

Description

basenamex prints NAME with any leading directory components removed. Trailing slashes are ignored, so 'dir/' yields 'dir', and a name made only of slashes yields the root. If SUFFIX is given and NAME ends with it, the suffix is removed too - unless that would leave nothing. With -a every operand is a NAME; -s supplies the suffix for all of them and implies -a. -z separates the output with NUL instead of newlines, for names that may contain newlines.

Permissions

basenamex works on the text of its operands only: it never touches the filesystem, so the names need not exist and no permissions are required.

Use cases

  • Getting a script's own name from $0.
  • Deriving an output file name by dropping the directory and extension of an input.
  • Stripping one extension from a whole batch of paths with -s.
  • Naming a directory after an archive, minus its .tar.gz.
  • Renaming or copying files in a loop while keeping their base names.
  • Printing several base names at once with -a, without a suffix.
  • Producing NUL-separated output with -z for names that may contain newlines.

Flags

      --help            display this help and exit
  -a, --multiple        support multiple arguments and treat each as a NAME
  -s, --suffix SUFFIX   remove a trailing SUFFIX; implies -a
      --version         output version information and exit
  -z, --zero            end each output line with NUL, not newline

Examples

basenamex /usr/bin/sort

Ignore trailing slashes

basenamex /home/user/

Drop the directory and the extension

basenamex include/stdio.h .h

Remove a multi-part extension

basenamex --suffix=.tar.gz release/gnux-1.0.tar.gz

Keep the name when removing the suffix would leave nothing

basenamex .h .h

Treat every operand as a NAME

basenamex -a /var/log/syslog /etc/hosts

Strip a suffix from several names at once

basenamex -s .go cmd/main.go core/core.go

Strip an extension from every match of a glob

basenamex -s .jpg photos/*.jpg

Separate the output with NUL bytes instead of newlines

basenamex -a -z a/b c/d | od -c
basenamex "$0"

Back up every photo under its base name

for f in photos/*.jpg; do cpx "$f" "out/$(basenamex "$f" .jpg).bak"; done

Create a directory named after an archive

name=$(basenamex -s .tar.gz gnux-1.0.tar.gz) && mkdirx "$name"

Strip a directory and extension from several logs with the gnux shell

gnux shell -c 'basenamex -s .log /var/log/app.log /var/log/db.log'

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

basenamex --help                # options, first examples, exit status
gnux help basenamex             # this page: the full manual
gnux help basenamex --OPTION    # one option in depth, with its examples

See also

On this page