Linux grep LinuxTextProccessing
2017-10-13
egrep
= grep -E
fgrep
= grep -F
# -G, --basic-regexp 基本正则, 默认
# -E, --extended-regexp 拓展正则
# -P, --perl-regexp Perl 正则
# -w 完全匹配字词
# -x 完全匹配整行
grep markjour /var/log/auth.log
grep -E markjour /var/log/auth.log
# -F, --fixed-strings
grep -F markjour /var/log/auth.log
tail -1000 /var/log/auth.log | grep -Ev 'gnome-keyring-daemon|CRON'
-r
目录
-R
目录,处理软链
-v
排除
-i
忽略大小写 (ignore-case
)
-m
控制匹配次数
-a
包含二进制内容的文件当作纯文本处理
-
-I
包含二进制内容的文件跳过
-
-b
输出命中内容的偏移量
-n
输出行号
-o
仅输出匹配部分
-h
不输出文件名(匹配多个文件时默认输出文件名 -H
)
-L
, --files-without-match
仅输出没有匹配的文件名
-l
, --files-with-matches
仅输出匹配文件名
-
-c
, --count
仅输出文件名和匹配行数
-
--include=GLOB
只查找匹配 GLOB(文件模式)的文件
--exclude=GLOB
跳过匹配 GLOB 的文件
--exclude-from=FILE
跳过所有匹配给定文件内容中任意模式的文件
-
--exclude-dir=GLOB
跳过所有匹配 GLOB 的目录
-
-B
, --before-context=NUM
打印文本及其前面NUM 行
-A
, --after-context=NUM
打印文本及其后面NUM 行
-C
, --context=NUM
打印NUM 行输出文本
-NUM
等同于 --context=NUM
Linux
2017-08-05
dd if=/dev/zero of=cdata.img bs=1G count=5
mkfs ext4 -F cdata.img
LANG=en fdisk -lu cdata.img
sudo mount -o loop cdata.img /mnt/iso
sudo umount /mnt/iso
Linux
2017-05-11
命名空间是什么
对内核资源进行分区,为进程隔离提供支持。
命名空间类型
Namespace |
CloneFlag |
Version |
Year |
Mount (mnt) |
CLONE_NEWNS |
2.4.19 |
2002 |
UTS |
CLONE_NEWUTS |
2.6.19 |
2006 |
Inter-process Communication (ipc) |
CLONE_NEWIPC |
2.6.19 |
2006 |
Process ID (pid) |
CLONE_NEWPID |
2.6.24 |
2008 |
Network (net) |
CLONE_NEWNET |
2.6.29 |
2009 |
User ID (user) |
CLONE_NEWUSER |
3.8 |
2013 |
Control group (cgroup) |
CLONE_NEWCGROUP |
4.6 |
2016 |
Time |
CLONE_NEWTIME |
5.6 |
2020 |
Mount
挂载点可以在不同命名空间之间共享。
UTS
UNIX Time-Sharing
不知道为什么叫这个名字。作用是隔离主机名和域名。
IPC
不同 IPC 命名空间的进程不能通过 shm
系列方法共享内存。
PID
Network
每个网络命名空间都有一组自己的网络资源,包括 IP 地址、路由表、套接字列表、连接跟踪表、防火墙等等。
User
主要作用是权限的隔离。
Cgroup
进程控制组。
Time
时间的隔离。
系统调用
参考资料与拓展阅读
Linux
2017-03-29
atime
:access time,访问时间
mtime
:change time,修改时间,内容修改
ctime
:modify time,变更时间,内容修改,或者元数据修改
crtime
:create time / birth time,创建时间,部分文件系统支持(Ext4,btrfs,xfs)
[root@localhost]# stat abc.txt
File: `abc.txt'
Size: 11214 Blocks: 24 IO Block: 4096 regular file
Device: fc10h/64528d Inode: 3153180 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-09 19:35:42.321279859 +0800
Modify: 2016-07-09 19:35:42.321279859 +0800
Change: 2016-07-09 19:35:42.321279859 +0800
[root@localhost]# cat abc.txt > /dev/null
[root@localhost]# stat abc.txt
File: `abc.txt'
Size: 11214 Blocks: 24 IO Block: 4096 regular file
Device: fc10h/64528d Inode: 3153180 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-07-09 19:35:42.321279859 +0800
Modify: 2016-07-09 19:35:42.321279859 +0800
Change: 2016-07-09 19:35:42.321279859 +0800
[root@localhost]# mount
/dev/vda1 on / type ext4 (rw,noatime,errors=remount-ro)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/vdb on /data type ext4 (rw,noatime)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
重点是 noatime 选项,表示文件系统忽略 atime 的变更,因为频繁的 atime 变更对性能有影响。
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
参考
man
--help
cheat
/ tldr
Linux
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
Linux CentOS RPM Yum
2017-02-08
总有些时候需要操作一些老旧的 CentOS 版本,如果需要更新就比较麻烦了,因为绝大部分更新源都不对老版本提供服务了。
这时我们只好使用 CentOS Vault,从官方接受这最后的支持,慢慢的下载更新。
Linux ShellScripting
2017-01-10
command >output.txt # 把标准输出重定向到一个文件中
command >> output.txt # 把标准输出重定向到一个文件中(追加)
command 1>output.txt # 把标准输出重定向到一个文件中
command 1>>output.txt # 把标准输出重定向到一个文件中(追加)
command 2>output.txt # 把标准错误重定向到一个文件中
command 2>>output.txt # 把标准错误重定向到一个文件中(追加)
command >output.txt 2>&1 # 把标准输出和标准错误一起重定向到一个文件中
command >>output.txt 2>&1 # 把标准输出和标准错误一起重定向到一个文件中(追加)
command <input.txt # command命令以output.txt文件作为标准输入
command <input.txt >output.txt # command命令以output.txt文件作为标准输入, 以 output.txt 文件作为标准输出
# 从标准输出中读入,直至遇到delimiter分界符(不能与已存在的文件重名)
command << delimiter
command <&m 把文件描述符m作为标准输出
command >&m 把标准输出重定向到文件描述符m中
command <&- 关闭标准输入
Linux
2017-01-02
示例
# %c 表示当前 locale 的日期和时间
$ date +%c
2017年01月02日 星期一 12时00分00秒
# 获取当前时间戳
$ date +%s
1483329600
# -d 表示使用指定时间,而非默认的当前时间
# 获取指定时间的时间戳 (时间字符串 -> 时间戳)
$ date +%s -d "2017/01/01 00:00:00"
1483200000
# @ 表示时间戳
# 时间戳 -> 时间字符串
$ date -d @1483200000 # 等于: date --date="@1483200000"
2017年 01月 01日 星期日 00:00:00 CST
$ date -d @1483200000 +%c
2017年01月01日 星期日 00时00分00秒
# -u 表示使用 UTC 时间
$ date -u
2017年01月02日 星期一 14时00分00秒 UTC
$ date -d @1483200000 -u
2016年 12月 31日 星期六 16:00:00 UTC
$ date +%Y%m%d%H%M%S
20170102120000
# -R, --rfc-email, RFC 5322 格式
$ date -R
Mon, 02 Jan 2017 12:00:00 +0800
$ date --rfc-3339=date # 可以缩写成 d
2017-01-02
$ date --rfc-3339=seconds # 可以缩写成 s
2017-01-02 12:00:00+08:00
$ date --rfc-3339=ns # 可以缩写成 n
2017-01-02 12:00:00.000000000+08:00
常见日期格式占位符
- Y 年,4 位数字
- m 月,2 位数字
- d 日,2 位数字
- H 时,2 位数字
- M 分,2 位数字
- S 秒,2 位数字
- F 完整日期, 等于
%Y-%m-%d
- T 完整时间, 等于
%H:%M:%S
- R 等于
%H:%M
Linux
2016-09-14
用等于号隔开的一组路径,当我们执行一个命令时,会逐个检查这些路径,直到找到一个可以执行的文件。