Home > Linux > My Scripts

 

 

Here are scripts I created or adapted from things I've found. If they work fine for me they are likely not bug free... So you should test this stuff carefully before any important use. In any case the use is at your own risks !

 

 

 


Unix/Linux Shell Scripts


 

  

 Recursive Search & Replace

 

I looked for a long time such script. It is very useful to replace quickly a string by another in all files of a whole directory tree. To be used carefully ! The interesting parts of code used here that can be extracted and reused are :

   - Function ConfirmOrExit()

   - Parameters with default values

   - Find, Sed and Xargs use

 

#!/bin/bash

clear

 

echo

echo "################################################################"

echo "#                                                              #"

echo "#    RECURSIVE SEARCH & REPLACE                                #"

echo "#    By Thomas Guilleminot - www.guilleminot.org               #"

echo "#                                                              #"

echo "################################################################"

 

## FUNCTION DECLARATION ##

 

function ConfirmOrExit() {

while true

do

  echo

  echo -n "Please confirm (y or n) : "

  read CONFIRM

  case $CONFIRM in

    y|Y|YES|yes|Yes) break ;;

    n|N|no|NO|No)

      echo

      echo Aborting as you entered $CONFIRM

      echo

      exit

      ;;

    *) echo Please enter only y or n

  esac

done

echo You entered $CONFIRM.  Continuing...

}

 

## PARAMETERS PASSED - IF NOT SET ASSIGNING DEFAULT VALUES ##

 

test -n "$1" && TARGET_DIRECTORY=$1

test -n "$2" && FILE_EXTENSION=$2

test -n "$3" && STRING_SEARCH=$3

test -n "$4" && STRING_REPLACE=$4

 

echo

echo "PARAMETERS :"

echo "------------"

echo "TARGET_DIRECTORY : " ${TARGET_DIRECTORY="/mydirectory/"};

echo "FILE_EXTENSION   : " ${FILE_EXTENSION="*.txt"};

echo "STRING_SEARCH    : " ${STRING_SEARCH="the old string"};

echo "STRING_REPLACE   : " ${STRING_REPLACE='the new string'};

 

## EXECUTION ##

 

echo

echo "STEP 1 : SEARCHING FILES..."

echo "---------------------------"

echo

 

find $TARGET_DIRECTORY -name $FILE_EXTENSION | sed 's/ /\\ /g' | xargs grep "$STRING_SEARCH" /dev/null

ConfirmOrExit

 

echo

echo "STEP 2 : REPLACING STRING..."

echo "----------------------------"

echo

 

find $TARGET_DIRECTORY -name $FILE_EXTENSION | sed 's/ /\\ /g' | xargs sed -i -e s/"$STRING_SEARCH"/"$STRING_REPLACE"/

echo "Done."

 

echo

echo "STEP 3 : CHECKING FILES..."

echo "--------------------------"

echo

 

find $TARGET_DIRECTORY -name $FILE_EXTENSION | sed 's/ /\\ /g' | xargs grep "$STRING_SEARCH" /dev/null

echo "Done. Exiting..."

echo

echo

 

 

 DVD to XVID Encoding with Mencoder

 

Soon...

 

 

 

 

 DVD to XVID Encoding with Transcode

 

Soon...

 

 

 

 Easier Mplex Mixing (Audio with Video)

 

Soon...

 

 

 

 

 

 


Guilleminot.org - Copyright © 2002/2008
All Rights Reserved