Sunday, August 31, 2008

To rename a file using inode value

When a file/directory is saved with special character other than underscore('_') then it is difficult to remove/rename it. Inorder to remove that file/directory, first rename to a valid filename.

Procedure:
Goto directory where the file/directory is present.
Run this command:
ls -iltr
copy the inode value associated with that file/directory which you want to rename/remove and put that number in the following command in place of XXXXX and replace newname with your desired filename.

find . -inum XXXXX -exec mv {} newname \;

Wednesday, August 20, 2008

Shell script to find and replace strings

#!/bin/sh
#Script Find a string and replaces it with another string
if [ $# -eq 0 ]
then
echo
echo "Usage: find_replace.sh find_str replace_str ..."
echo
exit 1
fi
for files in $*; do
if [ "$files" = "$1" -o "$files" = "$2" ]
then
a=1
else
mv $files $files.old
sed 's/'$1'/'$2'/g' $files.old > $files
fi
done
rm *.old 2>/dev/null

Thursday, August 7, 2008

Script to replace ^M characters in unix

#!/bin/sh
if [ $# -eq 0 ]
then
echo
echo "This script will replace the ^M line breaks from dos"
echo "Usage replace "
echo
exit 1
fi
tr -s "\r" "\n" < $1 >$1.tmp
rm $1
mv $1.tmp $1
chmod u+x $1


Note: Don't forget to remove ^M characters from this script first