shell命令
man
man name
man section name
man -k regexp
运行中任务,暂停,重新后台启动。
$ ctrl + Z
$ bg 1
date
date "+%Y-%m-%d %H:%M:%S Day %j"
date "+%s"
ntpdate
ntpdate -q 0.pool.ntp.org
ntpdate 0.pool.ntp.org
cal month year
cal 12 2019
bc
bc
bc -l
wc 统计
wc sum.c
wc x.c makefile.sh
ps -ef
|wc -l
ps -ef
|grep mysql
|wc -l
who |wc -l
sort 排序
sort -n
printf 输出到屏幕
printf "%-30s:%-15s\n" report_selinux_enable
$report_selinux_enable
tr
cat report
|tr '[a-z]' '[A-Z]'
cat file1
| tr %
'\012'
tr -cd
"[a-z]"
uniq 去重
uniq -u
uniq -d
uniq -c
uniq -u
|sort
awk
awk '{printf("%d: %s\n", NR, $0);}' pandas技巧.md
ps -ef
| awk '/vapor/{printf("%s\n",$2)}
ls -l |awk '$2 > 3
{printf
("%d\n",
$7)}' #文件大于3k
hadoop fs -cat /kafka/consumer/htmlevent/2019-01-10_1[4-6].log|awk '$3=="97B223AD305148E5A4690972B68CCA40" {printf
("%s",
$0)}'|head -10000 > /data/workdir/hunter/hkc/input/funnelEvent/2019-01-10.log #过滤指定产品日志
awk '{for
(i
=1
;i
<=5
;i++
)printf
("%s\t",
$i);for
(i
=7
;i
<=NF
;i+
=2
)printf
("%s\t",
$i);print
""}' file
awk '{for
(i
=5
;i
<=NF
;i+
=2
)$i=""}{print
$0}' file #第五行之后取偶数列
ls -l | awk '{print
$5}' | sed -n '2p
'
# phoenix视图
cat $base/temp.txt |awk -v col="$column" '{printf
("\"%s\".\"%s\" %s,\n", col,
$11,
$17)}'|sed '/TABLE_NAME/d
'|sed '/\
"\"/d'|sed "/\?/d
"|sed '$s/,/}/' >> $base/${tablename//:/_}.txt
awk -v #引入外部变量
# 打印某一列并抽取字母
cat hbase.txt |sed -n '9p'|awk '{printf("%s\n
"), $3}'|tr -cd "[a-zA-Z
]"
awk -F'\t' 'length($3)>2 {print $0}' 20160803.kanjian.log|wc -l
# 去重统计
hadoop fs -cat /kafka/consumer/appUser/2019-01-19_*.log|awk -F'\t' '$8 ~/^20180119[0-9]*/ {printf("%s\n
",$7);}'|sort -u|wc -l
# 按行统计频次
cat 2019-01-27*|grep '视频列表页'|awk -F'\t' '{printf("%s, %s\n
", $6, $29)}'|awk '{a[$0]++}END{for(i in a) print i":
"a[i]}'
hadoop fs -cat /sourcedata/appUser/2019-01-27/*/part-*|awk -F'\t' '$14=="2
" {printf("%s, %s\n
", $14, $9)}'|awk '{a[$0]++}END{for(i in a) print i":"a
[i
]}''
grep 过滤显示
grep -n
grep -n main *.c
grep -v
'[Dd]isable' dev.stat
> dev.active
ps -ef
|grep '^vapor'
ls -l
|grep '^d'
grep '[0-9][0-9]*' test.c
ps -ef
|grep 'mysql' 查找并显示mysql的进程号
sed
cat date
.txt
|sed
's/\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]*\)/\3.\1.\2/g'
文件查找
找出关键字并打印文件频次
find -type f -print
| xargs grep "T_PERF_SLOWURL_DAY" | awk '{gsub(":"," ");print $1}' | uniq -c
cat www.libenfu.com.access.log
|cut -d
' ' -f 1
|sort -r
|uniq -c
|sort -n -r
|head -n 10
for i
in {20
..30
}
do
day
=2018-12-
${i}
echo $day
echo ${day//-/}
done
if [ ! -d
"/data/" ];then
mkdir /data
else
echo "文件夹已经存在"
fi
md5sum
校验文件完整性
diff
cmp file1 file2
diff file1 file2
diff -U file1 file2
diff -U 0 file1 file2
filelist
=`ls /data/workdir/hkc/out/autoMail/20181223`
for file in ${filelist}
do
echo ${file}
done
if [ $? == 0
];then
vim 常用操作
h:左 j:下 k:上 l:右
:
[0-9
]h/j/k/l 左 下 上 右操作几下
ctrl-b
ctrl-f
^
$
w
b
:^/$/d 移至开头/结尾/d行
/var
/tmp
u撤销
复制
:5,10co20
移动
:5,10m20
j两行合并
ctrl-g
/
[0-9
][0-9
]*
n:向下查找 N:向上查找
替换命令
:n1,n2s/pattern/tring/g
:1,50s/abc/xyz/g
:50,80s/^/ /g
:50,80s/^ //g
:1,
$s/ *$//g
:1,$/a
[i
]/b
[j
]/g
:1,$/a*b/x+y/g
将buf.len/100替换为buffer.size/1024
:1,
$s/buf\.len\/100/buffer\.size\/1024/g
:1,
$s:buf.len/100:buffer.size/1024:g
:1,
$s^http://webmail30\.189\.cn^http://baidu\.com^g
模式描述中增加\
(和\
)
将04-23-2019替换成2019.04.23
:1,
$s\
([0-9
][0-9
]\
)-
([0-9
][0-9
]\
)-
([0-9
][0-9
]*\
)/\3./1./2./g
crtl-s
ctrl-q
env|grep LANG
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YQsskKbV-1593671283467)(/Users/vapor/Desktop/blogs/imgs/Linux文件.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H66Mv2Ib-1593671283470)(/Users/vapor/Desktop/blogs/imgs/linux文件2.png)]
循环遍历
#!/bin/bash
# 任何配置参数
executorMemory=2G
executorNum=20
driverMemory=2G
scalaJar="/data/workdir/xyz/jar/html.jar"
products=("52.76.155.228" "52.76.222.29" "52.8.239.192")
types=("美国" "新加坡1" "新加坡2")
basic_path="/workdir/2016/189mail_data/source"
basic_path2="/workdir/2016/189mail_data/nginx_out_xuz3/"
for idx in {0..2}
do
pid=${products[$idx]}
echo ${pid} excuting!
typeTmp=${types[$idx]}
path1=${basic_path}${pid}.access.txt
outData=${basic_path2}${pid}_result.txt
hadoop fs -rm -r ${outData}
# 执行任务
classMain="export.MailDataAnalysis"
spark-submit --master yarn --executor-memory ${executorMemory} --num-executors ${executorNum} --driver-memory ${driverMemory} --class ${classMain} ${scalaJar} ${path1} ${outData} ${typeTmp}
#hadoop fs -getmerge ${outData} ./${pid}.txt
done
关键字批量杀进程
ps -ef
|grep open_test3
|grep -v
grep|cut -c 9-15
|xargs kill -9
查看端口是否被占用及关闭端口
https://www.cnblogs.com/hindy/p/7249234.html https://blog.csdn.net/u013451157/article/details/78943072
访问api的结果
curl localhost:8083/s?\
&a
=民
查看存在哪些进程
ps -ef
| grep "open_test"
打印cluster 日志
yarn logs -applicationId application_1553888952013_207137
> data1.txt
杀死进程
yarn application -kill application_1553888952013_207147
日期操作
yesterday
=$(date +%Y%m%d -d "1 day ago")
lastMonth
=$(date +%Y%m -d "1 month ago")
start_date
="20180726"
end_date
="20180830"
while [ "$start_date" -le
"$end_date" ];
do
stat_date
=`date -d "$start_date" +%Y-%m-%d`
stat_date_num
=`date -d "$start_date" +%Y%m%d`
echo $stat_date
echo $stat_date_num
start_date
=$(date -d "$start_date+1days" +%Y%m%d)
done
linux 查看整个目录的大小
du -sh ./*
解压及压缩文件
unzip xx.zip 解压文件
zip xx.zip MOS.mat
zip -r xx.zip MOS
hadoop递归赋权限
hadoop fs -chmod -R 777 /dept_ana/dataCenter/dataCenter_xuyz/forecast
linux查看文件底部(自下而上)
less
我们可以用less来解决,less查看一个文件时,可以使用类似vi的command命令,在command模式下按G跳到文件末尾,再使用f或b来翻页。
b:向上翻页
f:向下翻页
j:向下一行
k:向上一行
用 “?关键字” 来做检索,n 向上查找下一个关键字
Cat
cat [file]
查看文件的内容。全程式concatenate的意思,将文件内容连续输出到屏幕上。第一行到最后一行显示。
cat file-name |wc -l #查看文本行数
cat 200 file-name
tac
tac [file]
和cat刚好相反 是从最后一行到第一行的方式查看。
tail
tail -f 文件名:监听日志文件,有新内容实时输出到控制台
tail -n 1000 文件名:从底部查看文件的最后1000行内容
telnet
telnet ip port
退出---
ctrl + ] 第二步+q
crontab定时任务
crontab操作
crontab -u #设定某个用户的cron服务
crontab -l #列出某个用户cron服务的详细内容
crontab -r #删除某个用户的cron服务
crontab -e #编辑某个用户的cron服务
crontab -i #打印提示,输入yes等确认信息
crontab基本格式
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
定时任务的每段为:分,时,日,月,周,用户,命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
*:表示任意时间都,实际上就是“每”的意思。可以代表00-23小时或者00-12每月或者00-59分
-:表示区间,是一个范围,00 17-19 * * * cmd,就是每天17,18,19点的整点执行命令
,:是分割时段,30 3,19,21 * * * cmd,就是每天凌晨3和晚上19,21点的半点时刻执行命令
/n:表示分割,可以看成除法,*/5 * * * * cmd,每隔五分钟执行一次
* */5 * * 8 对应的还是每分钟。
0 */1 * * * 对应的是每个整点0分。 如00:00 01::00
Curl 访问
-X 访问方式
-H 头部信息
-d 数据参数
curl -X POST/GET
curl -X POST -H 'content-type=application/json'
端口转发:
ssh登陆远程服务器
转发远程服务器端口到本地端口
案例:本地访问远程服务器数据库。
Xshell 连接 远程服务器 主机、ssh端口
输入用户账号密码
点击隧道->添加 ->
目标主机-> 远程服务器上的mysql端口 隐射到本地localhost 跟本地的端口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b4a3Bkpl-1593671283471)(shell命令.assets/image-20200103151431265.png)]
impala-shell
# 进入impala-shell
impala-shell
# 连接到集群, 集群机器可设为如下(001-012)
connect zhpt-bd-storage-009.e.189.cn;
# 显示进度条
set live_progress=true;
shell基础
shell错误输出重定向到标准输出
$ ./tmp/test.sh > /tmp/test.log 2>&1
shell中每个进程都和三个系统文件相关联
标准输入stdin
标准输出stdout
标准错误stderr
三个系统文件的文件描述符分别为0,1和2。
所以这里2>&1的意思就是将标准错误也输出到标准输出当中
取当前目录
$(cd "$(dirname "$0")";pwd)
# /data/jsp/data/