Total Pageviews

Wednesday 25 July 2012

linux的一些commands

此文用于记录使用 Linux 过程中的与 shell 和 commands 有关的有意思的小知识。

1, locale
默认情况下 Ubuntu 是 utf8 的 locale, 如:
liu@dog:$ locale -a
C
en_AG
en_AU.utf8
... ...
zh_CN.utf8
zh_SG.utf8
但某些情况下程序以GBK的Locale运行,则需要启用 zh_CN.GBK 这样的locale。 此时会报错:
$: export LC_ALL=zh_CN.gbk
warning: setlocale: LC_ALL: cannot change locale (zh_CN.gbk): No such file or directory
解决方法:
1) 修改/var/lib/locales/supported.d/local文件,在文件中添加zh_CN.GBK GBK
2) sudo dpkg-reconfigure locales
然后locale -a:
liuzhr@dog:/data/mq_tcp$ locale -a
C
en_AG
en_AU.utf8
en_BW.utf8
... ...
POSIX
zh_CN.gbk
zh_CN.utf8
zh_SG.utf8
2, 不重启服务清空日志
xxx@dog:/data/logs # echo "" > apache-access_2011101110.log
3, MySQL 没有权限的情况 (系统是Ubuntu 10.04 LTS):
The error message in /var/log/mysql/error.log:
/usr/sbin/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13)
110821  9:46:07 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
110821  9:46:07  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'open'.
InnoDB: Cannot continue operation.
110821  9:46:39 [Note] Plugin 'FEDERATED' is disabled.
Fix resolution: Edit /etc/apparmor.d/usr.sbin.mysqld , 将mysql-datadir 加入进去,如:
  /data/mysqldata/ r,
  /data/mysqldata/** rwk,
4, 如果 Ubuntu 不能启动怎么办? 答案是试试 boot-repair (Ubuntu 9.10 or later)。 这个问题与 grub2 有关,参考 Ubuntu 社区的 grub2 文档
5, 使用LVM: 参考 这里 还有 这篇
6, 如何在分区之间复制,还保持文件权限不变?
su root;
find / -xdev -print0 | cpio -pa0V /mnt/temp
参考:How to move your filesystem to a new hard drive
7, 如何压缩Linux分区的大小? 参考:Reducing VMDK Size 其中包括使用Gparted做分区复制。
8, Install Eclipse IDE:
http://colinrrobinson.com/technology/install-eclipse-ubuntu
----------------------------------------------------------------------
1. The problem of ftp url:
在写一个自动备份的脚本时发现,如果 username 中有 @ 符号时,使用 curl ftp://username:password@host/file 是不行的。这可能是 ftp url 地址格式的一个瑕疵。具体参考 Google Group上的一个讨论 (对于该讨论的正确性本人没有特别考证, 根据作者的观点,在整个URL中出现 “:”、”/”、”@” 都是不可行的。如果您发现此论断有问题请留言)。 还好 curl 可以使用 -n 指定使用 ~/.netrc 中的登录标识。
2. 直接使用 sed 命令批量替换文件中的内容
sed -i 's/\/var\/svn/d:\/data\/svn/g' `grep /var/svn -rl ./*/conf/trac.ini`
sed 命令还有很多用法,可惜平时用得不多,经常忘记了要查资料。
3. 使用 find 命令批量操作 Trac 项目同步SVN
find -maxdepth 1 -mindepth 1  -type d -print -exec trac-admin {} resync \;
对仅仅处理当前目录下的子目录的限定方法比较笨,也许有更好的方法。
find命令是最难用的,再提供一个统计代码行总数的例子。
$ find stat5.1projects/ ! -path '*.common/*' -a ! -path '*bin*' -a ! -path '*st
at.statuser*' -a \( -name *.java -or -name *.sql -or -name *.xml \) | xargs wc
-l
</pre lang="bash">
然后直接通过Shell计算出平均每天的代码行:
<pre lang="bash">
echo $((8888/22))
4. tar error message.
Removing leading `/’ from member names
It’s really annoying when it present in a crontab script. That is why (refer from here):
what do you want suggestions about? tar archives don’t contain absolute paths, only relative ones. this is correct behaviour, as the alternative is really nasty and illogical. this is the same way that other compression programs like winzip work. when you untar somethign you provide an destination directory, which implicity is ‘/’ in your case… but it should be explicit for logical operation
The resolution is using relative path. e.g.
cd /var/www
tar -cf /home/user/backup/wp-backup.tar web
# NOT below
# cd /home/user/backup
# tar -cf wp-backup.tar /var/www/web
5. svn: Delete unversioned files
svn status --no-ignore | grep '^[I?]' | sed 's/^[I?]//' | xargs rm -rf
Here are more frequent ask questions on subversion.
6. 防火墙后面的git通过代理访问:
设置GIT_PROXY_COMMAND系统变量即可。具体参考:Using Git with a SOCKS proxy
7. Ubuntu添加sudo用户:
Edit file /etc/sudoers, add line:
bob  ALL=(ALL) ALL
参考:http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch09_:_Linux_Users_and_Sudo
8. Unix timestamp 与 readable date 之间的转换:
shell:
date -d @1232144092 #timestamp to date
datedate=’1970-01-01 1000000000 sec GMT’
date -d today +%s #date to stamp
Perl:
date -d today +%s | perl -e 'print localtime(<>)."\n"' # to readable time
gawk:
date -d today +%s | gawk 'END{print strftime("%c",$1)}' # to readable time
9. AWK调用shell命令做一些计算
cat $f | awk '! /^NAI/ {cmd="date +%k%M -d @"$3; cmd | getline d; close(cmd); if (d>2357)print d}'
多行时可循环读取:
cmd_= ("cat "naiIdMappingFile);
while (cmd_ | getline d ) {
        split(d, naiIds," ");
        naiIdMaps[naiIds[1]" "]=naiIds[2];
}
close(cmd_)
其中要提醒一下的是,一定要记得 close() 的调用,不然很快会耗光文件描述符。
10. Perl 跟bash脚本配合,逐行对文件执行某些操作:
cat $f | perl -ane '$F[1] =~ s/\D+(\d+)\D+(\d+)\D+(\d+)/$1 $2/g; @cols = split(" ",$F[1]); $F[2] =~ s/\D+(\d+)\D+(\d+)\D+(\d+)/$3/g;print " @F ". scalar($F[2]-$cols[1]) ." \n";'