commit 8078747f2700f562070b9d2908ee473387313e47
parent 1b1586a27ec2ad90ce4f0e8a68d0075d250a1b7b
Author: Sebastiano Tronto <sebastiano.tronto@gmail.com>
Date: Fri, 14 Jan 2022 20:03:31 +0100
Added sel script for bulk operations on files
Diffstat:
M | Makefile | | | 1 | + |
A | sel | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 100 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -24,6 +24,7 @@ SCRIPTS = addressgrep \
popup-cal12 \
popup-cal3 \
popup-terminal \
+ sel \
sfeed-browser \
spawn \
templess \
diff --git a/sel b/sel
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+# Allow bulk operations on a list of selected files.
+# Usage: sel [command]
+# If no command is specified, shows the list of selected files
+# Commands:
+# add [files|dirs|all]: add files to selection
+# clear: clear selection
+# cp: copy to current dir, possbily after editing filenames
+# edit: open selection in editor
+# mv: move to current dir, possbily after editing filenames
+# rm: remove selected files and clear selection
+
+# Requires: dmenu (or similar), trashrm (optional)
+
+# TODO: add possibility of specifying file.
+# TODO 2: The usage of paste(1) is a bit of a hack, and for example it does
+# not work if filenames contain tab characters. Fix this.
+
+file="$XDG_DATA_HOME/selectedfiles"
+menu=${MENU:-dmenu}
+editor=${EDITOR:-vi}
+rm=trashrm # replace with rm -r if you don't use trashrm
+
+clear() {
+ cat /dev/null > "$file"
+}
+
+cphere() {
+ file2=$(mktemp)
+ sed 's/^.*\///' "$file" > "$file2"
+ $editor "$file2"
+ paste "$file" "$file2" | while read f; do
+ fold=$(echo "$f" | sed 's/\t.*//')
+ fnew=$(echo "$f" | sed 's/.*\t//')
+ cp -R "$fold" ./"$fnew"
+ done
+ clear
+}
+
+remove() {
+ while read f; do
+ $rm "$f"
+ done < "$file"
+}
+
+if [ -z "$1" ]; then
+ cat "$file"
+else
+case "$1" in
+ add)
+ if [ -z "$2" ]; then
+ dmenu-filepicker >> "$file"
+ else
+ case "$2" in
+ files)
+ ls | while read f; do
+ [ -f "$f" ] && echo "$PWD/$f" >> "$file"
+ done
+ ;;
+ dirs)
+ ls | while read f; do
+ [ -d "$f" ] && echo "$PWD/$f" >> "$file"
+ done
+ ;;
+ all)
+ ls | while read f; do
+ echo "$PWD/$f" >> "$file"
+ done
+ ;;
+ *)
+ echo "$2: not a valid option"
+ ;;
+ esac
+ fi
+ sort -u -o "$file" "$file"
+ ;;
+ clear)
+ clear
+ ;;
+ cp)
+ cphere
+ ;;
+ edit)
+ $editor "$file"
+ ;;
+ mv)
+ cphere
+ remove
+ ;;
+ rm)
+ remove
+ clear
+ ;;
+ *)
+ echo "$1: not a valid sel command"
+ ;;
+esac
+fi