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
9
#This is a script which takes a list of directories, descends through each one
10
#and ``corrects'' filenames that:
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
17
# The GNU version of 'tr' is required. Also requires 'sed'.
19
# Script to process a given list of directories recursively
20
# and rename each file to something that is reasonable.
24
# 1. replace each space, [, ], *, ", and ' character in the name with a
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 #
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
35
# written by Kaz Kylheku <kaz@cafe.net>
39
# requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
41
# minimal conversion to bash v2 syntax done by Chet Ramey
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}."
52
echo changing \"$1\" to \"$new_name\" in `pwd`
53
mv -- "$1" "$new_name"
65
if [ "$file" != "." -a "$file" != ".." ] ; then
66
if [ -L "$file" ] ; then
67
echo "skipping symlink" $file in `pwd`
68
elif [ -d "$file" ] ; then
70
elif [ -f "$file" ] ; then
79
shopt -s nullglob dotglob
82
echo "$0: must specify a list of directories" >&2
83
echo "$0: usage: $0 directory [directory ...]" >&2
87
while [ $# != 0 ] ; do