Linux:Tips
30.关于减号"-"的用途
1.创建TCP监听
/root>nc -l 9090
2.通过curl传入参数,注意,-d后指明从-读入数据,即从标准输入读取数据:
/root>curl -v -X GET http://127.0.0.1:9090/abc -d @-
abc
def
* About to connect() to 127.0.0.1 port 9090 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9090 (#0)
> GET /abc HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:9090
> Accept: */*
> Content-Length: 6
> Content-Type: application/x-www-form-urlencoded
>
……
3.在服务端nc处可以看到标准输出:
root@eb7023:/root>nc -l 9090
GET /abc HTTP/1.1
User-Agent: curl/7.29.0
Host: 127.0.0.1:9090
Accept: */*
Content-Length: 6
Content-Type: application/x-www-form-urlencoded
abcdef
……
注意,curl -d @-可以从标准输入读取。
又例如:curl -v -X GET http://127.0.0.1:9090/abc -d @- << eof
curl -v -X GET http://127.0.0.1:9090/abc -d @- << eof
> abc
> def
> eof
* About to connect() to 127.0.0.1 port 9090 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9090 (#0)
> GET /abc HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:9090
> Accept: */*
> Content-Length: 6
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 6 out of 6 bytes
<
……
重点是<<,重定义了标准输入的EOF字符串(标记)。
简单来说,减号"-"在很多命令中可以替代“文件”、“输入输出”:
如果命令要向文件(-)写入,则减号"-“指代标准输出;
如果命令要从文件(-)读入,则减号”-"指代标准输入;
29.参数代换xargs
ps aux | grep “xxx”
$ ps aux | grep test1280 | awk '{print $2}' | xargs kill -9
xargs存在的意义:
将前一个命令的标准输出按空白字符分割作为指定命令的参数。
$ ps aux | grep test1280 | awk '{print $2}' | xargs kill -9
xargs将前面的标准输出(PID号)作为kill -9 命令的参数。
区别于,管道命令一定会从标准输入(或指定文件)读取数据。
xargs要执行的命令在其后面展示。
28.管道命令和管道符
管道命令:可以接收standard input的命令,例如:
less、more、head、tail、cut、grep、sort、wc、uniq、tee、xargs
管道符:"|",能够将前一个命令的标准输出作为第二个命令的标准输入。
注意:管道符无法将前一个命令的标准错误进行传递处理。
tee双向重定向命令:
last | tee output.txt
tee读取标准输入,并:
1.输出到标准输出;
2.输出到tee指定的文件中;
数据流重定向:
错误的数据流重定向:
将标准输出和标准错误重定向到同一个文件:
find . -name xx > output.txt 2> output.txt
会造成错乱。
正确的数据流重定向(不会乱序):
find . -name xx > output.txt 2>&1
find . -name xx &> output.txt
cat不指定文件时,将阻塞地从标准输入读取数据然后输出到标准输出:
man cat:
With no FILE, or when FILE is -, read standard input.
说白了,cat命令就是读取多个文件并将其输出到标准输出的工具。
man cat:
cat - concatenate files and print on the standard output
Concatenate FILE(s), or standard input, to standard output.
cat:
1.读入:标准输入或命令行参数中指定的文件
2.输出:标准输出
思考:
cat > output.txt
备注:关于输入EOF
在标准输入时,可以通过Ctrl+D组合来键入EOF结束输入操作。
不过发现似乎,只有在新行的第一位键入才有效。
有效
abc
def
^D
无效
abc
def^D
可以使用<替代键盘输入到标准输入中:
cat > output.txt < input.txt
可以使用<<替换标准输入结束符,例如:
$ cat > output.txt << "abc"
> hi
> nihao
> jiangjinwang
> abc
[test1280@localhost ~]$
总结:
1.标准输出、覆盖:>
2.标准输出、追加:>>
3.标准输入:<
4.替换结束符:<<
在vim中一次性查找多个关键字:
查找一个关键字
/keyword
查找两个关键字
/keyword1\|keyword2
查找三个关键字
/keyword1\|keyword2\|keyword3
以此类推。(/后是正则表达式:"|“前的”“是为了转义”|")
添加iptables规则,开放8080端口:
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
持久化iptables配置:
service iptables save
ssh和scp在IPv6下的使用
ssh -6 -oPort=22 test1280@::1
scp -6 -oPort=22 anaconda-ks.cfg test1280@[::1]:/home/test1280
IPv6 address can be used everywhere where IPv4 address.
In all entries must be the IPv6 address enclosed in square brackets.
Note: The square brackets are metacharacters for the shell and must be escaped in shell.
informix:Informix:常用基本操作命令集
mysql导入导出:
导出表结构和表数据:
mysqldump -uuser -ppassword -h host -P port database table > output.sql
导出表结构不带数据:
mysqldump -uuser -ppassword -h host -P port -d database table > output.sql
导出数据库和其数据:
mysqldump -uuser -ppassword -h host -P port database > output.sql
导出数据库不带数据:
mysqldump -uuser -ppassword -h host -P port -d database > output.sql
打包压缩(解包解压):
-
打包|解包
tar cf redis-4.0.10.tar redis-4.0.10
tar xf redis-4.0.10.tar -
auto
tar axf redis-4.0.10.tar.*
自动识别压缩类型,解压缩。
- gzip
tar zcf redis-4.0.10.tar.gz redis-4.0.10
tar zxf redis-4.0.10.tar.gz
gzip redis-4.0.10.tar
gunzip redis-4.0.10.tar.gz
- bzip2
tar jcf redis-4.0.10.tar.bz2 redis-4.0.10
tar jxf redis-4.0.10.tar.bz2
bzip2 redis-4.0.10.tar
bunzip2 redis-4.0.10.tar.bz2
- xz
tar Jcf redis-4.0.10.tar.xz redis-4.0.10
tar Jxf redis-4.0.10.tar.xz
xz redis-4.0.10.tar
unxz redis-4.0.10.tar.xz
- compress
tar Zcf redis-4.0.10.tar.Z redis-4.0.10
tar Zxf redis-4.0.10.tar.Z
compress redis-4.0.10.tar
uncompress redis-4.0.10.tar.Z
详见:Linux:常用压缩及解压缩命令。
hostname:
输出主机名:
hostname
修改主机名:(实时,不持久化)
hostname test1280
修改主机名:(非实时,持久化,重启生效)
* CentOS 6: /etc/sysconfig/network
* CentOS 7: /etc/hostname
查看系统版本:
cat /etc/issue
lsb_release -a
cat /etc/os-release
cat /etc/redhat-release
cat /proc/version
uname -a
test -e FILE FILE exists
test -f FILE FILE exists and is a regular file
test -d FILE FILE exists and is a directory
test -r FILE FILE exists and read permission is granted
test -w FILE FILE exists and write permission is granted
test -x FILE FILE exists and execute (or search) permission is granted
test -h FILE FILE exists and is a symbolic link (same as -L)
test -L FILE FILE exists and is a symbolic link (same as -h)
man test
cp -s src dst
cp --symbolic-link src dst
make symbolic links instead of copying
cp -l src dst
cp --link src dst
hard link files instead of copying
cp -f src dst
cp --force src dst
if an existing destination file cannot be opened, remove it and try again
cp -r srcdir dstdir
cp --recursive srcdir dstdir
copy directories recursively
抓包:
tcpdump tcp -i any port 6081 -s 0 -w test1280.pcap
某文件被哪些进程占用:
fuser $ABSPATH
某进程占用的端口号:
lsof -p pid | grep LISTEN
curl -v http://127.0.0.1:8080/
curl -v -X POST http://127.0.0.1:8080/
curl -v -X POST -H 'Content-Type: application/json' http://127.0.0.1:8080/
curl -v -X POST -H 'Content-Type: application/json' http://127.0.0.1:8080/ -d '{"name": "test1280"}'
curl -v -X POST -H 'Content-Type: application/json' http://127.0.0.1:8080/ -d @test1280.json
curl -v -k https://127.0.0.1:8090/
端口占用(监听)
netstat -an | grep 22
netstat -anp | grep 22
-p, --program
Show the PID and name of the program to which each socket belongs.
du * -sh
shell无限循环:
for((;;))
do
done
while true
do
done
shell有限循环:
for((i=0;i<10;i++))
do
done
动态库搜索路径:
echo "$INFORMIXDIR/lib" >> /etc/ld.so.conf.d/ids.conf
echo "$INFORMIXDIR/lib/esql" >> /etc/ld.so.conf.d/ids.conf
/sbin/ldconfig
/sbin/ldconfig -p
可执行文件添加粘着位:
* root权限下执行
* chown root $ABSPATH
* chmod a+s $ABSPATH
CPU物理核数:
cat /proc/cpuinfo | grep "physical id" | uniq
CPU逻辑核数:
cat /proc/cpuinfo | grep "core id" | wc -l
CPU型号参数:
cat /proc/cpuinfo | grep 'model name'
内存总量:
cat /proc/meminfo | grep MemTotal
字符集转码
iconv gbkfile -f gbk -t utf8
iconv gbkfile -f gbk -t utf8 > utf8file
iconv -l
-f fromcodeset
-t tocodeset
创建硬连接(非目录)
ln source destination
创建软连接(目录或文件)
ln -s source destination
ln --symbolic source destination
Create hard links by default, symbolic links with --symbolic.
hwclock(Showing the Hardware Clock time is the default when no function is specified.)
hwclock --show
hwclock --hctosys
hwclock --systohc
设置日期:
date -s "20070414 13:58:00"
date -s "2008-04-14 13:58:00"
date -s "2009/04/14 13:58:00"
获取日期:
date +%Y%m%d%H%M%S
date +%Y/%m/%d
mkdir -p
mkdir --parents
no error if existing, make parent directories as needed