From 3525ade66ff9e4cd6dd325d0dd099695d33eb2c0 Mon Sep 17 00:00:00 2001 From: "Ivan K. Boikov" Date: Sat, 25 Jan 2025 15:51:56 +0100 Subject: New flattening script --- scripts/.local/bin/flatdir-do | 2 -- scripts/.local/bin/flatdir-try | 2 -- scripts/.local/bin/flatten | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) delete mode 100755 scripts/.local/bin/flatdir-do delete mode 100755 scripts/.local/bin/flatdir-try create mode 100755 scripts/.local/bin/flatten diff --git a/scripts/.local/bin/flatdir-do b/scripts/.local/bin/flatdir-do deleted file mode 100755 index 23bfbf0..0000000 --- a/scripts/.local/bin/flatdir-do +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -find */ -type f -exec bash -c 'file=${1#./}; mv "$file" "${file//\//_}"' _ '{}' \; diff --git a/scripts/.local/bin/flatdir-try b/scripts/.local/bin/flatdir-try deleted file mode 100755 index 06cdbd7..0000000 --- a/scripts/.local/bin/flatdir-try +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -find */ -type f -exec bash -c 'file=${1#./}; echo mv "$file" "${file//\//_}"' _ '{}' \; diff --git a/scripts/.local/bin/flatten b/scripts/.local/bin/flatten new file mode 100755 index 0000000..a4b833f --- /dev/null +++ b/scripts/.local/bin/flatten @@ -0,0 +1,38 @@ +#!/bin/sh + +# flatten the directory tree +# example: dir1/dir2/file1 -> dir1_dir2_file1 + +DIR="${1:-.}" + +_flatten(){ + cmd="$1" + find "$DIR" -mindepth 2 -type f -exec bash -c 'file=${1#./}; '"$cmd"' "$file" "${file//\//_}"' _ '{}' \; +} + +toflat=$(_flatten "echo mv") +if [ -z "$toflat" ]; then + echo "Abort: nothing to do"; exit +fi + +echo "Flattening will do:" +echo "$toflat" +printf "Proceed? [y/N] " +read -r yn +case $yn in + [Yy]* ) ;; + * ) echo "Abort" ; exit;; +esac +_flatten "mv" + + +todel="$(find . -type d -empty -print)" +[ -z "$todel" ] && exit +echo "Empty subdirectories:" +echo "$todel" +printf "Remove subdirectories? [y/N]" +read -r yn +case $yn in + [Yy]* ) find . -type d -empty -delete;; + * ) echo "Abort" ; exit;; +esac -- cgit v1.2.3