BATCH(1) | General Commands Manual | BATCH(1) |
batch
— A tool for
batch processing using your favorite text editor.
batch |
[switches] [-- ]
[arguments] |
batch
is a tool that takes a list of
items, lets you edit the list in your favorite text editor, then generates a
shell script to batch process unchanged items, renamed items, and deleted
items. You can edit the script to add your own logic for how to process each
kind of item, and then run it.
By default, the batch
utility reads a list
of items from standard input and writes unchanged items, renamed items, and
deleted items to standard output.
In addition to interactive use, you can supply shell script fragments to automate the editing and batch processing stages.
Run the following in your terminal.
batch *
The above is equivalent to:
ls | batch
This command opens the directory contents in an external editor.
Alternatively, enter batch
in your
terminal, paste the following text and hit
‘Control+D
’.
Star Platinum.png Magician's Red.png Hermit Purple.png Hierophant Green.png Silver Chariot.png The Fool.png
You can also create these files with the
‘batch -E -p touch
’ command.
After you edit and save the file, it will generate a shell script which does the specified actions according to the changes you did in the file.
Edited file:
star-platinum.png hermit-purple.png silver-chariot.png
Generated shell script:
# This file will be executed when you close the editor. # Please double-check everything, clear the file to abort. # Action on unchanged items. pick() { echo "$1" } # Action on modified items. map() { echo "$1" "$2" } map 'Star Platinum.png' 'star-platinum.png' map 'Hermit Purple.png' 'hermit-purple.png' map 'Silver Chariot.png' 'silver-chariot.png' # Action on deleted items. drop() { echo "$1" } drop 'Magician'"'"'s Red.png' drop 'Hierophant Green.png' drop 'The Fool.png'
This shell script is opened in an editor for you to review. After you close it, it will be executed.
By default, batch
uses whatever
you’ve set as your default text editor via one of the
VISUAL
or EDITOR
environment
variables, or else falls back to the
vi(1) editor to
process items.
To change that default to something else, you can use the
--editor
option:
batch -e vim
Now, no matter what is set as your default shell editor,
batch
will fire up
vim(1) to
process items.
batch
lets you apply external
filters—such as
tr(1),
sed(1) or
awk(1)—before
invoking the editor:
batch -f 'sed "s/ /-/g"'
This option can be used to ease your editing.
Combined with the --no-edit
flag,
batch
can be fully automated:
batch -f 'sed "s/ /-/g"' -E -m 'echo mv -vi --'
Finally, you might want to specify a shell script for your actions:
batch -M 'mkdir -vp -- "$(dirname -- "$2")" && mv -vi -- "$1" "$2"'
This command allows you to automatically create the destination directory when renaming files.
Depending on my situation here are the tools I use:
Since batch
does not try to load config
files from a specific folder at startup, the best way to emulate this is by
creating an alias in your shell profile.
Here is my personal configuration:
# interactive mv imv() { batch -e 'kak' -f 'iconv -f "UTF-8" -t "ASCII//TRANSLIT//IGNORE"' -f 'tr "A-Z" "a-z"' -f 'tr -s " '"'"'" "-"' -f 'tr -d "!,?"' -p ':' -M 'mkdir -vp -- "$(dirname -- "$2")" && mv -vi -- "$1" "$2"' -d 'rm -vi --' "$@" } # graphical mv xmv() { nnn -p - | imv "$@" }
Process files in the current working directory.
batch *
Process files from
‘argv
’.
batch *.png
Process files from stdin.
find . -type f | batch
The options are as follows:
-p
command,
--pick-command
=commandDefault is echo(1).
-m
command,
--map-command
=commandDefault is echo(1).
-d
command,
--drop-command
=commandDefault is echo(1).
-P
command,
--pick-shell-script
=command-M
command,
--map-shell-script
=command-D
command,
--drop-shell-script
=command-e
command,
--editor
=commandThe order of preference is the VISUAL
environment variable, then the EDITOR
environment variable, and then the default chosen at compile time, which
is usually
vi(1).
-f
command,
--filter
=command-E
,
--no-edit
--no-pick
--no-map
--no-drop
-
-h
,
--help
-V
,
--version
The following environment variables have an effect on
batch
.
VISUAL
,
EDITOR
Mathieu Ablasou <taupiqueur.kanto@gmail.com>
March 26, 2022 |