Path Case Script

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
(What is this?)
m (Reverted edits by 217.108.237.125 (Talk); changed back to last version by 72.147.156.223)
 
(7 intermediate revisions by 3 users not shown)
Line 11: Line 11:
 
* upper|lower - either upper or lower here determines which way the case will be converted (default is upper)
 
* upper|lower - either upper or lower here determines which way the case will be converted (default is upper)
 
* verbose - if this argument exists will display to terminal what it is moving for verification
 
* verbose - if this argument exists will display to terminal what it is moving for verification
 +
 +
It is based on another script found earlier, but added arguments, checks for proper paths and if renaming is needed, verbose mode, and restricted the case conversion to only that part of the path after what is specified on the command line. ;;
 +
 +
 +
*todo: Hmmz, a thought, since foreach sometimes delimits on " " rather than exclusively "\n", should either set the delimiter enviroment var or use the execute mechanism built into the find command? @_@
  
 
==Shell Source Code==
 
==Shell Source Code==
Line 44: Line 49:
 
repath=`echo $path | sed -e 's/\//\\\\\//g'`
 
repath=`echo $path | sed -e 's/\//\\\\\//g'`
  
echo "Do the directories first, so that the path doesn't change in $path ..."
+
echo "changing all dir/files in $path"
 
for each in `find $path -type d`
 
for each in `find $path -type d`
 
do
 
do
Line 50: Line 55:
 
newname=`echo $justend | tr $translateString`
 
newname=`echo $justend | tr $translateString`
 
if [ "$justend" != "$newname" ]; then
 
if [ "$justend" != "$newname" ]; then
mv $each $path$newname
+
mv "$each" "$path$newname"
 
if [ "$verbose" != "" ]; then
 
if [ "$verbose" != "" ]; then
 
echo mv $each $path$newname
 
echo mv $each $path$newname
Line 56: Line 61:
 
fi
 
fi
 
done
 
done
echo "Now to the files in $path ..."
+
 
 
for eachf in `find $path -type f`
 
for eachf in `find $path -type f`
 
do
 
do
Line 62: Line 67:
 
newnamef=`echo $justend | tr $translateString`
 
newnamef=`echo $justend | tr $translateString`
 
if [ "$justend" != "$newnamef" ]; then
 
if [ "$justend" != "$newnamef" ]; then
mv $eachf $path$newnamef
+
mv "$eachf" "$path$newnamef"
 
if [ "$verbose" != "" ]; then
 
if [ "$verbose" != "" ]; then
 
echo mv $eachf $path$newnamef
 
echo mv $eachf $path$newnamef
Line 70: Line 75:
  
 
</source>
 
</source>
 +
 +
==Disclaimer!==
 +
'''Disclaimer!!! I'm not responsible for any mishaps this script may result in! It works for me but please, use with caution.'''

Latest revision as of 14:52, 7 November 2011

Script to change the case of all files and directories under a given path

Contents

What is this?

Quickie shell script to convert the case of all files within a given path to either uppercase or lowercase. This is often required when moving files between windows and unix style operating systems.

The scripts options are as follows:

usage ./pathChangeCase.sh PATH/TO/CHANGE/CASE [upper|lower] [verbose]
  • PATH/TO/CHANGE/CASE - the path you wish to change
  • upper|lower - either upper or lower here determines which way the case will be converted (default is upper)
  • verbose - if this argument exists will display to terminal what it is moving for verification

It is based on another script found earlier, but added arguments, checks for proper paths and if renaming is needed, verbose mode, and restricted the case conversion to only that part of the path after what is specified on the command line. ;;


  • todo: Hmmz, a thought, since foreach sometimes delimits on " " rather than exclusively "\n", should either set the delimiter enviroment var or use the execute mechanism built into the find command? @_@

Shell Source Code

Copy and paste this to a file, chmod +x filename, and enjoys!

#!/bin/sh
 
path=$1;
direction=$2;
verbose=$3;
 
if [ "$path" = "" ]; then
	echo "usage $0 PATH/TO/CHANGE/CASE [upper|lower] [verbose]"
	echo
	echo "will change all files in path to either upper or lower case"
	exit
fi
 
if [ -e $path ]; then
	echo
else
	echo "$path is invalid?!"
	exit;
fi
 
if [ "$direction" = "lower" ]; then
	translateString="[A-Z] [a-z]"
else
	translateString="[a-z] [A-Z]"
fi
 
#make a regexp friendly path
repath=`echo $path | sed -e 's/\//\\\\\//g'`
 
echo "changing all dir/files in $path"
for each in `find $path -type d`
do
justend=`echo $each | sed s/^$repath//`
newname=`echo $justend | tr $translateString`
if [ "$justend" != "$newname" ]; then
	mv "$each" "$path$newname"
	if [ "$verbose" != "" ]; then
		echo mv $each $path$newname
	fi
fi
done
 
for eachf in `find $path -type f`
do
justend=`echo $eachf | sed s/^$repath//`
newnamef=`echo $justend | tr $translateString`
if [ "$justend" != "$newnamef" ]; then
	mv "$eachf" "$path$newnamef"
	if [ "$verbose" != "" ]; then
		echo mv $eachf $path$newnamef
	fi
fi
done

Disclaimer!

Disclaimer!!! I'm not responsible for any mishaps this script may result in! It works for me but please, use with caution.

Personal tools
irssi scripts
eggdrop scripts