博文以前的格式不是markdown格式的,现在更新的时候很难受。
需要自己开新帖,重新整理成markdown格式.
零碎的命令整理

debian将用户加入sudo

usermod -aG sudo username

测试用户是否加入了sudo列表

lostspeed@localhost:~$ sudo whoami
[sudo] lostspeed 的密码:
root
lostspeed@localhost:~$ whoami
lostspeed

将.tex文件转成pdf

apt-get install texlive
pdflatex myfilename.tex

debian安装16位汇编语言套件

as86, ld86等都装上了。

apt-get install bin86

debian安装c开发套件

gcc, g++, make等都装全了。

apt-get install build-essential

debian查看已经安装的包信息

dpkg -l | grep binu

检查锁

如果程序挂死了或死循环,可以先找一下,是否由于文件锁阻塞引起的

  • 查看系统中当前的锁状态
root@debian9:/dev# cat /proc/locks
1: FLOCK  ADVISORY  WRITE 1067 00:13:15498 0 EOF
2: FLOCK  ADVISORY  WRITE 4241 00:15:17316 0 EOF

第1列是序号
第2列是锁类型, FLOCK是文件锁,如果锁类型前面还有 "->"描述,说明此锁请求正在被阻塞。
第3列是锁类别, ADVISORY是建议锁
第4列是锁操作, WRITE 是写
第5列是持有锁的进程ID, e.g. 4241
第6列是锁文件的信息(主设备号,次设备号,文件ID)
e.g. 主设备号是0, 次设备号是15, 文件ID是17316
第7列是锁在文件中的开始字节位置 e.g. 0,文件开始处
第8列是锁在文件中的结束字节位置 e.g. EOF, 文件结束处

  • 看看发生锁操作的设备是哪个
    设备信息在/dev中
    拿最后一个锁为例,可以看到设备主号为0,设备次号为15.
    用ls /dev -liv | awk命令来过滤设备主号为0,设备次号为15的设备。
    因为设备号为0时,不显示,所以,只列出次设备号为15的设备,然后自己人肉过滤。
root@debian9:/dev# ls /dev -liv | awk '$6 == "15" || $7 == "15"'
 1185 lrwxrwxrwx  1 root       root          15 1月   5 10:51 stderr -> /proc/self/fd/2
 1183 lrwxrwxrwx  1 root       root          15 1月   5 10:51 stdin -> /proc/self/fd/0
 1184 lrwxrwxrwx  1 root       root          15 1月   5 10:51 stdout -> /proc/self/fd/1
 1056 crw--w----  1 root       tty       4,  15 1月   5 10:52 tty15

可以看到发生锁操作的设备是标准输入,标准输出,和标准错误输出

  • 查看锁文件位置
    拿随后一个锁实验
    可以看到锁的持有者PID = 4241
    列出PID = 4241的进程信息
root@debian9:/dev# lsof -p 4241
COMMAND    PID USER   FD   TYPE             DEVICE SIZE/OFF     NODE NAME
test_file 4241 root  cwd    DIR               8,17     4096 16777243 /home/lostspeed/disk_work/my_dev/test_case/test_file_locker
test_file 4241 root  rtd    DIR                8,1     4096        2 /
test_file 4241 root  txt    REG               8,17    27464 16777229 /home/lostspeed/disk_work/my_dev/test_case/test_file_locker/test_file_locker
test_file 4241 root  mem    REG                8,1  1689360   391688 /lib/x86_64-linux-gnu/libc-2.24.so
test_file 4241 root  mem    REG                8,1   135440   391703 /lib/x86_64-linux-gnu/libpthread-2.24.so
test_file 4241 root  mem    REG                8,1    92584   391684 /lib/x86_64-linux-gnu/libgcc_s.so.1
test_file 4241 root  mem    REG                8,1  1063328   391692 /lib/x86_64-linux-gnu/libm-2.24.so
test_file 4241 root  mem    REG                8,1  1566168  1179964 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
test_file 4241 root  mem    REG                8,1   153288   391683 /lib/x86_64-linux-gnu/ld-2.24.so
test_file 4241 root    0u   CHR              136,2      0t0        5 /dev/pts/2
test_file 4241 root    1u   CHR              136,2      0t0        5 /dev/pts/2
test_file 4241 root    2u   CHR              136,2      0t0        5 /dev/pts/2
test_file 4241 root    3uW  REG               0,21        0    17316 /run/lock/my_prog/my_prog_was_exist
test_file 4241 root    4u  unix 0xffff8d0020eb1800      0t0    25698 type=DGRAM

在4241进程中,找inode_id为17316的行信息
如果信息较少,直接就看到了
如果信息较多,可以过滤一下。

root@debian9:/dev# lsof -p 4241 | grep 17316
test_file 4241 root    3uW  REG               0,21        0    17316 /run/lock/my_prog/my_prog_was_exist
  • 验证锁文件的主人
root@debian9:/dev# fuser /run/lock/my_prog/my_prog_was_exist
/run/lock/my_prog/my_prog_was_exist:  4241

可以看到/run/lock/my_prog/my_prog_was_exist确实是/home/lostspeed/disk_work/my_dev/test_case/test_file_locker/test_file_locker

后台执行一个命令

root@localhost:/home# nohup ls /home &> /home/run_result.txt
root@localhost:/home# cat /home/run_result.txt 
nohup: ignoring input
alarm.data
nohup.out
run_result.txt

mysql命令行

// mysql本地命令行登陆
./mysql -u root -p

// 切换数据库
use my_db

// 执行查询
select * from tbl_name;

// 查看表记录数量
select count(*) from tbl_name;

安装完组件后,如果想找该组件安装了啥文件

(e.g. libicu-dev的头文件装在哪了?)

dpkg -L libicu-dev

debian8.8的源

装libicu-dev时,只设置163的源还不行

root@localhost:/home/dev/ExIconv-master# cat /etc/apt/sources.list
# 
# jessie-updates, previously known as 'volatile'
# A network mirror was not selected during install.  The following entries
# are provided as examples, but you should amend them as appropriate
# for your mirror of choice.
#
deb http://ftp.debian.org/debian/ jessie-updates main contrib
deb-src http://ftp.debian.org/debian/ jessie-updates main contrib

deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
aptitude install libicu-dev

debian8.8屏幕解锁

debian8.8装完,默认是开机后,如果不动桌面, 屏幕自动锁屏。
查了资料,按ESC键,就可以退出锁屏,进入登陆UI.

在vi中查找字符串

?/字符串可以带空格
如果出现模式不匹配,不要理会,按n就可以找到想要的下一个字符串
e.g.

:s/2018-08-06 14:59
n

判断软件是否已经安装

which automake

echo $?
如果返回0为已经安装

tcpdump抓包命令

抓回环包并打印包内容

tcpdump -i lo -u -s 0 -X

排除ssh的包

tcpdump not port 22  -u -s 0 -X

抓默认的包

tcpdump  -u -s 0 -X

抓本地包

tcpdump -i lo -u -s 0 -X

在fedora22中看syslog

‘tail -f /var/log/message’ 是无效的

journalctl -f

执行一个命令时,定位这个命令的全路径

[root@localhost ~]# whereis -b passwd
passwd: /usr/bin/passwd /etc/passwd

gdb下断点的技巧

如果用 break x.cpp:rows, 要拷贝文件名,麻烦。
// gdb下断点的技巧 - 先走到要下断点的函数附近, 再对看见的具体行下断点, 这样不用计算行数.
// list class_a::fn_member_a
// break 55

sh : if 字符串的判断

if [ -n “${str_obj}” ] 字符串是否长度不为0

if [ -z “${str_obj}” ] 字符串是否长度为0

function shfn_install_netcard_driver() {
	declare -i i_rc=0
	declare str_tmp=""
	declare net_card_ko_path_name=""
	
	echo ">> install_net_card_driver.sh"

	while true
	do
		# --------------------------------------------------------------------------------
		# install net card driver
		# --------------------------------------------------------------------------------
		net_card_ko_path_name=$(find /lib/modules/$(uname -r) -name 'e1000e.ko')
		i_rc=$?
		printf "i_rc = %d, net_card_ko_path_name = %s\n" $i_rc "$net_card_ko_path_name"
		if [ -n "${net_card_ko_path_name}" ]
		then
			echo "find e1000e.ko"
		fi

		str_tmp=$(find /lib/modules/$(uname -r) -name 'e1000ea.ko')
		i_rc=$?
		printf "i_rc = %d, str_tmp = [%s]\n" $i_rc "$str_tmp"
		if [ -z "${str_tmp}" ]
		then
			echo "not find e1000ea.ko"
		fi

		break
	done
	
	echo "<< install_net_card_driver.sh"
}

debian安装linux-headers

编译intel官方e1000e网卡驱动时,需要linux-headers

aptitude install linux-headers-`uname -r`

建立一个简单的readme文件

declare g_dir_cur="$(readlink -f $(dirname "$0"))"
declare g_dir_output="${g_dir_cur}/output_dir"

		# --------------------------------------------------------------------------------
		# create readem file
		# --------------------------------------------------------------------------------
		touch ${g_dir_output}/readme
		echo -e "create by my_test.sh" > ${g_dir_output}/readme
		echo -e $(date "+%Y-%m-%e %H:%M:%S") >> ${g_dir_output}/readme
		echo -e $(pwd) >> ${g_dir_output}/readme

运行脚本时, 得到脚本的全路径

#/bin/bash
# @file \my_case\test_2018_0425_1006.sh

declare -r MY_RC_OK=0
declare -r MY_RC_ERR=1

g_param_0="$0"
g_dir_cur="$(readlink -f $(dirname "$0"))"
g_dir_prev="$(pwd)"

function shfn_main() {
	printf "g_param_0 = %s\n" "${g_param_0}"
	printf "g_dir_cur = %s\n" "${g_dir_cur}"
	printf "g_dir_prev = %s\n" "${g_dir_prev}"

	return ${MY_RC_OK}
}

shfn_main
exit ${MY_RC_OK}
root@localhost:/home/dev/1.1# ./test_2018_0425_1006.sh 
g_param_0 = ./test_2018_0425_1006.sh
g_dir_cur = /home/dev/1.1
g_dir_prev = /home/dev/1.1
root@localhost:/home/dev/1.1# pwd
/home/dev/1.1

在当前目录查找文件,返回目标文件的绝对路径

如果 find . -name “x”, 返回的是./x. 如果要用找到的目标文件来写个脚本, 还是绝对路径方便.

root@localhost:/home/dev# find $(pwd) -name "pf_ring.ko"
/home/dev/pf_ring_release/drivers/pf_ring.ko

bz2文件解压

-d 解压
-k 解压后包留源文件

bzip2 -dk ./binutils-2.25.tar.bz2

同时查看多个文件的内容

如果要在已知的文件列表中,查找特定的文件内容,这挺实用的。

[root@localhost ~]# cat /etc/*-release*
CentOS release 6.9 (Final)
CentOS release 6.9 (Final)
CentOS release 6.9 (Final)
cpe:/o:centos:linux:6:GA

[root@localhost ~]# cat /etc/redhat-release /etc/system-release /etc/system-release-cpe /etc/centos-release
CentOS release 6.9 (Final)
CentOS release 6.9 (Final)
cpe:/o:centos:linux:6:GA
CentOS release 6.9 (Final)

查看系统发行版本配置文件

/etc下有一些*-release文件,记录着系统的发行版本. 可以看出此发行版是从OS分支发展来的。
前提: 版本的发行者,没有人为的修改这些文件。

[root@localhost ~]# find /etc -name '*release*'
/etc/redhat-release
/etc/system-release
/etc/system-release-cpe
/etc/centos-release
[root@localhost ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@localhost ~]# cat /etc/system-release 
CentOS release 6.9 (Final)
[root@localhost ~]# cat /etc/system-release-cpe
cpe:/o:centos:linux:6:GA
[root@localhost ~]# cat /etc/centos-release
CentOS release 6.9 (Final)

可以看出centos是从redhat来的。

在系统syslog日志中查看特定的日志内容

tail -f /var/log/messages | grep 'affected_rows ='

用cat命令建立一个简单的文件

cat > the_new_file_path_name << “the_end_word”

root@localhost:/home/test# cat > ./test3.c << "END"
> #include <stdlib.h>
> #include <stdio.h>
> 
> int main(int argc, char** arg) {
>     printf("use cat make a c file\n");
> }
> END
root@localhost:/home/test# cat ./test3.c 
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** arg) {
    printf("use cat make a c file\n");
}

tcpdump发包

tcpdump -i eth0 host 192.168.6.66 and port 6666 -n

设置时间

date "+%Y-%m-%d %H:%M:%S" -s "2018-3-30 11:22:00"

查看时间

date "+%Y-%m-%d %H:%M:%S"

ssh客户端连接ssh主机

# connect ssh server
# usage : ssh user_name@ip_addr port
ssh root@192.168.6.66 22

在红帽中安装g++

yum install gcc-c++

增加so到debian的优雅方法

root@debian750devmin:/etc/ld.so.conf.d# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

root@debian750devmin:/etc/ld.so.conf.d# ls /etc/ld.so.conf.d/
总用量 8
-rw-r--r-- 1 root root 44 11月 30  2013 libc.conf
-rw-r--r-- 1 root root 68 12月 23  2013 x86_64-linux-gnu.conf

root@debian750devmin:/etc/ld.so.conf.d# cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib

echo "# libpcap 1.8.1 configuration" > /etc/ld.so.conf.d/libpcap.conf
echo "/home/dev/libpcap_1.8.1_release/lib" >> /etc/ld.so.conf.d/libpcap.conf

root@debian750devmin:/etc/ld.so.conf.d# cat /etc/ld.so.conf.d/libpcap.conf
# libpcap 1.8.1 configuration
/home/dev/libpcap_1.8.1_release/lib

ldconfig

删除一个拥有大量文件的目录

用rm -f ./*删除报错了
用下列命令,边删除,边打印被删除的文件名。

find . -type f -print -delete

查看一个目录的文件数量限制

这个限制,对于要放大量文件到一个目录时,很有用。如果达到了最大文件数量限制,就无法在目录中再创建新文件了。
df -i [your_path]

# df -i $(pwd)
Filesystem            Inodes   IUsed IFree IUse% Mounted on
/dev/mapper/VolGroup-lv_root
                     2334720 2334720     0  100% /

修改debian的DNS服务器列表

在/etc/network/interfaces中添加dns-nameservers条目,然后reboot
只修改自己用的那块网卡. e.g. eth0

root@me:~# cat /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth3
iface eth3 inet static
	address 192.168.100.135
	netmask 255.255.255.0
	gateway 192.168.100.1

allow-hotplug eth0
iface eth0 inet static
	address 192.168.17.12
	netmask 255.255.255.0
	gateway 192.168.26.1
	dns-nameservers 8.8.8.8 8.8.4.4

查看debian网卡列表

ls -l -h -p /sys/class/net/

root@fort:~# ls -l -h -p /sys/class/net/
total 0
lrwxrwxrwx 1 root root 0 Mar 16 17:41 eth0 -> ../../devices/pci0000:00/0000:00:1c.0/0000:01:00.0/net/eth0
lrwxrwxrwx 1 root root 0 Mar 16 17:41 eth1 -> ../../devices/pci0000:00/0000:00:1c.1/0000:02:00.0/net/eth1
lrwxrwxrwx 1 root root 0 Mar 16 17:41 eth2 -> ../../devices/pci0000:00/0000:00:1c.2/0000:03:00.0/net/eth2
lrwxrwxrwx 1 root root 0 Mar 16 17:41 eth3 -> ../../devices/pci0000:00/0000:00:1c.3/0000:04:00.0/net/eth3
lrwxrwxrwx 1 root root 0 Mar 16 17:41 lo -> ../../devices/virtual/net/lo

查找文件带通配符

find . -name "*.ini"
find /etc/ -name "*.ini"

查看端口的占用者

lsof -i :port

lsof -i :22222

ls带彩色的显示

#!/bin/bash
# @file my_ls
# @brief execute ls command by color
# @note
#	copy ./my_ls /usr/bin/
#	chmod 775 /usr/bin/my_ls
#	my_ls [ls command parameter...]
#	e.g. on one dir, run my_ls /home/dev/
#
#	replace my_ls as ls
#	mv /bin/ls /bin/ls.org
#	mv /usr/bin/my_ls /bin/ls
#
#	now, can execute ls [...], display will be by color

function main() {
	echo "argv = [${@:1}]"
	ls.org --color -l -h -p "${@:1}"
}

main "${@:1}"
exit 0

将tar文件解压到指定目录

tar xzvf ./x.tar -C /dir/
指定的目录要用mkdir -p 先建立出来才行

tar xzvf ./media_for_install.tar -C /home/dev/demo/unzip/

sh function : 参数操作

function func_parameter_forwarding_to_printf() {
	# example : modify input parameter
	# insert new parameter
	# set -- "${@:1:2}" "parameter was insert on orignal $3" "${@:4}"

	# remove current first parameter
	# shift

	# get parameter content by range
	# $(echo ${@:parameter_position:parameter_number}
	# $(echo ${@:2:3} # get parameter 2 to parameter 4's content
	# if parameter_number not given, get given pos to last parameter, e.g. ${@:2}
	# if parameter_number >= max parameter number, get given pos to last parameter, e.g. ${@:2:99}
	
	printf "$(echo $1)" $(echo ${@:2})
	return 0
}

function print_color_text() {
<<MY_NOTE
	调用本函数时, 是按照printf函数调用的格式来的
	得到并修改原始参数1, 加入颜色控制符
MY_NOTE

	local new_cmd_line="$COLOR_BEGIN_TEST$1$COLOR_END"

	# ${@:2} is content from parameter 2 to last parameter
	func_parameter_forwarding_to_printf "$new_cmd_line" $(echo ${@:2})

	return 0
}

sh function parameter list

$0: shell的名称。
$n:第n个位置参数。
$*:含有所有参数内容的单个值
$@:将所有的命令行参数展开为多个参数。
$#:位置参数的总数。
$?:最近一个命令的退出状态码。
$$:当前shell的进程ID(PID)。
$!:最近一个后台命令的PID

get current user id and user name

# id -u
0
# id -u -n
root

/etc/passwd

/etc/passwd file format

row content : news:x:9:9:news:/var/spool/news:/bin/sh

field1 news: // login user name 
field2 x: // user password
field3 9: // user UID
field4 9: // group UID
field5 news: // note or desc
field6 /var/spool/news: // user home dir
field7 /bin/sh // user default shell

查看操作系统类型和版本

debian, redhat, fedora

head -n 1 /etc/issue.net
# --------------------------------------------------------------------------------
# check os type and version
# --------------------------------------------------------------------------------
# [root@localhost ~]# head -n 1 /etc/issue.net
# Red Hat Enterprise Linux Server release 5.4 (Tikanga)
# --------------------------------------------------------------------------------
# root@debian750devmin:~# head -n 1 /etc/issue.net
# Debian GNU/Linux 7
# --------------------------------------------------------------------------------
# cx@localhost:~$ head -n 1 /etc/issue.net
# Debian GNU/Linux 8
# --------------------------------------------------------------------------------
# [root@localhost ~]# head -n 1 /etc/issue.net
# Fedora release 22 (Twenty Two)
# --------------------------------------------------------------------------------

Fedora 安装软件

dnf install xx

用户相关的操作

查看用户组

cat /etc/group

添加用户

useradd usr_test1

查看某个用户

cat /etc/passwd | grep game

将make的结果全部重定向到文件中

只用重定向符号“>”是不行的, 要用"&>",才能将编译工程时,屏幕上的编译输出全部重定向到文件中。

make rebuild &> /home/dev/build_rc.txt

在linux下安装jdk

找个地方(e.g. /home/dev/)放安装包(jdk-9.0.1_linux-x64_bin.tar.gz)
cd /home/dev/
tar -xzvf jdk-9.0.1_linux-x64_bin.tar.gz
vi /etc/profile
增加如下内容

JAVA_HOME=/home/dev/jdk-9.0.1
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

source /etc/profile
java -version

root@debian750devmin:/home/dev# java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

生成.so的Makefile

编译时比正常的linux工程的Makefile多加一个编译选项: -shared
将工程输出改成 x.so

CFLAGS = -Wall -g -shared

make rebuild 完成后,用 nm -D x.so 可以看到so中的导出函数.

向系统中增加so

将so放到/lib和/usr/lib里面
然后运行 ldconfig

查看依赖的so

ldd ./my_prog

查看库函数

如果调用第三方库中的函数,说无法参考的函数,可以看看是否库中的导出函数列表是否有这函数。

nm -D x.so

查看内存使用状态

free -m | grep Mem:

debian7.5安装libpcap

最初,为了系统的干净,source list中安装源为光盘。
但是那个光盘上没有libpcap…
可能当初没有下全debian7.5的所有的安装dvd。

vi /etc/apt/sources.list
#

# deb cdrom:[Debian GNU/Linux 7.5.0 _Wheezy_ - Official amd64 DVD Binary-1 20140426-13:37]/ wheezy contrib main

deb cdrom:[Debian GNU/Linux 7.5.0 _Wheezy_ - Official amd64 DVD Binary-1 20140426-13:37]/ wheezy contrib main

deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free
apt-get update
aptitude install libpcap-dev

pod文件转成html

在debian下使用pod2html, 转完,再到windows中去看。

#! /bin/sh
# @file pod_to_html.sh
# run on debian 7.5

echo pod file to html begin ...

pod2html --infile androiddump.pod --outfile androiddump.pod.html
pod2html --infile asn2deb.pod --outfile asn2deb.pod.html
pod2html --infile capinfos.pod --outfile capinfos.pod.html
pod2html --infile captype.pod --outfile captype.pod.html
pod2html --infile ciscodump.pod --outfile ciscodump.pod.html
pod2html --infile dftest.pod --outfile dftest.pod.html
pod2html --infile dumpcap.pod --outfile dumpcap.pod.html
pod2html --infile editcap.pod --outfile editcap.pod.html
pod2html --infile extcap.pod --outfile extcap.pod.html
pod2html --infile idl2deb.pod --outfile idl2deb.pod.html
pod2html --infile idl2wrs.pod --outfile idl2wrs.pod.html
pod2html --infile mergecap.pod --outfile mergecap.pod.html
pod2html --infile randpkt.pod --outfile randpkt.pod.html
pod2html --infile randpktdump.pod --outfile randpktdump.pod.html
pod2html --infile rawshark.pod --outfile rawshark.pod.html
pod2html --infile reordercap.pod --outfile reordercap.pod.html
pod2html --infile sshdump.pod --outfile sshdump.pod.html
pod2html --infile text2pcap.pod --outfile text2pcap.pod.html
pod2html --infile tshark.pod --outfile tshark.pod.html
pod2html --infile udpdump.pod --outfile udpdump.pod.html
pod2html --infile wireshark-filter.pod --outfile wireshark-filter.pod.html

echo pod file to html end.

禁止syslog的发送速率限制

默认的syslog设置是,如果发太快了,有一些不会显示。
如果要调试程序的话,漏掉日志的显示,那就没法玩了。

  1. Locate and open the following file in a text editor:

/etc/rsyslog.conf
Add the following lines under the Global Directives section:

# Disable rate-limiting of log entries
$SystemLogRateLimitInterval 0
$SystemLogRateLimitBurst 0
  1. Then restart rsyslogd
service rsyslog restart (/etc/init.d/rsyslog restart)

杀掉给定的进程名称

kill -9 $(ps aux | grep 'my_prog' | grep -v 'grep' | awk '{print $2}')

列出一个有太多文件的目录中的部分文件

ls -l -f | head -n count_want_to_list
e.g. 进入到一个有巨量文件的目录,这时运行ls直接挂掉了,用如下命令只列出前40个文件。

ls -l -f | head -n 40

删除一个有太多文件的目录

第2个命令删除的时候有提示,虽然慢,但是安心,可以看到删除进度。
改进后,第一个命令也有进度,好像比第2个命令还快。

ls -l -f | xargs -n 10 rm -v -fr ls
ls -1 -f | xargs rm -v

linux-ORACLE基本操作

切换到oracle用户

su - oracle

查看系统服务列表

/sbin/service service_name restart // 服务如果不被识别,可以看看服务是否存在

/sbin/chkconfig --list

查看进程的内存使用情况

watch -n 1 "pmap pid_to_view"

查看内存使用情况

watch -n 1 "free -t"

查找文件

按照通配符查找文件,通用. 因为我们大部分情况下都不知道文件全名,有时候只能想起文件名的片段.

find / -name '*8168*'

卸载CentOS系统已安装的服务

卸载CentOS自带的Apache
/** 查看CentOS自带的Apache安装包 */
[root@ls local]# rpm -qa | grep httpd
httpd-2.2.3-11.el5_1.centos.3
/** 卸载Apache */
rpm -e httpd-2.2.3-11.el5_1.centos.3 --nodeps

查看端口占用情况(程序, IP, 协议)

[root@ls bin]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:653                 0.0.0.0:*                   LISTEN      4711/rpc.statd      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      4682/portmap        
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      4926/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      4946/sendmail: acce 
tcp        0      0 :::3690                     :::*                        LISTEN      32567/svnserve      
tcp        0      0 :::80                       :::*                        LISTEN      5411/httpd          
tcp        0      0 :::22                       :::*                        LISTEN      4915/sshd           
[root@ls bin]#

查看进程内存布局

127|root@generic:/ # cat /proc/1/maps
00008000-00030000 r-xp 00000000 00:01 1222       /init
00031000-00032000 r--p 00028000 00:01 1222       /init
00032000-00034000 rw-p 00029000 00:01 1222       /init
00034000-00065000 rw-p 00000000 00:00 0          [heap]
b6fa0000-b6fc0000 rw-s 00000000 00:0b 1364       /dev/__properties__
b6fc0000-b6fc1000 r--p 00000000 00:00 0
b6fc1000-b6fc3000 rw-p 00000000 00:00 0
beca6000-becc7000 rw-p 00000000 00:00 0          [stack]
ffff0000-ffff1000 r-xp 00000000 00:00 0          [vectors]

建立多级子目录,不用关心该文件夹路径中是否有不存在的父目录

mkdir -p
mkdir -p /home/lostspeed/dir1/dir2/dir3/dir4/dir5
验证下
ls -AR ./dir1
当最终目录的某个父目录不存在时,不加-p选项,建立文件夹失败。

apt-get install之前预览包名称

apt-get install时,如果报名填错,就找不到包来安装.
需要在缓存中先找下包名, 看看要安装哪个包.
e.g. 要安装libpcap

root@debian750:/home/lostspeed/test/src# apt-cache search libpcap
libpcap0.8 - system interface for user-level packet capture
libnet1 - library for the construction and handling of network packets
libwiretap2 - network packet capture library -- shared library
dns-flood-detector - detect abusive usage levels on high traffic nameservers
dsc-statistics-collector - DNS Statistics Collector - Collector component
fprobe - export captured traffic to remote NetFlow Collector
fprobe-ng - export captured traffic to remote NetFlow Collector (meta)
fwknop-client - FireWall KNock OPerator client side - C version
fwknop-server - FireWall KNock OPerator server side - C version
imsniff - Simple program to log Instant Messaging activity on the network
libdessert0.87 - a simple and extensible routing-framework for testbeds
libdessert0.87-dev - header file and documentation for libdessert0.87
libnet1-dbg - debugging symbols for libnet
libnet1-dev - development files for libnet
libnet1-doc - developers documentation files for libnet
libnet-libdnet-perl - interface to libdumbnet's low-level network functions
libnet-pcap-perl - Perl binding to the LBL pcap packet capture library
libpcap-dev - development library for libpcap (transitional package)
libpcap0.8-dbg - debugging symbols for libpcap0.8
libpcap0.8-dev - development library and header files for libpcap0.8
libpcapnav0 - wrapper to libpcap
libpcapnav0-dev - development files for libpcapnav
libmlpcap-ocaml - binding of libpcap for OCaml (runtime package)
libmlpcap-ocaml-dev - binding of libpcap for OCaml
libncap-dev - static library and header files for libncap
libncap44 - network capture library
ncaptool - network capture tool
python-ncap - Python bindings for libncap
netdiscover - active/passive network address scanner using arp requests
ntop - display network usage in web browser
ntop-dbg - display network usage in web browser (debug symbols)
pcaputils - specialized libpcap utilities
python-pcapy - Python interface to the libpcap packet capture library
python-pcs - Packet Construction Set for Python
python-pypcap - object-oriented Python interface for libpcap
libpcap-ruby - Transitional package for ruby-pcap
libpcap-ruby1.8 - Transitional package for ruby-pcap
ruby-pcap - Interface to LBL Packet Capture library (libpcap)
snort - flexible Network Intrusion Detection System
snort-common - flexible Network Intrusion Detection System [common files]
snort-common-libraries - flexible Network Intrusion Detection System ruleset
snort-doc - Documentation for the Snort IDS [documentation]
snort-mysql - flexible Network Intrusion Detection System [MySQL]
snort-pgsql - flexible Network Intrusion Detection System [PostgreSQL]
tcpick - TCP stream sniffer and connection tracker
tcptrace - Tool for analyzing tcpdump output
tcpxtract - extracts files from network traffic based on file signatures
libwiretap-dev - network packet capture library -- development files
libwiretap4 - network packet capture library -- shared library
bittwist - libpcap based Ethernet packet generator
fwknop-apparmor-profile - FireWall KNock OPerator - Apparmor profile
libfko-doc - FireWall KNock OPerator - documentation
libfko-perl - FireWall KNock OPerator - Perl module
libfko-python - FireWall KNock OPerator - Python module
libfko2 - FireWall KNock OPerator - shared library
libfko2-dbg - FireWall KNock OPerator - debugging symbols
libfko2-dev - FireWall KNock OPerator - developpement library
libghc-pcap-dev - Haskell wrapper around the C libpcap library
libghc-pcap-doc - Haskell wrapper around the C libpcap library; documentation
libghc-pcap-prof - Haskell wrapper around the C libpcap library; profiling libraries
netmate - netdude clone that shows pcap dump lines in network header style
ntopng - High-Speed Web-based Traffic Analysis and Flow Collection Tool
ntopng-data - High-Speed Web-based Traffic Analysis and Flow Collection Tool (data files)
ntopng-dbg - High-Speed Web-based Traffic Analysis and Flow Collection Tool (debug symbols)
pcapfix - repairs broken pcap and pcapng files
python-libpcap - Python wrapper for libpcap packet capture library
ruby-packetfu - PacketFu is a mid-level packet manipulation library for Ruby
ruby-pcaprub - Ruby bindings for LBL Packet Capture library (libpcap)
tcpspy - Incoming and Outgoing TCP/IP connections logger

在结果中找libpcap, 会发现要装libpcap-dev才行.

libpcap-dev - development library for libpcap (transitional package)

so we apt-get below

apt-get install libpcap-dev

查看libc版本

如果开发机编译时的libc版本比部署环境的libc版本高,会导致编译后的程序在部署环境中不能运行.

root@debian750:/home/lostspeed# find / -name libc.so.6
/lib/x86_64-linux-gnu/libc.so.6
root@debian750:/home/lostspeed# ls -l /lib/x86_64-linux-gnu/libc.so.6
lrwxrwxrwx 1 root root 12 4月  29 12:23 /lib/x86_64-linux-gnu/libc.so.6 -> libc-2.19.so

增加用户

在登录后, su root
然后 adduser

root@debian:/home/lostspeed# adduser dev
Adding user `dev' ...
Adding new group `dev' (1002) ...
Adding new user `dev' (1002) with group `dev' ...
Creating home directory `/home/dev' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for dev
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y
root@debian:/home/lostspeed# ls -l -p /home/dev
total 0

vi翻页

Ctrl+F => PageDown
Crtl+B => PageUp

vi跳到最后一行

gg + G => goto the end of file

vi查找

向下查找 /keyword
向上查找 ?keyword
查找下一个 n

查看一个文件夹的文件数量

ls -l | wc -l

自动查看一个文件夹的文件数量

watch -n 1 -d 'ls -l | wc -l'

列出目录

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

效果

/home/lostspeed/dev
   .
   |-1_7
   |---1.0.0.1
   |-----empty_dir
   |-3rd
   |---1.0.0.1
   |-base
   |---1.0.0.1
   |-----file
   |-----net
   |-----process
   |-----string
   |-business
   |---1.0.0.1
   |-----string

查看redhat发行版本

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-573.el6.x86_64 #1 SMP Wed Jul 1 18:23:37 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /proc/version
Linux version 2.6.32-573.el6.x86_64 (mockbuild@x86-027.build.eng.bos.redhat.com) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Wed Jul 1 18:23:37 EDT 2015
[root@localhost ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.7 (Santiago)
[root@localhost ~]#

查看debian发行版本

root@ls:/usr/local/bin# cat /etc/issue
Debian GNU/Linux 7 \n \l

查看glibc版本

[root@localhost ~]# ldd --version
ldd (GNU libc) 2.12
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

.xz文件解压

root@debian750devmin:/home/lostspeed/dev# ls
linux-4.13.4.tar.xz
root@debian750devmin:/home/lostspeed/dev# xz -d linux-4.13.4.tar.xz 
root@debian750devmin:/home/lostspeed/dev# ls
linux-4.13.4.tar
root@debian750devmin:/home/lostspeed/dev#

.tar文件解压

root@debian750devmin:/home/lostspeed/dev# ls
linux-4.13.4.tar
root@debian750devmin:/home/lostspeed/dev# tar -xvf ./linux-4.13.4.tar

.tar文件压缩

tar -cvf ./my_tar.tar ./objdir/

.Z文件解压

root@debian750devmin:/home/lostspeed/dev# uncompress code.tar.Z 
root@debian750devmin:/home/lostspeed/dev# ls
code.tar

debian中软件的安装

aptitude install <软件包名称>

root@debian750devmin:/home/lostspeed/dev# aptitude install autoconf
aptitude install tcpdump

查看syslog日志文件

tail -f [日志文件] |grep [搜索关键字]

tail -f /var/log/syslog |grep test

/var/log/messages中只记录了LOG_WARNING, LOG_NOTICE, LOG_INFO级别的日志

tail -f /var/log/messages |grep test

查看日志中的错误信息(查看日志行记录中的多个关键字)
tail -f 日志文件全路径 | grep keyword1 | grep keyword2

tail -f /var/log/messages | grep test_syslog | grep ERROR

redhat修改IP

/etc/hosts 负责域名解析

[root@localhost ~]# more /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

/etc/sysconfig/network 负责本地域名和网络配置是否有效

[root@localhost ~]# more /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
GETAWAY=192.168.227.1

/etc/sysconfig/network-scripts/ifcfg-ethx 负责具体的网卡IP地址配置

[root@localhost ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:7C:82:91
TYPE=Ethernet
UUID=33fc9622-333f-4b1b-8de4-e9ca5bee0cdc
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.227.41
NETMASK=255.255.255.0
GATEWAY=192.168.227.1
DNS1=202.106.46.151

rh设置时间

如果时间服务器连不上(e.g. 自己本地的虚拟机), 可以自己设置时间

[root@localhost src]# date -s "2017-10-19 14:23:00"
Thu Oct 19 14:23:00 CST 2017

在VI中跳到指定行数

行数gg

e.g. 打开某文件后, 在默认的命令输入状态输入969gg

969gg

查看端口占用

netstat -anp |grep 端口

netstat -anp |grep 6666

安装dos2unix

在博客上贴出的Makefile, 再回帖到本地的Makefile时,换行符号已经变成windows格式了。在linux下编译时,Makefile是错误的。可以用dos2unix在linux下,先将Makefile修正后,再编译。
测试环境debian, 添加安装源.
向 /etc/apt/sources.list 文件中添加一行安装源:

deb http://ftp.de.debian.org/debian wheezy main

更新安装包列表:

aptitude update

安装dos2unix:

aptitude install dos2unix

使用dos2unix:

dos2unix -n ./Makefile ./Makefile.linux

./Makefile.linux 就是转好的linux版Makefile
试了一下,还要手工改点东西。要将Makefile中的开头4个或8个空格,都手工改成TAB键才行, make才能通过.

SUSE12开启ssh服务

打开装好的suse12虚拟机, 用xshell连不上去(上次还是可以的)。
ps aux |grep ssh // 没有发现ssh运行。
service sshd restart // 启动ssh服务,再运行xshell, 可以连上suse12了。