bash

crontab -e: I can't use the default editor!

|

Usually we use crontab -e command, while logged in as a user, to modify the user's cron jobs or crontab -e -u[user] as root.

The editor used fot this depends on the system variable VISUAL or EDITOR (if present) else the default editor will be used /usr/bin/editor.

So if the default editor is a not well known program (take "VI" as an example, even if you use the more user friendly "VIM"...), you can modify the suggested system variables if present, to verify:

set | grep -iE '(visual)|(editor)'

grep, cut, awk, sort, uniq... linux shell power!

|

How powerful can be linux shell? This is just an other nice example of the power of linux shell!

Well, this is the game: suppose to need on a remote machine to count how many times an ip address (or user or whatever you want) is used in a log file or a part of it.

Let's take a sample log: Apache access log.

The format is something like (see attached example log file):
1.1.1.1 - - [16/Jan/2008:22:18:42 +0100] "GET /dir/pag.htm" 200 11 "http://diegobelotti.com/a.php" "Mozilla 5 (Windows..) Firefox"

Keep the time and date of your Linux box always up to date

|

The attached file is a simple bash script, to correct the date and time of your linux box (binaries paths are relative to a Debian Etch).

Prerequisites

  • ntpdate: apt-get install ntpdate
  • ntpd stopped: if you have a ntp daemon running you don't need this script
  • port 123 (UDP) opened
  • user permission: the user that run the script must have the permission to set up the clock

What the script does

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/oldemail@domain.tld/newemail@domain.tld/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 {} \;

Syndicate content