aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README33
-rwxr-xr-xbulkrename42
2 files changed, 75 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..a951f39
--- /dev/null
+++ b/README
@@ -0,0 +1,33 @@
+Description
+===========
+
+Bulkrename allows you to pipe a list of filenames and to rename them
+interactively within your editor. It is inspired by ranger's bulkrename
+command (see http://nongnu.org/ranger/).
+
+Why ?
+=====
+
+When dealing with a great number of files, or having the same modification to
+do a lot of time it it often easier to do it in our well-known editor.
+
+Documentation
+=============
+
+Just pipe a list of files to bulkrename.
+
+If your $EDITOR environment variable is not set, vi will be called.
+
+License
+=======
+
+The code is under the WTFPL (see http://www.wtfpl.net/) mainly because I
+wanted to stay at 42 lines of code and the GPLv3 was too long.
+
+Contact
+=======
+
+Main developper: Cédric Picard
+Email: cedric.picard@efrei.net
+Twitter: @Cym13
+GPG: 383A 76B9 D68D 2BD6 9D2B 4716 E3B9 F4FE 5CED 42CB
diff --git a/bulkrename b/bulkrename
new file mode 100755
index 0000000..70f2407
--- /dev/null
+++ b/bulkrename
@@ -0,0 +1,42 @@
+#!/bin/bash
+# Copyright Cédric Picard 2014 -- License DWTFYW -- inspired by Ranger's
+
+set -e
+
+if [ $# -ne 0 ] ; then
+ for i in "$@" ; do printf "%s\n" "$i" ; done | "$0"
+ exit
+fi
+
+[ -x "$(which "$EDITOR" 2>/dev/null)" ] || EDITOR=vi
+
+file=/tmp/blkrn-$$
+cat > ${file}.1
+exec </dev/tty >/dev/tty
+
+cp ${file}.1 ${file}.2
+"$EDITOR" ${file}.2
+
+line_count=$(wc -l < ${file}.1)
+if [ $line_count -ne $(wc -l < ${file}.2) ] ; then
+ rm ${file}.1 ${file}.2
+ echo "Wrong number of lines" >&2
+ exit 1
+fi
+
+paste -d "\n" ${file}.1 ${file}.2 | while [ $line_count -ne 0 ] ; do
+ line_count=$(($line_count - 1))
+ read -r input ; input="$(printf "%s" "$input" | sed "s/'/'\"'\"'/")"
+ read -r output ; output="$(printf "%s" "$output" | sed "s/'/'\"'\"'/")"
+
+ if [ "$input" != "$output" ] ; then
+ echo "mv -v -- '$input' '$output'"
+ fi
+done > ${file}.sh
+
+if [ "$(stat --printf="%s" ${file}.sh)" -ne 0 ] ; then
+ "$EDITOR" ${file}.sh
+ sh ${file}.sh
+fi
+
+rm ${file}.1 ${file}.2 ${file}.sh