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:

time find . -iname '*.go' -print0 | xargs -0 -n1 -- basename
# find . -iname '*.go' -print0  0.07s user 0.12s system 4% cpu 4.255 total
# xargs -0 -n1 -- basename  2.59s user 2.74s system 102% cpu 5.195 total

time find . -iname '*.go' | path basename
# find . -iname '*.go'  0.08s user 0.09s system 99% cpu 0.174 total
# path basename  0.02s user 0.02s system 21% cpu 0.207 total

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

About

Tool for working with paths

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors 2

  •  
  •