Linux General Scripts

From Wickle Wiki

Contents

Maintaining versions of system files with RCS

To help the life of a system admin, i make a scripts to maintain versions on files of configuration, its easy and i use it instead vim , its a simple script that commits after editing and before editing locks he file :


#!/bin/bash
# Needs an argument
if [ $# -eq 0 ] ; then
  # no argument given, print usage
  echo "start control version on configuration file";
  echo "use: $0 file";
else
  #Getting some variables
  FULLPATH=`dirname $1`;
  BASE=`basename $1`;
  # check if RCS directory exist or create it
  if [ -d $FULLPATH/RCS ] ; then
    echo "revisions directory existent ...";
  else
    mkdir $FULLPATH/RCS
  fi

  # Things to do when a RCS file exist
  if [ -e $FULLPATH/RCS/$BASE,v ] ; then
    echo "File exist. Checking for differences ..."
    # Check if there is any differences
    if ! rcsdiff $1; then
      echo "==================================="
      echo "WARNING: changes have been made to $1 and are not in the repository. Carefully look at the diff above, comment them below for committing or CTRL+C to abort."
      # Lock file
      /usr/bin/rcs -l $1
      if ! /usr/bin/ci -u $1; then
        echo "Error when commiting. Aborting"
        exit
      fi
    fi
    echo "==================================="
    echo "Getting lastest revision and locking it ..."
    /usr/bin/co -l $1
  fi

  echo "Launching editor ..."
  vim $1
  # changes made, commit them to the repository unlocked
  /usr/bin/ci -u $1
  exit
fi

Them to bring to an old version (for example we need to retrieve 1.2 version) we use :

:~$ rlog file (to see the available versions)
:~$ rdiff -r1.1 file (view differences between revision 1.1 and actual of file)
:~$ rcsdiff -r1.1 file (work with rcs package installed)
:~$ co -r1.2 file (retrieve 1.2 version)
:~$ co -p -r1.2 file (ONLY VIEW the version 1.2, only retrieve to default output)

Note: this script need RCS installed on the system (that usually comes on all systems).

Renaming a lot of files at the same time from Bash

Something configuration files comes with a name like file.cfg-dist. and on occasions there are a lot of this configurations files. To change the name of all this files at the same time to another files without the '-dist' we only need to execute the next line on a BASH shell:

cd /directorio/donde/estan/los/archivosdeconfiguracion
for i in *; do cp $i ${i%.*}.cfg ; done

Checking consistency of Hard Disk on Linux after crashs

Type CONTROL-D to proceed with normal startup,
(or give root password for system maintenance):

When this appear, enter the root password and at the prompt check the consistency at hand on all the partitions of harddisk (you can view all partitions with:

fdisk -l /dev/hda (substitute a for the letter of your hard disk)

them you must do for each partition :

fsck.ext2 /dev/hdax (where x goes from 1 to all the linux paritions)


Remove a message Bounced on postfix

with mailq we can know the ID of the mail :

5F3D61003A4    51751 Thu Sep 16 16:53:28  MAILER-DAEMON
                (connect to 192.168.0.15[192.168.0.15]: Connection timed out)
                         vic@localdomain.com

to delete it :

#postfix stop
#find /var/spool/postfix -name 5F3D61003A4 -print | xargs rm
#postfix start


Setting DISPLAY from a remote ssh session


#!/bin/bash
if [ -z ${DISPLAY:=""} ]; then
    DISPLAY=$(who am i)
    DISPLAY=${DISPLAY%%\!*}
    if [ -n "$DISPLAY" ]; then
        export DISPLAY=$DISPLAY:0.0
    else
        export DISPLAY=":0.0"  # fallback
    fi
fi

Copying all jars in a structure of directories to another one

if you have : ./apache/jar/apache.jar

./log4j/jar/log4j.jar

./lucene/jar/lucene.jar

./dom4j/jar/dom4j.jar

./alljars

this script copy all .jars in all subdirectories to another one (the last ./alljars):

for i in $(find . -name "*.jar") ; do cp $i alljars/ ; done

Comparing all files in actual directory to another one

If you want to compare all files in actual directory to the mirror of this directory (for example the working directory to the published directory) just do:


for i in * ; do diff $i /var/web/site/pages/$i ; done

Comparing all files in this directory to another one RECURSIVELY

If you want to compare all files from actual tree to another tree, except the ones related to svn information:


cd ~/workspace/web_backup
for i in $(find . -name "*.jsp" -type f ! -name "*svn*") ; do diff $i ~/workspace/web/$i ; done
  • type f: is to compare only "regular" files

Knowing if a device is more that 99% full

This is a script i use to automate the check of free space on devices, i launch this from a crontab ecery day if the test pass, them i make a backup on that device, if the check fails i dont make a backup (because i dont have free space) and i sent an email to the admin (me on that case ;) )

PORCENTAJE_LIBRE=`df -h | grep opt | awk '{ print $5}' | cut -d'%' -f1`
echo $PORCENTAJE_LIBRE
if [ $PORCENTAJE_LIBRE -ge "99" ] ; then
        echo "El servidor supercoff tiene menos del 99% de espacio libre. NO SE REALIZA BACKUP DEL CVS" | mailx -s "ESPACIO EN DISCO DURO INSUFICIENTE PARA REALIAR EL BACKUP" youraccount@isp.com
        exit
else
        #echo "OK"
fi

Deleting all files like __ShowDetails.exe from Alfresco with find

On Alfresco are a lot of Garbage files (to be honest these files has a lot of utility, but on specified environments we can avoid them)

To delete all files starting "__" on a tree of files you must execute this:

$ find . -name "__*" -exec rm -f {} \;
Personal tools
This site is powered by Queres Tecnologias