From 8078747f2700f562070b9d2908ee473387313e47 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 14 Jan 2022 20:03:31 +0100 Subject: Added sel script for bulk operations on files --- Makefile | 1 + sel | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100755 sel diff --git a/Makefile b/Makefile index daf38f0..63650aa 100644 --- 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 new file mode 100755 index 0000000..fd8020f --- /dev/null +++ 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 -- cgit v1.3