From b08187c1130edb56ab9a7e7e6f8f9ed4f9b627bb Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 11 Apr 2018 20:55:01 +0200 Subject: [PATCH] Add file_utils/prefixsubdirs.sh --- bin/prefixsubdirs | 1 + file_utils/README.md | 22 ++++++++++++++++ file_utils/prefixsubdirs.sh | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 120000 bin/prefixsubdirs create mode 100755 file_utils/prefixsubdirs.sh diff --git a/bin/prefixsubdirs b/bin/prefixsubdirs new file mode 120000 index 0000000..1f883f8 --- /dev/null +++ b/bin/prefixsubdirs @@ -0,0 +1 @@ +../file_utils/prefixsubdirs.sh \ No newline at end of file diff --git a/file_utils/README.md b/file_utils/README.md index 777a932..88ed08d 100644 --- a/file_utils/README.md +++ b/file_utils/README.md @@ -49,6 +49,28 @@ If you want to integrate it in ROX-Filer: cp mvparent.sh ~/.config/rox.sourceforge.net/SendTo/.inode_directory/"Move here" +prefixsubdirs.sh +================ + +The use case for this script is to flatten a file hierarchy, for instance +switching from: + + dir1 + |-- dirA + |-- dirB + `-- dirC + +to: + + dir1 ; dirA + dir1 ; dirB + dir1 ; dirC + +However, `prefixsubdirs.sh` will only rename the subdirectories (not the +regular files) within the given directories, it will not actually flatten the +hierarchy. You can use `mvparent.sh` for this purpose (see above). + + unln.py ======= diff --git a/file_utils/prefixsubdirs.sh b/file_utils/prefixsubdirs.sh new file mode 100755 index 0000000..9c296c4 --- /dev/null +++ b/file_utils/prefixsubdirs.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# +# prefixsubdirs.sh, Copyright © 2018 Matteo Cypriani +# +# This program is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. +# +# For each directory provided on the command-line, this script prefixes its +# subdirectorie(s) with its name and a separator (" ; " by default). + +set -e +#set -x + +SEPARATOR=' ; ' +MVOPTS='-v' + +prefix_subdirs() +( + dir="$1" + cd "$dir" + + files="$(echo -- *)" + [ "$files" = '*' ] && return 0 + + for file in * ; do + [ -d "$file" ] || continue + mv $MVOPTS "$file" "${dir}${SEPARATOR}${file}" + done +) + +if [ $# -lt 1 ] ; then + echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2 + exit 1 +fi + +while [ -n "$1" ] ; do + dir=$1 + shift + + if [ ! -d "$dir" ] ; then + printf 'Warning! "%s" is not a directory: ignoring.\n' "$dir" >&2 + continue + fi + + printf 'Entering "%s" ...\n' "$dir" + prefix_subdirs "$dir" +done + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4