1 | #! /bin/bash
|
---|
2 | #
|
---|
3 | # From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
|
---|
4 | # Newsgroups: comp.unix.shell,comp.os.linux.misc
|
---|
5 | # Subject: GNU Bash Script to fix filenames
|
---|
6 | # Date: 28 Mar 1996 14:54:43 -0800
|
---|
7 | # Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
|
---|
8 | #
|
---|
9 | #This is a script which takes a list of directories, descends through each one
|
---|
10 | #and ``corrects'' filenames that:
|
---|
11 | #
|
---|
12 | # - contain filename globbing characters: * ? [ ]
|
---|
13 | # - quote characters: ' "
|
---|
14 | # - control characters: 0-31 (127 is not dealt with---oops)
|
---|
15 | # - - or + as the first character
|
---|
16 | #
|
---|
17 | # The GNU version of 'tr' is required. Also requires 'sed'.
|
---|
18 | #
|
---|
19 | # Script to process a given list of directories recursively
|
---|
20 | # and rename each file to something that is reasonable.
|
---|
21 | #
|
---|
22 | # The rules are:
|
---|
23 | #
|
---|
24 | # 1. replace each space, [, ], *, ", and ' character in the name with a
|
---|
25 | # period.
|
---|
26 | # 2. replace each control character 1..31 with a printable character obtained
|
---|
27 | # by adding 64 to the ascii value. ^A becomes A, ^B becomes B and so on.
|
---|
28 | # 3. replace a - or + occuring at the beginning of the name with a #
|
---|
29 | #
|
---|
30 | # 4. if the resulting name has been changed in any way, then
|
---|
31 | # 5. if a file of the new name already exists, then
|
---|
32 | # 6. add a . to the new name and goto step 5.
|
---|
33 | # 7. rename the old name to the new name
|
---|
34 | #
|
---|
35 | # written by Kaz Kylheku <kaz@cafe.net>
|
---|
36 | # March 1996
|
---|
37 | # Vancouver, Canada
|
---|
38 | #
|
---|
39 | # requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
|
---|
40 | #
|
---|
41 | # minimal conversion to bash v2 syntax done by Chet Ramey
|
---|
42 |
|
---|
43 | processfile()
|
---|
44 | {
|
---|
45 | new_name="`echo -n $1 | tr '\173\175\052\077\042\047 ' '.......' |
|
---|
46 | tr '[\000-\037]' '[\100-\137]' |
|
---|
47 | sed -e 's/^-/#/' -e 's/+/#/'`"
|
---|
48 | if [ "$new_name" != "$1" ] ; then
|
---|
49 | while [ -e "$new_name" ] ; do
|
---|
50 | new_name="${new_name}."
|
---|
51 | done
|
---|
52 | echo changing \"$1\" to \"$new_name\" in `pwd`
|
---|
53 | mv -- "$1" "$new_name"
|
---|
54 | fi
|
---|
55 | }
|
---|
56 |
|
---|
57 | processdir()
|
---|
58 | {
|
---|
59 | set -f
|
---|
60 | local savepwd="$PWD"
|
---|
61 | if cd "$1" ; then
|
---|
62 | set +f
|
---|
63 | for file in * ; do
|
---|
64 | set -f
|
---|
65 | if [ "$file" != "." ] && [ "$file" != ".." ] ; then
|
---|
66 | if [ -L "$file" ] ; then
|
---|
67 | echo "skipping symlink" $file in `pwd`
|
---|
68 | elif [ -d "$file" ] ; then
|
---|
69 | processdir "$file"
|
---|
70 | elif [ -f "$file" ] ; then
|
---|
71 | processfile "$file"
|
---|
72 | fi
|
---|
73 | fi
|
---|
74 | done
|
---|
75 | cd "$savepwd"
|
---|
76 | fi
|
---|
77 | }
|
---|
78 |
|
---|
79 | shopt -s nullglob dotglob
|
---|
80 |
|
---|
81 | if [ $# = 0 ] ; then
|
---|
82 | echo "$0: must specify a list of directories" >&2
|
---|
83 | echo "$0: usage: $0 directory [directory ...]" >&2
|
---|
84 | exit 2
|
---|
85 | fi
|
---|
86 |
|
---|
87 | while [ $# != 0 ] ; do
|
---|
88 | processdir "$1"
|
---|
89 | shift
|
---|
90 | done
|
---|
91 |
|
---|
92 | exit 0
|
---|