Browse Source

Add file_utils/prefixsubdirs.sh

Matteo Cypriani 9 months 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 @@
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:
49 49
     cp mvparent.sh ~/.config/rox.sourceforge.net/SendTo/.inode_directory/"Move here"
50 50
 
51 51
 
52
+prefixsubdirs.sh
53
+================
54
+
55
+The use case for this script is to flatten a file hierarchy, for instance
56
+switching from:
57
+
58
+    dir1
59
+    |-- dirA
60
+    |-- dirB
61
+    `-- dirC
62
+
63
+to:
64
+
65
+    dir1 ; dirA
66
+    dir1 ; dirB
67
+    dir1 ; dirC
68
+
69
+However, `prefixsubdirs.sh` will only rename the subdirectories (not the
70
+regular files) within the given directories, it will not actually flatten the
71
+hierarchy. You can use `mvparent.sh` for this purpose (see above).
72
+
73
+
52 74
 unln.py
53 75
 =======
54 76
 

+ 52
- 0
file_utils/prefixsubdirs.sh View File

@@ -0,0 +1,52 @@
1
+#!/bin/sh
2
+#
3
+# prefixsubdirs.sh, Copyright © 2018 Matteo Cypriani
4
+#
5
+# This program is free software. It comes without any warranty, to
6
+# the extent permitted by applicable law. You can redistribute it
7
+# and/or modify it under the terms of the Do What The Fuck You Want
8
+# To Public License, Version 2, as published by Sam Hocevar. See
9
+# http://sam.zoy.org/wtfpl/COPYING for more details.
10
+#
11
+# For each directory provided on the command-line, this script prefixes its
12
+# subdirectorie(s) with its name and a separator (" ; " by default).
13
+
14
+set -e
15
+#set -x
16
+
17
+SEPARATOR=' ; '
18
+MVOPTS='-v'
19
+
20
+prefix_subdirs()
21
+(
22
+    dir="$1"
23
+    cd "$dir"
24
+
25
+    files="$(echo -- *)"
26
+    [ "$files" = '*' ] && return 0
27
+
28
+    for file in * ; do
29
+        [ -d "$file" ] || continue
30
+        mv $MVOPTS "$file" "${dir}${SEPARATOR}${file}"
31
+    done
32
+)
33
+
34
+if [ $# -lt 1 ] ; then
35
+    echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2
36
+    exit 1
37
+fi
38
+
39
+while [ -n "$1" ] ; do
40
+    dir=$1
41
+    shift
42
+
43
+    if [ ! -d "$dir" ] ; then
44
+        printf 'Warning! "%s" is not a directory: ignoring.\n' "$dir" >&2
45
+        continue
46
+    fi
47
+
48
+    printf 'Entering "%s" ...\n' "$dir"
49
+    prefix_subdirs "$dir"
50
+done
51
+
52
+# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

Loading…
Cancel
Save