Easy find and replace with perl

If you need an easy way to find and replace a string in one or more text files (php script, bash script, perl script or just text) using only a command, may be that the following is the easiest way:

find . -name ‘[PATTERN]’ |xargs perl -pi -e ‘s/[NEEDLE]/[REPLACESTR]/g’

Example: if you are in the folder where you usually store you alert script, that send email on certain events, and you want to replace the previous email with the new one you can type

find . -name ‘*.php’ |xargs perl -pi -e ‘s/[email protected]/[email protected]/g’

DONE!

Error: Argument list too long

Using linux shell commands such as cp, mv, rm you could encounter the following error message:

/bin/cp: Argument list too long.

Obviously you can modify system settings and increase the max number of files, but an easyer solution is using the find command.

Example: to remove a great number of temporary files in a folder, all with the ‘.dieg’ extension , try this (or something similar!):

find ./ -name ‘*.dieg’ -exec rm -r -v {} \;

BASH: bad interpreter: No such file or directory

If you are getting this error message from a linux shell

bad interpreter: No such file or directory

over and over again, may be a “path” variable setting mistake, that is the correct interpreter (e.g. bash) can’t be found and you have to specify the full path. Else the script file has been witten on a win32 machine, so the “end of line” character is ‘\r\n‘ rather than ‘\n‘.

If this last case, you can solve the problem editing the script file with “vim” and digiting:

:set fileformat=unix

and finally to save:

WGET example, (for Windows too!)

wget example:

wget -F -c -w 1 -r -L -k -l 1 http://www.example.com/index2.html

Options meaning:
-F This enables you to retrieve relative links from existing HTML files on your local disk, by adding <base href=”url”> to HTML, or using the –base command-line option
-c continue getting a partially-downloaded file, otherwise, if the destination file already exists, the new downloaded file will be renamed [FILENAME.EXT].1
-w XX wait the specified (XX) number of seconds between the retrievals. To prevent servers overload
-r recurse folder
-L follow relative links only
-k after the download is complete, convert the links in the document to make them suitable for local viewing
-l specify recursion maximum depth level depth. The default maximum depth is 5

You may want to use -b to execute the command in backgrount mode.

Windows version: http://www.interlog.com/%7Etcharron/wgetwin.html
Manual: wget –help

Esempio di utilizzo WGET, (anche per Windows!)

Esempio di wget:

wget -F -c -w 1 -r -L -k -l 1 http://www.esempio.it/index2.html

vediamo il significato dei parametri:
-F forza il recupero dei link relativi dal file (considera il file come HTML)
-c continua il download di file parziali, altrimenti in caso il file da salvare esista già rinomina il nuovo file [FILENAME.EXT].1
-w XX fa una pausa di XX secondi tra un file e l’altro
-r recurse folder
-L segue solo i link relativi
-k converte i link nelle pagine scaricate per rendere il sito navigabile in locale
-l livello di profondità in cui seguire i link, di default 5

può essere utile utilizzare -b per mandare l’esecuzione in background

Se si preferisce la versione per windows: http://www.interlog.com/%7Etcharron/wgetwin.html
Per il manuale: wget --help

Script per conservare gli n file più recenti in una dir

Mi è capitato di voler tenere traccia di operazioni effettuate da script con dei file di log. Se si chiama il file di log con un nome tipo anno-mese-giorno (es:script1_AAAAMMGG.log) il numero di log cresce continuamente.

Con questo script che lancio periodicamente, vengono conservati i solo gli ultimi n file più nuovi.

Utilizzo:
keepnewestonly.sh [directory to check] [opt:maxnum] [opt:filepattern] [opt:T (testonly)]

dove

  • il primo parametro è la directory in cui effettuare la pulizia
  • il secondo (opzionale, default =20) è il numero di file che si vogliono conservare