pathmut is a path string manipulation utility
This was made because I'm tired of resorting to basename, dirname, cut -d. -f1 or whatever other random core util to just extract some information from paths.
Important to know this utility only works with path strings and doesn't touch the file system whatsoever.
$ pathmut
Mutate path strings
Usage: pathmut [OPTIONS] [COMMAND or COMPONENT]
Commands:
get Read a path component [default]
delete Remove a path component
replace Replace an existing path component
set Set a path component
has Check if a path component exists
is Ask questions about a file path
normalize Normalize a file path
convert Convert between unix and windows paths
depth Number of components before the last component.
help Print this message or the help of the given subcommand(s)
Options:
-n, --normalize Normalize the path first
-u, --as-unix Parse paths as unix paths
-w, --as-windows Parse paths as windows paths
-h, --help Print help information
-V, --version Print version information
Components:
ext File extension
stem File stem
prefix File prefix
name File name
parent Parent of the file or directory
disk Disk of a windows path
n Ordinal of the nth component
Installation
For now, you have to build from source. The package is available on crates.io and can be installed with cargo.
cargo install pathmut
You can also build using nix if you have flakes enabled.
nix shell github:rutrum/pathmut
Future Development
I'd like this to be the all in one tool for manipulating path strings. This tool should never touch or look at the actual filesystem, which means it shouldn't be able to answer if a path exists, if something is a file, create files, etc.
I should be able to ask questions about path strings, like the following:
- Is the path valid, if it did exist?
- What is the depth of the path?
I should be able to work with multiple paths:
- Does path A begin with path B?
- Does path A end with path B?
- Is path B contained with path A?
- Join path A and path B together.
Why stop at paths? A command line utility built to mutate URIs like "scheme://user:pass@sub.domain.com/route?query=param#anchor" would have an almost identical API, and would even overlap with some path functionality. In the future, this library will expand to include URIs as well.
Changelog
0.8.0
- Add URL parsing with
-u/--as-urlflag - Add URL components:
scheme,host,port,path,query,frag,user,pass,origin,tld - Add
infocommand to print detailed path information--jsonflag to output as JSON instead of YAML
- Add
is urlquestion to check if a path is a URL - Rename
prefixcomponent towinprefixfor Windows path prefix - Change
-ushort flag to mean--as-url; unix paths now use-x/--as-unix - Remove
normalizecommand - Remove
-n/--normalizeflag - Remove
is normalizedquestion
0.7.0
- Normalize flag
-nthat can apply normalization to the paths before any command - Flags to force parsing paths as windows
-wor as unix-upaths - Add
is normalcommand which checks if a path string is normalized - Add
depthcommand which counts the number of components before the last - Add
diskcomponent for Windows paths with disk prefix
0.6.0
- Specify nth component using negative indicies to count backwards,
-1for last component, etc. - Add
normalizecommand that removes..and.from path appropriately - Add
convertcommand to convert between Windows and unix paths
0.5.0
- Rename
replacetoset - Add
replacecommand, which does not change paths when the component does not exist - Windows and unix path support, independent of native OS
- Add
iscommand which tests if paths are relative, absolute, windows, or unix--printflag to print true/false instead of changing exit code--anyand--allflags for testing multiple paths
- Add
hascommand for checking if a path component exists
0.4.0
- Remove
--removeand--replaceargs withdeleteandreplacecommands - Add
getcommand (default behavior) - Components are now arguments to
get,delete, andreplace - Specify nth component by using number,
0for first component, etc. - Remove
firstcomponent
0.3.0
- Add
firstcommand - Add
--removeand--replacearguments for every command - Remove
rmextcommand
0.2.0
- Show help by default
- Read from stdin, all at once
- Allow passing multiple file paths as args (not stdin)
- Add
rmextcommand
0.1.0
- Add
ext,stem,prefix,name,parentcommands.