#194 Linux 常用压缩命令 CheatSheet

2017-03-22

rar

rar a xxx.rar files...
rar e xxx.rar ./

rar a -p123456 xxx.rar files...
rar e -p123456 xxx.rar ./

rar a -v20m xxx.rar files...

tar

常用的参数说明(grep "^ \-[a-zA-Z0-9]" <(tar --help) | sort):

  • vf 分别表示输出详细信息、指定压缩包名称
  • x 解压
  • c 压缩
  • t 列出压缩包里面的文件列表
  • 压缩文件类型(Linux 下最常用的两种):
  • z:gz
  • j:bz2
# To extract an uncompressed archive:
tar -xvf /path/to/foo.tar

# To create an uncompressed archive:
tar -cvf /path/to/foo.tar /path/to/foo/

# To extract a .gz archive:
tar -xzvf /path/to/foo.tgz

# To create a .gz archive:
tar -czvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .gz archive:
tar -ztvf /path/to/foo.tgz

# To extract a .bz2 archive:
tar -xjvf /path/to/foo.tgz

# To create a .bz2 archive:
tar -cjvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .bz2 archive:
tar -jtvf /path/to/foo.tgz

# To create a .gz archive and exclude all jpg,gif,... from the tgz
tar czvf /path/to/foo.tgz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} /path/to/foo/

# To use parallel (multi-threaded) implementation of compression algorithms:
tar -z ... -> tar -Ipigz ...
tar -j ... -> tar -Ipbzip2 ...
tar -J ... -> tar -Ipixz ...

zip

# Create zip file
zip archive.zip file1 directory/

# To list, test and extract zip archives, see unzip
cheat unzip

# Extract archive
unzip archive.zip

# Windows 下创建的压缩包放到 Ubuntu 下解压可能会有编码问题
unzip archive.zip -O gbk

# Test integrity of archive
unzip -tq archive.zip

# List files and directories in a file
unzip -l archive.zip

7z

sudo apt install p7zip-full
dpkg -L p7zip-full p7zip | grep bin/
# /usr/bin/7z
# /usr/bin/7za
# /usr/bin/7zr
# /usr/bin/p7zip

文档中说是支持 7z,xz,tar,gz,zip,bz2,iso,rpm,deb 等等等等格式,不过没用过。

# 压缩
7z a  xxxx.7z files...
# 解压
7z e  xxxx.7z
# 查看文件列表
7z l  xxxx.7z

参考

  1. man
  2. --help
  3. cheat / tldr

#193 使用 git-daemon

2017-03-15

有时需要临时分享一个仓库给朋友,我们可以用 SSH 协议:

git clone ssh://markjour@192.168.64.234/home/markjour/Projects/Mine/lego

其实 git-daemon 是一个更好的方法。

#192 logging 时间格式

2017-03-12
import logging
LOG_LEVEL = logging.DEBUG
LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s'
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
logging.info('hello world')

默认时间格式是 yyyy-mm-dd hh:mm:ss,xxx,如果我们要改这个格式可以用 datefmt 参数,遵循 time.strftime 的格式化参数格式。

问题有一个,毫秒从哪里来?

方法一:使用 msecs 占位符

最简单的办法:在格式字符串中使用 msecs 占位符,比如:

import logging
LOG_LEVEL = logging.DEBUG
LOG_FORMAT = '%(asctime)s.%(msecs)03d %(levelname)s %(message)s'
LOG_DATEFMT = '%H:%M:%S'
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT, datefmt=LOG_DATEFMT)
logging.info('hello world')

方法二:改用 datetime 对象

import logging
from datetime import datetime

class MyFormatter(logging.Formatter):
    converter = datetime.fromtimestamp

    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            # s = time.strftime(datefmt, ct)
            s = ct.strftime(datefmt)
        else:
            # t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
            t = ct.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s,%03d" % (t, record.msecs)
        return s

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler()
logger.addHandler(console)
formatter = MyFormatter(fmt='%(asctime)s %(levelname)s %(message)s', datefmt='%H:%M:%S.%f')
console.setFormatter(formatter)
logging.info('hello world')

#191 dc 计算器

2017-02-26

dc, desk caclulator,使用的是逆波兰式表达方法,而不是熟悉的代数标记法。

只是一些简单的用法,复杂的指令就不研究了:

# 1 + 2 + 3 + 4 + 5
dc -e "1 2 + 3 + 4 + 5 + p"

# 4 ** 3
dc -e "4 3 ^ p"

就是数在前面,操作符在后面,最后 p 输出。

# 加 减 乘 除 取余
+ - * / %
# 乘方 开方
^ v
# 用后面的数除以前面的数
~
# 清除结果
c
# 设置精度为 3
3k

如果进入 bc 交互模式,按 q 退出。

dc -e "3k 1 2 / p 2 / p"
.500
.250

#190 MySQL 增加或修改注释

2017-02-19

表注释

SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = '5mzb' AND TABLE_NAME = 'user';
ALTER TABLE `user` COMMENT = '用户';

字段注释

SELECT COLUMN_NAME, COLUMN_COMMENT FROM `information_schema`.`COLUMNS`
WHERE TABLE_SCHEMA = 'sendcloud' AND TABLE_NAME = 'user_info' ORDER BY ORDINAL_POSITION;
ALTER TABLE `user`
CHANGE COLUMN `create_time`
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' AFTER `expire_time`;
ALTER TABLE `user`
MODIFY COLUMN
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' AFTER `expire_time`;

好傻 X 啊,只改个备注,却必须要把字段声明带上,增加出错的可能。

#189 JS: split 方法

2017-02-12
"ni wo ta".split(" ");
// [ 'ni', 'wo', 'ta' ]

"ni wo ta".split(" ", 1);
// [ 'ni' ]
"ni wo ta".split(" ", 2);
// [ 'ni', 'wo' ]
"ni wo ta".split(" ", 3);
// [ 'ni', 'wo', 'ta' ]
"ni wo ta".split(" ", 4);
// [ 'ni', 'wo', 'ta' ]

"ni wo ta".split(":");
// [ 'ni wo ta' ]
"ni wo ta".split(":", 1);
// [ 'ni wo ta' ]
"ni wo ta".split(":", 2);
// [ 'ni wo ta' ]

如果要一刀将字符串切两半:

var line = "a : b : c";
var part1 = line.split(":", 1)[0];
if (a !== line) {
  var a = part1.trim();
  var b = line.substr(part1.length + 1).trim();
  console.log([a, b]);
}
var line = "a : b : c";
var index = line.indexOf(":");
if (index != -1) {
  var a = line.substr(0, index).trim();
  var b = line.substr(index + 1).trim();
  console.log([a, b]);
}

参考资料与拓展阅读

#188 为旧版本 CentOS 设置更新源

2017-02-08

总有些时候需要操作一些老旧的 CentOS 版本,如果需要更新就比较麻烦了,因为绝大部分更新源都不对老版本提供服务了。
这时我们只好使用 CentOS Vault,从官方接受这最后的支持,慢慢的下载更新。

#187 Nginx 连接处理方法

2017-02-07

Syntax: use method;
Default: —
Context: events

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

Nginx 默认会自动选择当前平台最高效的方法。

  • epoll Linux 平台上的最佳选择
  • kqueue BSD 家族(包括 MacOS)的最佳选择
  • poll 第一备选
  • select 第二备选
  • eventport Solaris 提供的机制,但是 Nginx 文档上建议使用 /dev/poll
  • /dev/poll

我们接触多的是 Linux 服务器,所以知道是 epoll 就行了。
除非你非常知道自己在做什么,不要调整 use 参数。

以下参数可以控制相关模块的引入:

--with-poll_module
--without-poll_module
--with-select_module
--without-select_module

#186 勇敢者的游戏

2017-02-04
  1. 勇敢者的游戏 Jumanji
  2. 勇敢者的游戏 2:太空飞行棋
  3. 勇敢者游戏:决战丛林
  4. 勇敢者游戏 2:再战巅峰

Jumanji

莎拉 展翅飞得高,白天看不到,晚上来追杀,赶紧把命逃
     # 吸血蝙蝠
艾伦 身陷丛林难回家,色子要等五或八
朱迪 它个子最小,咬一下起个包,能让你昏迷发高烧
     # 大蚊子
皮特 这段任务很艰难,红毛长尾阻你向前
     # 猴子
皮特 头大体胖牙齿尖,用你来个大会餐,腿短胆小命难保
     # 狮子

莎拉 长得又快又急促,小心别让它抓住
     # 食人花
艾伦 野蛮猎人一出现,鲁莽射击不留人
     # 范培特
朱迪 打雷没什么,不要被迷惑,按兵不动,必成大祸
     # 动物大迁徙
皮特 游戏规则被破坏,往后倒退一大块
     # 皮特作弊变猴子

莎拉 每月一逢台风到,大雨滂沱起浪潮
     # 暴雨
艾伦 陷阱就在你脚下,地板瞬间变流沙
     # 流沙
朱迪 有个教训宜记取,后退一步有转机
     # 流沙消失,地板凝固
皮特 除了具有山核桃,我们还有八只手
     # 蜘蛛

莎拉 紧要关头目标近,地牛翻身陷困境
     # 地震
艾伦 Jumanji (赢了,游戏结束)

#185 Supervisor 进程守护

2017-01-31

子命令

  • help
  • help <action>
  • add <name> […]
  • remove <name> […]
  • update
  • update all
  • update <gname> […]
  • clear <name>
  • clear <name> <name>
  • clear all
  • fg <process>
  • pid
  • pid <name>
  • pid all
  • reload
  • reread
  • restart <name> 不会重新加载配置文件
  • 如果配置有更新,应该 reread 一下
  • 如果有 section 增删,还应该 update 一下
  • restart <gname>:*
  • restart <name> <name>
  • restart all
  • signal
  • start <name>
  • start <gname>:*
  • start <name> <name>
  • start all
  • status
  • status <name>
  • status <name> <name>
  • stop <name>
  • stop <gname>:*
  • stop <name> <name>
  • stop all
  • tail [-f] <name> [stdout|stderr] (default stdout)