Monday, March 28, 2011

vim find replace regex

Try to remove \n before the last "
Update:
"B160102","4-Nitropicolinaldehyde","108338-19-8
"
To:
"B160102","4-Nitropicolinaldehyde","108338-19-8"

:s%/-[0-9]\zs\n"\ze

\zs and \ze to set the start and end of a pattern.

mysql change root password

$ mysqladmin -u root password NEWPASSWORD

How to find the total number of files in a folder

root@209:/temp# ls /var/htdocs/temp | wc
19767 19767 212692

Friday, March 25, 2011

Disabling root SSH logins.

CentOS:

1,
Edit the /etc/ssh/sshd_config file and set the PermitRootLogin parameter to no.

[root@sor ~]# vim /etc/ssh/sshd_config
PermitRootLogin no


2, restart ssh deamon
[root@sor ~]# /etc/init.d/sshd restart

Wednesday, March 16, 2011

Using vim as a hex editor - replace windows chars

For example: <99> <B0>
:%s/\%x99//g
:%s/\%xB0//g



Switch to hex mode
Open a file in vim, hit escape and type:
:%!xxd

to exit from hex mode
hit escape again and type:
:%!xxd -r

Search Hex using vim
\%xff or [\xff]

Search 44
\%x34\%x34 or [\x34][\x34]


LF:    Line Feed, U+000A
 CR:    Carriage Return, U+000D 

Run MySQL SQL queries on Linux command line

This is a shell script

#!/bin/bash

deletelist="delete from capture_files where date(updated_on) < DATE_SUB(CURdate(), INTERVAL 30 DAY);"

mysql -u dbuser -pdbpasswd amp -e "$deletelist"

Tuesday, March 15, 2011

Argument list too long - rm files failed

Use find and rm to delete files:

find /var/mol/temp/ -name *.txt -exec rm -rf {} \;

Enable Remote Access To MySQL Server

1, update my.cnf to make it liston to ip address, not localhost

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
bind-address = 6.31.83.19

2, Save the file and Restart the mysql server, enter:
# /etc/init.d/mysql restart

3, Update your mysql database user privileges
A: If you want to update existed account, you may do:
mysql> update user set Host='%' where user='greg';
mysql> flush privileges ;

Host='%' means allow user greg access this mysql server from any IP address.
If you want a specified IP, you can do
mysql> use mysql
mysql> update user set Host='6.6.6.6' where user='greg';
mysql> flush privileges ;

B, Or you can use grant
mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO greg@'6.6.6.6' IDENTIFIED BY 'PASSWORD';
mysql> flush privileges ;

You don't need to restart mysql for step 3

Monday, March 14, 2011

linux remove the first line of a file

Two ways:
1, generate new file
sed 1d delete_old_capture_file.dump > delete_old_capture_file.list

2, update same file
sed -i 1d /tmp/delete_old_capture_file.dump

Friday, March 11, 2011

linux read line one by one from a file

[root@localhost ~]# cat delete_old_capture_file.sh
#!/bin/bash

while read line
do
echo "Delete $line" >> /var/log/delete_old_captured_file.log
find /opt/ntr/file_capture/ -name $line -exec rm -rf {} \;
done < /tmp/file_capture_delete.list

Save MySQL query results into a text file

There are 2 ways to do it.
1, save the result to a file in the sql query directly
select file_name
from files
limit 3
into OUTFILE '/tmp/file_capture_delete.list';


[root@localhost file_capture]# cat /tmp/file_capture_delete.list
20110311-154002_0001.bin
20110311-154002_0002.bin
20110311-154002_0003.bin

2, dump the results to a file
A, create a mysql file
[root@localhost delete_old_capture_file]# cat delete_old_capture_file.mysql
select file_name
from capture_files
limit 5

B, Run the mysql and dump the results to a file
mysql -u username -ppassword DBname < delete_old_capture_file.mysql > delete_old_capture_file.dump

Linux Delete Files/Folder Older Than n Days

[root@localhost /]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

Test it:
1, create a new folder
mkdir 20090909

[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Mar 11 16:25 20090909

2, change folder time
touch -t 200909090909 20090909/

3, check time
[root@localhost xxx]# touch -t 200909090909 20090909/
[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Sep 9 2009 20090909

4, delete files and folder older than 30 days

[root@localhost xxx]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

[root@localhost xxx]# ll
drwxr-xr-x 2 esl esl 20480 Mar 11 16:21 20110311

linux change folder date

Need to change the folder date

I created a folder on 20110311

[root@localhost file_capture]# mkdir 20110101
[root@localhost file_capture]# ll
drwxr-xr-x 2 root root 4096 Mar 11 16:13 20110101


Change the date using touch

[root@localhost file_capture]# touch -t 201101010101
touch: missing file operand
Try `touch --help' for more information.
[root@localhost file_capture]# touch -t 201101010101 20110101/
[root@localhost file_capture]# ll
total 24
drwxr-xr-x 2 root root 4096 Jan 1 01:01 20110101

[root@localhost file_capture]# touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit

Note that the -d and -t options accept different time-date formats.

If a FILE is -, touch standard output.

Report bugs to .

mysql select entryby date range

select * from capture_files where date(created_on) < DATE_SUB(CURdate(), INTERVAL 30 DAY) and date(updated_on) < DATE_SUB(CURdate(), INTERVAL 30 DAY);

Assign Values to variables from a text file

[root@localhost xxx]# cat test.sh

#!/bin/bash

read a < process.log
echo $a


results:
[root@localhost xx]# ./test.sh
mysql-bin.000005

Thursday, March 10, 2011

linux synchronize two folder

Sync local folders:

rsync -avz /var/lib/mysql/mysql-bin.* /target/folder/

–a for archive mode (preserve all the attributes of each file and directory – ownership, permissions, etc),
-v for verbose mode (report a list of files processed by rsync)
-z for data compression to speed transfers up.

Sync remote folders over ssh:

rsync -avz ssh /root/origbinlog/* root@1922.168.1.196:/root/mysqlrecovery/binlogs/


DiskStation_230> rsync -avz chang@180:/usr/local/apache2/htdocs/RR/endeavor/download/dirty/ /volume1/lab/pdf/

cronjob every n minutes

22,58 * * * * your command here

*/5 * * * * rsync -avz /var/lib/mysql/mysql-bin.* /root/origbinlog/ >> rsync.log
every 5 minutes

Wednesday, March 9, 2011

mysqldump some tables

[root@localhost glen]# mysqldump -u username -pxxxxx DB users user_role role groups > user_group_role.mysql
[root@localhost glen]# less user_group_role.mysql

check cpu information on Linux machine

[root@localhost ~]# cat /proc/cpuinfo | grep core
core id : 0
cpu cores : 4
core id : 1
cpu cores : 4
core id : 2
cpu cores : 4
core id : 3
cpu cores : 4

Tuesday, March 8, 2011

linux cut last n chars

I want to cut last 17 chars

Before:
[root@localhost mysqlrecovery]# echo $a
mysql-bin.000001 mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 mysql-bin.000005 mysql-bin.000006 mysql-bin.000007 mysql-bin.000008 mysql-bin.000009 mysql-bin.000010 mysql-bin.000011 mysql-bin.000012

After:
[root@localhost mysqlrecovery]# echo $a | rev | cut -c17- | rev
mysql-bin.000001 mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 mysql-bin.000005 mysql-bin.000006 mysql-bin.000007 mysql-bin.000008 mysql-bin.000009 mysql-bin.000010 mysql-bin.000011