Browse Source

Add file_utils/prefixsubdirs.sh

master
Matteo Cypriani 1 year ago
parent
commit
b08187c113
3 changed files with 75 additions and 0 deletions
  1. 1
    0
      bin/prefixsubdirs
  2. 22
    0
      file_utils/README.md
  3. 52
    0
      file_utils/prefixsubdirs.sh

+ 1
- 0
bin/prefixsubdirs View File

@@ -0,0 +1 @@
../file_utils/prefixsubdirs.sh

+ 22
- 0
file_utils/README.md View File

@@ -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
=======


+ 52
- 0
file_utils/prefixsubdirs.sh View File

@@ -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

Loading…
Cancel
Save