Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GNU Bash Script to fix filenames

13 views
Skip to first unread message

Kazimir Kylheku

unread,
Mar 28, 1996, 3:00:00 AM3/28/96
to
This is a script which takes a list of directories, descends through each one
and ``corrects'' filenames that:

- contain filename globbing characters: * ? [ ]
- quote characters: ' "
- control characters: 0-31 (127 is not dealt with---oops)
- - or + as the first character

This requires the GNU Bourne-Again Shell (bash). The GNU version of 'tr' is
required. Also requires 'sed'.


#!/bin/bash
#
# Script to process a given list of directories recursively
# and rename each file to something that is reasonable.
#
# The rules are:
#
# 1. replace each space, [, ], *, ", and ' character in the name with a
# period.
# 2. replace each control character 1..31 with a printable character obtained
# by adding 64 to the ascii value. ^A becomes A, ^B becomes B and so on.
# 3. replace a - or + occuring at the beginning of the name with a #
#
# 4. if the resulting name has been changed in any way, then
# 5. if a file of the new name already exists, then
# 6. add a . to the new name and goto step 5.
# 7. rename the old name to the new name
#
#
# written by Kaz Kylheku <k...@cafe.net>
# March 1996
# Vancouver, Canada
#
# requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
#

function processfile()

{
new_name="`echo -n $1 | tr '\173\175\052\077\042\047 ' '.......' |
tr '[\000-\037]' '[\100-\137]' |
sed -e 's/^-/#/' -e 's/+/#/'`"
if [ "$new_name" != "$1" ] ; then
while [ -e "$new_name" ] ; do
new_name="${new_name}."
done
echo changing \"$1\" to \"$new_name\" in `pwd`
mv -- "$1" "$new_name"
fi
}

function processdir()

{
set -f
local savepwd="$(pwd)"
if cd "$1" ; then
set +f
for file in * ; do
set -f
if [ "$file" != "." -a "$file" != ".." ] ; then
if [ -L "$file" ] ; then
echo "skipping symlink" $file in `pwd`
elif [ -d "$file" ] ; then
processdir "$file"
elif [ -f "$file" ] ; then
processfile "$file"
fi
fi
done
cd "$savepwd"
fi
}

allow_null_glob_expansion=1
glob_dot_filenames=1

if [ $# = 0 ] ; then
echo "$0: specify a list of directories"
fi

while [ $# != 0 ] ; do
processdir "$1"
shift
done

--


0 new messages