Change a string recursively in all directories

Make sure you’re using GNU find and GNU sed.

find . -type f -exec sed -i 's/OLDSTRING/NEWSTRING/g' {} +

Tags

Related Posts

Share This

Check TCP connections and their remote origins

Useful if you feel you’re getting DDOS, flood, or other attacks:

netstat -an | grep tcp | awk '{print $5}'|sed 's/::ffff://'|cut -f1 -d':'| sort | uniq -c | sort -n -r

Tags

Related Posts

Share This

Redirect all shell output

BASH Shell Redirect Output and Errors To /dev/null

How do I redirect output and errors to /dev/null under bash / sh shell scripting? How do I redirect the output of stderr to stdout, and then redirect this combined output to /dev/null?

You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null:

$ command > /dev/null 2>&1
$ ./script.sh > /dev/null 2>&1
$ ./example.pl > /dev/null 2>&1

You can also use the same syntax for all your cronjobs to avoid emails and output / error messages:

@hourly /scripts/backup/nas.backup >/dev/null 2>&1

Tags

Related Posts

Share This

Converting the charset for an entire schema

By now, we’re all aware that a utf8 charset and collation is the “right” thing to do, with most folks opting for utf8_general_ci or utf8_unicode_ci. However, lots of installations still default to some form of latin1, which is unfortunate. There are multiple ways to do this, all which functionally do the same thing. Be aware there is a big potential gotcha that you should be aware of.

Here’s the “safe” way:

First, the schema itself:

ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;

Then…

SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," CHARACTER SET utf8 COLLATE utf8_general_ci;   ",
    "ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  ") 
    AS alter_sql
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = dbname ;

Capture this output to a file and then execute it.

If you’re feeling brave, you can do the second bit with a one-liner:

DB="dbname"; ( echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'; mysql "$DB" -e "SHOW TABLES" --batch --skip-column-names | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' ) | mysql "$DB"

Automate with expect

#!/usr/bin/expect -f
# catch the date passed on the command line and assign it to a variable
set thedate [lindex $argv 0]
# connect to remote server
spawn scp "user@server:/backups/*$thedate*" /backups_archives
#######################
expect {
-re ".*es.*o.*" {
exp_send "yesr"
exp_continue
}
-re ".*sword.*" {
exp_send "your_passwordrr"
}
}
interact

Read more about expect here: http://linux.die.net/man/1/expect

 

Tags

Related Posts

Share This

Rsync a remote directory to a local one

Basic rsyncing:

 #!/bin/bash
 rsync -avz --delete --exclude= --exclude=remotehost.com:/the/remote/directory/ /the/local/dir

 

Tags

Related Posts

Share This