Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbush committed Jun 6, 2020
0 parents commit 0ae2164
Show file tree
Hide file tree
Showing 19 changed files with 1,290 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
include config.mk

all:
@echo no need to build

install:
install -D -m 755 bulkrename ${DESTDIR}${PREFIX}/bin/bulkrename
install -D -m 755 pick ${DESTDIR}${PREFIX}/bin/pick
install -D -m 755 preview ${DESTDIR}${PREFIX}/bin/preview
install -D -m 755 skel ${DESTDIR}${PREFIX}/bin/skel
install -D -m 755 trash ${DESTDIR}${PREFIX}/bin/trash
install -D -m 755 untrash ${DESTDIR}${PREFIX}/bin/untrash
install -D -m 755 xopen ${DESTDIR}${PREFIX}/bin/xopen
install -D -m 644 bulkrename.1 ${DESTDIR}${MANPREFIX}/man1/bulkrename.1
install -D -m 644 pick.1 ${DESTDIR}${MANPREFIX}/man1/pick.1
install -D -m 644 preview.1 ${DESTDIR}${MANPREFIX}/man1/preview.1
install -D -m 644 skel.1 ${DESTDIR}${MANPREFIX}/man1/skel.1
install -D -m 644 untrash.1 ${DESTDIR}${MANPREFIX}/man1/untrash.1
install -D -m 644 xopen.1 ${DESTDIR}${MANPREFIX}/man1/xopen.1

uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/bulkrename
rm -f ${DESTDIR}${PREFIX}/bin/pick
rm -f ${DESTDIR}${PREFIX}/bin/preview
rm -f ${DESTDIR}${PREFIX}/bin/skel
rm -f ${DESTDIR}${PREFIX}/bin/trash
rm -f ${DESTDIR}${PREFIX}/bin/untrash
rm -f ${DESTDIR}${PREFIX}/bin/xopen
rm -f ${DESTDIR}${MANPREFIX}/man1/bulkrename.1
rm -f ${DESTDIR}${MANPREFIX}/man1/pick.1
rm -f ${DESTDIR}${MANPREFIX}/man1/preview.1
rm -f ${DESTDIR}${MANPREFIX}/man1/skel.1
rm -f ${DESTDIR}${MANPREFIX}/man1/untrash.1
rm -f ${DESTDIR}${MANPREFIX}/man1/xopen.1

.PHONY: all install uninstall
72 changes: 72 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
FMUtils

FMUtils are a set of POSIX scripts that implement some handy file
management utilities. Each script has its respective manual page.

The scripts are the following.
• bulkrename: Bulk rename files with your text editor.
• pick: Interactivelly select options.
• preview: Generate text preview of a file to be used by lf(1) or ranger(1).
• skel: Create a template file in the current directory.
• trash: Move files to trash following XDG trash specification.
• untrash: Remove files fropoint to m trash following XDG trash specification.
• unpack: Unarchive archived files (.zip, .rar, .tar.gz, etc).
• xopen: Open file or URL with an application.


§ Installation

First, edit ./config.mk to match your local setup.

Since FMUtils are just script, they do not need to be built.

By default, FMUtils are installed into the /usr/local prefix. Enter the
following command to install FMUtils (if necessary as root). This command
installs the scripts into the ${PREFIX}/bin/ directory, and the manual
pages into the ${MANPREFIX}/man1/ directory.

make install


§ Running FMUtils

The following are examples of how to use FMUtils.

Bulk rename all files in the current directory:
$ ls | bulkrename

Bulk rename all .jpg files in the ~/Downloads directory:
$ bulkrename ~/Downloads/*.jpg

Pick can be used to interactivelly remove files with rm(1). This
example is equivalent to `rm -i`, but can be used with other commands
that, unlike rm(1), does not provide a -i option.
$ rm $(pick *)

The previous example rely on the condition that each filename does not
contain space on it. The following example avoid this by using a
quotion option, such as -q on pick(1), and a pipe into xargs(1):
$ pick -q * | xargs rm

Add the following line to your ~/.config/lf/lfrc to use preview(1) as
file previewer.
set previewer preview

Move all .jpg files to trash:
$ trash *.jpg

Remove all .jpg files from trash (the environment variable $TRASH is
supposed to have the path of the trash)
$ untrash $TRASH/files/*.jpg

Extract the content of file.zip into your home directory.
$ unpack file.zip $HOME

Open a file with the default application specified, as specified in the
file whose path is in the environment variable $MIMEFILE.
$ xopen file.jpg


§ License

This software is in public domain and is provided AS IS, with NO WARRANTY.
102 changes: 102 additions & 0 deletions bulkrename
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh
# bulkrename: bulk rename files using $EDITOR and temporary files
# this file in public domain

set -f -e -u

EDITOR="${EDITOR:-${VISUAL:-vi}}"

usage()
{
echo "usage: bulkrename [file...]" >&2
exit 1
}

# quote and escape filename for moving
quote()
{
echo "$1" | sed "s/'/'\\\\''/g; s/^/'/; s/\$/'/"
}

# populate the files $old and $new with filenames, one per line
populatefiles()
{
if [ $# -eq 0 ]
then # run on stdin
cat | tee "$old" > "$new"
else # run on arguments
for i
do
printf "%s\n" "$i" >> "$old"
printf "%s\n" "$i" >> "$new"
done
fi
}

# edit the filenames in $new and test if they are valid
editfile()
{
if [ "$(wc -l <"$old")" -ne "$(sort -u "$old" | wc -l)" ]
then
echo "bulkrename: repeated filenames to be renamed" >&2
exit 1
fi

"$EDITOR" "$new" </dev/tty

if [ $(wc -l <"$new") -ne $(wc -l <"$old") ]
then
echo "bulkrename: number of filenames not equal to number of files" >&2
exit 1
fi

if [ "$(wc -l <"$new")" -ne "$(sort -u "$new" | wc -l)" ]
then
echo "bulkrename: repeated target filenames" >&2
exit 1
fi
}

# create $cmd file
createcmd()
{
cat /dev/null >"$cmd"
while read -r from <&3 && read -r to <&4
do
if [ "$from" != "$to" ]
then
from=$(quote "$from")
to=$(quote "$to")
printf "mv %s\t%s\n" "$from" "$to" >> "$cmd"
fi
done 3<"$old" 4<"$new"
}

# remove temporary files
cleanup()
{
rm -f "$old" "$new" "$cmd"
}

while getopts 'h' c
do
case "$c" in
h|*)
usage
;;
esac
done
shift $((OPTIND -1))

old=$(mktemp)
new=$(mktemp)
cmd=$(mktemp)

trap cleanup EXIT

populatefiles "$@"
editfile
createcmd
sh -x "$cmd"

exit 0
56 changes: 56 additions & 0 deletions bulkrename.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.TH bulkrename 1
.SH NAME
bulkrename \- edit filenames interactively within editor
.SH SYNOPSIS
.B bulkrename
.RI [ arg ...]
.SH DESCRIPTION
.B bulkrename
receives filenames from its arguments (one per argument) or from stdin
(one per line) and opens an editor to edit them; then, it renames each
file to the corresponding edited one.
.PP
If there are no arguments,
.B bulkrename
operates on stdin, reading one filename per line.
If there are arguments,
each argument is read as a filename.
.PP
The environment variables
.B $EDITOR " and " $VISUAL
are checked, in this order, for an editor program.
If both are unset, use
.IR vi (1)
by default.
.SH ENVIRONMENT
The following environment variables affect the execution of
.B bulkrename
.TP
.B EDITOR
The editor to be used.
.TP
.B VISUAL
The editor to be used if
.B $EDITOR
is not set.
.SH EXIT STATUS
.TP
.B 0
Success.
.TP
.B >0
Error occurred.
.SH EXAMPLES
Bulk rename all files in current directory (using stdin):
.IP
.EX
$ ls | bulkrename
.EE
.PP
Bulk rename all .jpg files in the ~/Downloads directory (using arguments):
.IP
.EX
$ bulkrename ~/Downloads/*.jpg
.EE
.SH SEE ALSO
.IR vi (1)
3 changes: 3 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# paths
PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man
75 changes: 75 additions & 0 deletions pick
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/sh
# pick: select arguments
# this file in public domain

quoting_style="${QUOTING_STYLE:literal}"
delimiter='
'
IFS='
'

usage() {
echo "usage: pick [-bnq] arg..." >&2
exit 1
}

pick() {
# print prompt to terminal and read response from terminal
printf "%s? " "$i" >/dev/tty
read response </dev/tty

case "$response" in
y*|s*)
if [ "$quoting_style" = "escape" ]
then
printf "%s" "$i" | sed "s/\\([ ]\\)/\\\\\\1/g; s/'/\\\\'/g"
elif [ "$quoting_style" = "shell" ]
then
printf "'"
printf "%s" "$i" | sed "s/'/'\\\\''/g"
printf "'"
else
printf "%s" "$i" | sed "s/'/\\\\'/g"
fi
printf "%s" "$delimiter"
;;
q*)
exit 1
;;
esac
}

while getopts 'bqn' c
do
case "$c" in
b)
quoting_style="escape"
;;
q)
quoting_style="shell"
;;
n)
delimiter=" "
;;
*)
usage
;;
esac
done
shift $((OPTIND -1))

if [ $# -eq 0 ]
then
usage
elif [ $# -eq 1 -a $1 = '-' ]
then
while read -r i
do
pick "$i"
done
else
for i
do
pick "$i"
done
fi
Loading

0 comments on commit 0ae2164

Please sign in to comment.