Skip to content

essentialkaos/path

GoReportCard Code Climate Maintainability Codebeat badge GitHub Actions CI Status GitHub Actions CodeQL Status

InstallationCommand-line completionMan documentationUsageCI StatusContributingLicense


path is a dead simple tool for working with paths. This tool provides commands which you can use to replace such tools as basename, dirname, and readlink and many more. But unlike these tools, path allows you to pass input not only as arguments, but also using standard input (for example with pipes). It's easy to use and doesn't require to know all this kung-fu with find or xargs.

Simple examples:

find . -iname '*.txt' -print0 | xargs -0 -n1 -- basename
# or
find . -iname '*.txt' | xargs -L1 -I{} basename "{}"
# with path
find . -iname '*.txt' | path basename
# Note that there is two spaces between {} and \; and if you forget
# about this it will don't work. Also in this case we will run 'basename'
# for each item in find output.
find . -mindepth 1 -maxdepth 1 -type d -exec basename {}  \;
# with path
find . -mindepth 1 -maxdepth 1 -type d | path basename

Also, it works MUCH faster:

$ git clone https://github.com/kubernetes/kubernetes.git --depth=1

$ cd kubernetes

$ hyperfine 'find . -iname *.go -print0 | xargs -0 -n1 -- basename' 'find . -iname *.go | path basename'

Benchmark 1: find . -iname *.go -print0 | xargs -0 -n1 -- basename
  Time (mean ± σ):     12.621 s ±  0.077 s    [User: 5.871 s, System: 7.043 s]
  Range (min … max):   12.512 s … 12.745 s    10 runs

Benchmark 2: find . -iname *.go | path basename
  Time (mean ± σ):     106.5 ms ±   1.5 ms    [User: 59.8 ms, System: 60.4 ms]
  Range (min … max):   104.1 ms … 111.1 ms    28 runs

Summary
  find . -iname *.go | path basename ran
  118.45 ± 1.80 times faster than find . -iname *.go -print0 | xargs -0 -n1 -- basename

Installation

From source

To build the path from scratch, make sure you have a working Go 1.20+ workspace (instructions), then:

go install github.com/essentialkaos/path@latest
sudo yum install -y https://pkgs.kaos.st/kaos-repo-latest.el$(grep 'CPE_NAME' /etc/os-release | tr -d '"' | cut -d':' -f5).noarch.rpm
sudo yum install path

Prebuilt binaries

You can download prebuilt binaries for Linux and macOS from EK Apps Repository:

bash <(curl -fsSL https://apps.kaos.st/get) path

Command-line completion

You can generate completion for bash, zsh or fish shell.

Bash:

sudo path --completion=bash 1> /etc/bash_completion.d/path

ZSH:

sudo path --completion=zsh 1> /usr/share/zsh/site-functions/path

Fish:

sudo path --completion=fish 1> /usr/share/fish/vendor_completions.d/path.fish

Man documentation

You can generate man page using next command:

path --generate-man | sudo gzip > /usr/share/man/man1/path.1.gz

Usage

Usage: path {options} {command}

Commands

  base path                 Strip directory and suffix from filenames
  dir path                  Strip last component from file name
  dirn num path             Return N elements from path
  link path                 Print resolved symbolic links or canonical file names
  clean path                Print shortest path name equivalent to path by purely lexical processing
  compact path              Converts path to compact representation
  abs path                  Print absolute representation of path
  ext path                  Print file extension
  match pattern path        Filter given path using pattern
  join root path            Join path elements
  add-prefix prefix path    Add the substring at the beginning
  del-prefix prefix path    Remove the substring at the beginning
  add-suffix suffix path    Add the substring at the end
  del-suffix suffix path    Remove the substring at the end
  exclude substr path       Exclude part of the string
  is-abs path               Check if given path is absolute
  is-local path             Check if given path is local
  is-safe path              Check if given path is safe
  is-match pattern path     Check if given path is match to pattern

Options

  --zero, -z         End each output line with NUL, not newline
  --space, -s        End each output line with space, not newline
  --quiet, -q        Suppress all error messages
  --no-color, -nc    Disable colors in output
  --help, -h         Show this help message
  --version, -v      Show version

Examples

  path base /path/to/file.txt
  → file.txt

  path dir /path/to/file.txt
  → /path/to

  path compact /very/long/path/to/some/file.txt
  → /v/l/p/t/s/file.txt

  ls -1 | path is-match '*.txt' && echo MATCH!
  Check if all files in current directory is match to pattern

  PATH_QUIET=1 path dir /path/to/file.txt
  Run dir command in quiet mode enabled by environment variable

CI Status

Branch Status
master CI
develop CI

Contributing

Before contributing to this project please read our Contributing Guidelines.

License

Apache License, Version 2.0