diff options
author | John Ankarström <john@ankarstrom.se> | 2021-07-12 13:24:49 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-07-12 13:27:07 +0200 |
commit | 03d827e2fbc409ef97829f25b8eeca5204f81a3c (patch) | |
tree | 6099f0feb9adf3425fba87549b164043e18bd0c7 /etc/ord | |
parent | 45cddd072119c5abd7ec076cf28d51ee01f125b7 (diff) | |
download | xutil-03d827e2fbc409ef97829f25b8eeca5204f81a3c.tar.gz |
Re-organize files
Diffstat (limited to 'etc/ord')
-rwxr-xr-x | etc/ord | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -0,0 +1,56 @@ +#!/bin/sh + +# Order files by adding/changing numbers (like "01. ") in front of their names + +usage() { echo usage: $0 [-n number width] [-s separator] 1>&2; exit 1; } + +# Default values +n=2 +s='. ' + +# Parse options +while getopts n:s: o +do + case $o in + n) n=$OPTARG ;; + s) s=$OPTARG ;; + ?) usage ;; + esac +done +shift $((OPTIND-1)) + +# Validate options +case "$n" in +[1-9]) ;; +*) echo $0: n must be a number from 1 to 9 1>&2 + exit 1 ;; +esac +test -z "$1" && usage + +# Construct glob and regex substitution from -s and -n +i=0; while test $((i++)) -lt "$n"; do sub=$sub'[0-9]'; done +glob=$sub +sub="s/^$sub$(printf '%s\n' "$s" | sed 's/\([.*[\\]\|\]\)/\\&/g')//" + +totext() { + for f in "$@"; do printf '%s\n' "$f"; done | + sort | + sed "$sub" +} + +fromtext() { + nl -s.\ -w2 -nrz | { + i=1 + while read new + do + name=`printf '%s\n' "$new" | sed "$sub"` + if test -e "$name" + then mv "$name" "$new" + else mv $glob"$s$name" "$new" 2>&- + fi + done + } +} + +# Edit order +totext "$@" | ep | fromtext |