[net.sources] Simple tools for renaming files conveniently

gnu@hoptoad.uucp (John Gilmore) (03/09/87)

I wrote a few tools for automated renaming of files, sort of like
'rename' which was posted in <486@myrias.UUCP>, but more in the Unix
style.  Together with the Unix commands, they provide all the
commands I commonly need for file arranging, including pulling things
out of archives on non-Unix systems and making them reasonable for Unix.
They all use "mv -i" as their engine, so they are nondestructive.
I've been using them for years now.  They are public domain.

'sedname' runs 'sed' across the names of a bunch of files.  The first
argument is the sed command, the rest are the file names.  Unfortunately
dots have to be quoted since they are a sed metacharacter, but you
can't win 'em all.  E.g. to turn all foo.ftn into foo.f:

	sedname 's/\.ftn$/.f/' *.ftn

'slide' moves files with a common name prefix into a directory,
removing the prefix.  I found this very useful in organizing my
net.sources archives, which are arranged by topic.  E.g. to move the
files em.#1, em.patch, and em.shar into directory 'emacs', calling them
#1, patch, and shar:

	slide em. emacs

'lower' lowercases the file names you give it, e.g. to turn
RFC822.TXT into rfc822.txt:

	lower RFC822.TXT


: To unbundle, sh this file
echo sedname
cat >sedname <<'@@@ Fin de sedname'
#!/bin/sh
# sedname sedcmd names
# Takes file*names* and edits them with the command.  Moves the files
# to the new names.  Does *not* edit the contents of the files, just the names.
# Note, keep "sh" on separate line so "mv -i" takes stdin of sedname.
sedcmd=$1
shift
ls -d $* >/tmp/sedname$$
sed "$sedcmd" </tmp/sedname$$ >/tmp/sedname$$x
pr -m -t -l1 -s' ' /tmp/sedname$$ /tmp/sedname$$x | sed -e '/^ $/d' -e "s/ /' '/" -e "s/^/mv -i '/" -e "s/$/'/" >/tmp/sedname$$y
sh $sh /tmp/sedname$$y
rm -f /tmp/sedname$$ /tmp/sedname$$x /tmp/sedname$$y
@@@ Fin de sedname
echo slide
cat >slide <<'@@@ Fin de slide'
#!/bin/sh
# slide dir
# slide template dir
# Take files where name begins with 'template.', remove it, move them to dir. 
# If one arg, template is dir name with "." on the end.
foo=${2-${temp=${1?'Template please'}.}}
temp=${temp-$1}
dir=${2-$1}
trap  "rm -f /tmp/slid$$; exit 1" 1 2 15
#echo temp=$temp dir=$dir
ls -d ${temp}* |
	sed "s&^${temp}\(.*\)&mv -i '${temp}\1' '${dir}/\1'&" > /tmp/slid$$
sh $sh /tmp/slid$$
rm -f /tmp/slid$$
@@@ Fin de slide
echo lower
cat >lower <<'@@@ Fin de lower'
#!/bin/sh
# Move these files to lower case, same name.
for file
do
	newf=`echo "$file" | tr A-Z a-z`
	mv -i "$file" "$newf"
done
@@@ Fin de lower
exit 0
-- 
John Gilmore  {sun,ptsfa,lll-crg,ihnp4}!hoptoad!gnu   gnu@ingres.berkeley.edu
Love your country but never trust its government.
		     -- from a hand-painted road sign in central Pennsylvania