Linux命令大全


########################################################
echo -e '1\x01foo' > /tmp/a.txt
echo -e '2\x01bar' >> /tmp/a.txt

########################################################

#!/bin/bash
HADOOP_HOME=/usr/local/hadoop
HIVE_HOME=/usr/local/hive

HADOOP_CORE=$(ls $HADOOP_HOME/hadoop-core*.jar)
CLASSPATH=.:$HIVE_HOME/conf:$(hadoop classpath)
 
for i in ${HIVE_HOME}/lib/*.jar ; do
    CLASSPATH=$CLASSPATH:$i
done
 
########################################################

java -cp $CLASSPATH HiveJdbcClient
########################################################
last命令用于显示用户最近登录信息

########################################################
grep命令:

     基本格式:grep  expression

     1.主要参数

    [options]主要参数:
    -c:只输出匹配行的计数。
    -i:不区分大小写
    -h:查询多文件时不显示文件名。
    -l:查询多文件时只输出包含匹配字符的文件名。
    -n:显示匹配行及行号。
    -s:不显示不存在或无匹配文本的错误信息。
    -v:显示不包含匹配文本的所有行。

    pattern正则表达式主要参数:
    \: 忽略正则表达式中特殊字符的原有含义。
    ^:匹配正则表达式的开始行。
    $: 匹配正则表达式的结束行。
    \<:从匹配正则表达 式的行开始。
    \>:到匹配正则表达式的行结束。
    [ ]:单个字符,如[A]即A符合要求 。
    [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
    .:所有的单个字符。
    * :有字符,长度可以为0。

    2.实例  

  (1)grep 'test' d*  #显示所有以d开头的文件中包含 test的行
  (2)grep ‘test’ aa bb cc    #显示在aa,bb,cc文件中包含test的行
  (3)grep ‘[a-z]\{5\}’ aa   #显示所有包含每行字符串至少有5个连续小写字符的字符串的行
  (4)grep magic /usr/src  #显示/usr/src目录下的文件(不含子目录)包含magic的行
  (5)grep -r magic /usr/src  #显示/usr/src目录下的文件(包含子目录)包含magic的行
  (6)grep -w pattern files :只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’),

########################################################

linux cut、sed、awk:

cut语法:
[root@www ~]# cut -d'分隔字符' -f fields    ## 用于有特定分隔字符
[root@www ~]# cut -c 字符区间            ## 用于排列整齐的信息
选项与参数:
-d:后面接分隔字符。与 -f 一起使用;
-f:依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思;
-c:以字符 (characters) 的单位取出固定字符区间;

#echo $PATH |cut -d':' -f 5
#echo $PATH |cut -d ':' -f 3,5
#echo $PATH |cut -d ':' -f 1-3
#echo $PATH |cut -d ':' -f 1-3,5
#cat /etc/passwd| cut -d ':' -f 1,7 

sed语法:
1/ 删除:d命令
$ sed '2d' test.txt                  -----删除example文件的第二行。
$ sed '2,$d' test.txt               -----删除example文件的第二行到末尾所有行。
$ sed '$d' test.txt                       -----删除example文件的最后一行。
$ sed '/test/'d test.txt         -----删除example文件所有包含test的行。
2/ 替换:s命令
$ sed's/test/mytest/g' test.txt                                 
## 在整行范围内把test替换为mytest。如果没有g标记,则只有每行第一个匹配的test被替换成mytest。
$ sed -n 's/^test/mytest/p' test.txt                          
## (-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的test被替换成mytest,就打印它。
$ sed 's/^192.168.0.1/&localhost/' test.txt           
## &符号表示追加一个串到找到的串后。所有以192.168.0.1开头的行都会被替换成它自已加 localhost,变成192.168.0.1localhost。
$ sed -n 's/loveable/\1rs/p' test.txt
## love被标记为1,所有loveable会被替换成lovers,而且替换的行会被打印出来。
$ sed's#10#100#g' test.txt
## 不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,“#”在这里是分隔符,代替了默认的“/”分隔符。表示把所有10替换成100。
选定行的范围:逗号
$ sed -n'/test/,/check/p' test.txt
## 所有在模板test和check所确定的范围内的行都被打印。
$ sed -n'5,/^test/p' test.txt
## 打印从第五行开始到第一个包含以test开始的行之间的所有行。
$ sed'/test/,/check/s/$/sed test/' test.txt
## 对于模板test和west之间的行,每行的末尾用字符串sed test替换。
多点编辑:e命令
$ sed -e '15,25d'-e 's/test/check/' test.txt
## (-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除15至25行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。
$ sed--expression='s/test/check/' --expression='/love/d' test.txt
## 一个比-e更好的命令是--expression。它能给sed表达式赋值。(通过上面的命令将所有的test替换成龙check,将所有的带有love的行删除掉了)
3/ 从文件读入:r命令
$ sed '/test/rfile' test.txt
-----file里的内容被读进来,显示在与test匹配的行下面,如果匹配多行,则file的内容将显示在所有匹配行的下面。
4/ 写入文件:w命令
$ sed -n'/test/w file' test.txt
-----在example中所有包含test的行都被写入file里
5/ 追加命令:a命令
$ sed'/^test/a\\--->this is a example' test.txt   
## '--->this is a example'被追加到以test开头的行后面,sed要求命令a后面有一个反斜杠。
6/ 插入:i命令
$ sed '/test/i\\some thing new-------------------------' test.txt
如果test被匹配,则把反斜杠后面的文本插入到匹配行的前面。
7/ 下一个:n命令
$ sed '/test/{ n; s/aa/bb/; }' test.txt
-----如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续。
8/ 退出:q命令
$ sed '10q' test.txt
-----打印完第10行后,退出sed。

awk
#last -n 5 |awk  '{print $1}'
-F指定域分隔符为':'
#cat/etc/passwd |awk  -F ':'  '{print $1}' 
#cat /etc/passwd|awk  -F ':'  '{print $1"\t"$7}'
#cat/etc/passwd |awk  -F ':'  'BEGIN {print "name,shell"}  {print $1","$7} END {print "blue,/bin/nosh"}'
搜索
#awk  -F: '/root/'  /etc/passwd
找root开头的: awk -F:  '/^root/'  /etc/passwd
#awk  -F ':' '{print "filename:" FILENAME ",linenumber:" NR",columns:" NF ",linecontent:"$0}' /etc/passwd

########################################################
隐藏文件都不会被拷贝
cp -r /home/user/* /mnt/temp
隐藏文件都会被拷贝:
cp -r /home/user/. /mnt/temp
########################################################

制造一个10M的文件: /dev/zero 是一个字符设备,会不断返回0值字节(\0)。
dd if=/dev/zero of=sun.txt bs=1M count=10

查找文件链接全路径:
ls -l 链接文件

########################################################
数字 4 、2 和 1表示读、写、执行权限(具体原因可见下节权限详解内容),即 r=4,w=2,x=1 。此时其他的权限组合也可以用其他的八进制数字表示出来,如: rwx = 4 + 2 + 1 = 7 rw = 4 + 2 = 6 rx = 4 +1 = 5 即
若要同时设置 rwx (可读写运行) 权限则将该权限位 设置 为 4 + 2 + 1 = 7 若要同时设置 rw- (可读写不可运行)权限则将该权限位 设置 为 4 + 2 = 6 若要同时设置 r-x (可读可运行不可写)权限则将该权限位 设置 为 4 +1 = 5
设置所有人可以读写及执行
chmod 777 file  (等价于  chmod u=rwx,g=rwx,o=rwx file 或  chmod a=rwx file)
设置拥有者可读写,其他人不可读写执行
chmod 600 file (等价于  chmod u=rw,g=---,o=--- file 或 chmod u=rw,go-rwx file )

chmod -R 777 aaa

########################################################

apt-get install -f
--fix-broken:修复错误的包依赖

ubnntu repositiories:
gedit /etc/apt/sources.list

linux连续执行命令:使用   ;   &&   ||  隔开,分号:全部执行,&&:遇到错误停止, || : 遇到成功停止

#####################i添加组和用户###################################
groupadd hue
useradd -g hue hue         usermod -a -G apache cnzhx 添加已有用户到组
passwd hue
########################################################

groups 查看当前登录用户的组内成员
groups gliethttp 查看gliethttp用户所在的组,以及组内成员
whoami 查看当前登录用户名
/etc/group文件包含所有组
/etc/shadow和/etc/passwd系统存在的所有用户名

########################################################
tail -n 100 ./file.txt
head -n 100 ./file.txt

查看链接源文件:
ls -l 

更改文件归属
chown firebird:firebird employee.fdb
更改文件夹归属
chown www.www -R abcdef


.well-known/pki-validation/fileauth.txt
echo "201812050638442e8y7j4gskve7c3edmil5l32fzc9tkuhc9paqc695buu669o9t" > fileauth.txt


postfix/master[1490]: fatal: open lock file /var/lib/postfix/master.lock: unable to set exclusive lock: Resource temporarily unavailable

# ls -l /var/lib/postfix/master.lock
-rw------- 1 postfix postfix 17 Feb  7 11:08 /var/lib/postfix/master.lock
# fuser /var/lib/postfix/master.lock
/var/lib/postfix/master.lock: 15590
# ps -ef | grep 15590
root     15590  0.0  0.0  6624 1868 ?        Ss   Feb07   0:33    /usr/libexec/postfix/master
Kill the process that is accessing the master.lock file:
# fuser -k /var/lib/postfix/master.lock
Stop postfix, remove pid and lock file, kill the process accessing master.lock, restart postfix:
Starting postfix: [  OK  ]
master (pid  18179) is running...


echo 'izhp3d1faiqf6plidc20ysz' > /etc/hostname
hostname -F /etc/hostname


echo 'mail.humantrix.com' > /etc/hostname
hostname -F /etc/hostname

iz8vbav2lxypf6xsqqvrxvz  humantrix.com

tcpdump tcp port 25

tar -czf bak.tar.gz bak
tar -xf postfix-3.3.2.tar -C postfix-3.3.2
tar -zxf postfix-3.3.2.tar.gz -C source

压缩: 
tar -cvjpf etc.tar.bz2 /etc  //-c为创建一个打包文件,相应的-f后面接创建的文件的名称,使用了.tar.bz2后缀,-j标志使用bzip2压缩,最后面为具体的操作对象/etc目录
backup ubuntu:
# tar cvpzf Ubuntu.tgz --exclude=/proc --exclude=/lost+found --exclude=/Ubuntu.tgz --exclude=/mnt --exclude=/sys --exclude=/media --exclude=/home/bros/Downloads /

tar cvpzf catalina.out.tgz catalina.out

解压缩
tar zxvf fileNameHere.tgz
tar zxf Python-3.7.8.tgz
tar -xJf Python-3.7.8.tar.xz

rpm -qa|grep -i postfix
yum remove postfix-2.10.1-7.el7.x86_64
find / -name postfix

Add a User without Home Directory:
useradd -M shilpi
Add a User No Shell:
-s /usr/sbin/nologin
Create a User with Specific User ID:
useradd -u 999 navin

cat /etc/group
cat /etc/passwd

userdel postfix
groupdel postfix
groupdel postdrop

groupadd -g 12345 postfix
groupadd -g 54321 postdrop
useradd -u 12345 -g postfix -M -s /usr/sbin/nologin postfix

useradd -G {group-name} username
usermod -a -G {group-name} username

1、grep 'user1' /etc/group //找出用户组的gid
user1:x:1004://得出gid=1004
2、 awk -F":" '{print $1"\t"$4}' /etc/passwd |grep '1004' //列出user1组的所有成员

实时查看日志:
1. watch -n 1 aa.txt  #每个1秒显示aa.txt的内容
2. tail -f ***.log

> ls -alt # 按修改时间排序
> ls --sort=time -la # 等价于> ls -alt
> ls -alc # 按创建时间排序
> ls -alu # 按访问时间排序
 
# 以上均可使用-r实现逆序排序
> ls -alrt # 按修改时间排序
> ls --sort=time -lra # 等价于> ls -alrt
> ls -alrc # 按创建时间排序
> ls -alru # 按访问时间排序

-----------------------ubuntu install rpm package---------------------------------------
apt-get install rpm
apt-get install alien
alien *.rpm
dpkg -i *.deb
-----------------------ubuntu install rpm package---------------------------------------

查看操作系统版本。
cat /etc/redhat-release


命令被重写时,比如加了-i (提示用户); 编辑.bashrc注释掉相应行就行了
vi ~/.bashrc

创建空文件:
touch 文件名

1、压缩功能
安装 sudo apt-get install rar
卸载 sudo apt-get remove rar
2、解压功能
安装 sudo apt-get install unrar
卸载 sudo apt-get remove unrar
压缩解压缩.rar
解压:unrar x FileName.rar
压缩:rar a FileName.rar DirName

-----------------------linux---------------------------------------
当我们需要一次执行多个命令的时候,命令之间需要用连接符连接,不同的连接符有不同的效果。
(1) ; 分号,没有任何逻辑关系的连接符。当多个命令用分号连接时,各命令之间的执行成功与否彼此没有任何影响,都会一条一条执行下去。
(2) || 逻辑或,当用此连接符连接多个命令时,前面的命令执行成功,则后面的命令不会执行。前面的命令执行失败,后面的命令才会执行。
(3) && 逻辑与,当用此连接符连接多个命令时,前面的命令执行成功,才会执行后面的命令,前面的命令执行失败,后面的命令不会执行,与 || 正好相反。
(4) | 管道符,当用此连接符连接多个命令时,前面命令执行的正确输出,会交给后面的命令继续处理。若前面的命令执行失败,则会报错,若后面的命令无法处理前面命令的输出,也会报错。
-----------------------linux---------------------------------------


要杀掉所有的chromedriver进程 
ps aux | grep chromedriver | grep -v grep|awk '{print $2}' | xargs kill -9
ps aux | grep /usr/bin/google-chrome | grep -v grep|awk '{print $2}' | xargs kill -9

amd64 兼容 x86_64 i386

apt-get instal指定版本号
sudo apt-get install package=version
例如:sudo apt-get install samba=2:4.4.5+dfsg-2ubuntu6
sudo apt-get install libgbm1=19.2.8-0ubuntu0~19.10.2

-----------------------linux命令在后台运行---------------------------------------
1,命令后+ &
2,nohup 命令 &   按终端上键盘任意键退回到shell输入命令窗口,然后通过在shell中输入exit来退出终端
-----------------------linux---------------------------------------

CentOS 7.0默认使用的是firewall作为防火墙
firewall-cmd --state
systemctl stop firewalld.service
systemctl disable firewalld.service 

firewall-cmd --list-all

# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

firewall-cmd --query-port=22/tcp

启用某个服务
firewall-cmd --zone=public --add-service=https //临时
firewall-cmd --permanent --zone=public --add-service=https //永久
开启某个端口
firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp //永久
firewall-cmd --zone=public --add-port=8080-8081/tcp //临时
删除某个端口
firewall-cmd --permanent --zone=public --remove-port=8080-8081/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

systemctl stop firewalld 关闭防火墙
yum install iptables-services
systemctl enable iptables.service
reboot
重启后就出现了好多chain
添加规则后再添加一条到IN_public_allow这个chain里 eg,iptables -A IN_public_allow -p tcp --dport 50001 -j ACCEPT   #IN_public_allow 是自定义链


iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT
iptables -A INPUT -p udp --dport 1701 -j ACCEPT
iptables -t nat -A POSTROUTING -s 172.21.162.0/20 -o eth0 -j MASQUERADE

iptables -A INPUT -p tcp -m tcp --dport 52222 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 3050 -j ACCEPT
service iptables save


iptables -A INPUT -p tcp -m tcp --dport 8999 -j ACCEPT
service iptables save
service   iptables restart    / systemctl restart iptables

iptables -n -L

service iptables save
systemctl stop firewalld
systemctl disable  firewalld
systemctl start iptables
systemctl status iptables
systemctl enable iptables

kill = kill -15 , kill -9

netstat -ntlp
netstat -nulp

netstat -nltp | grep 8999

Linux使用nc命令测试服务器UDP端口是否被IDC运营商封堵:
#服务端运行 nc -l -u ip port
nc -l -u 121.0.99.68 1701
#客户端运行 nc -u ip port
nc -u 121.0.99.68 1701
#如果端口可用,没有被IDC或者运营商封堵那么在客户端输入任意字符,服务端会显示同样的字符。


ssh 到指定端口 ssh -p xx user@ip xx 为 端口号 user为用户名 ip为要登陆的ip


tcpdump udp port 53 
tcpdump tcp port 8080 -w tcpdump.cap

查看磁盘
df
df -T
df -H
查看文件夹占用空间大小
du -sh *
du -h --max-depth=1 /data/home/
du -sh ./
du -sh * | sort -n
du -sh * | sort -nr
du -sh * | sort -n | head -5
du -sh * | sort -n | tail -5


linux创建swap分区

dd if=/dev/zero of=/mnt/swapfile bs=1MB count=2048
mkswap /mnt/swapfile
swapon /mnt/swapfile

查看 vim /etc/rc.local 如果有 swapoff -a 修改为 swapon -a
设置自动挂载
vim /etc/fstab
/mnt/swapfile swap swap defaults 0 0
权限设置
chown root:root /mnt/swapfile
chmod 0600 /mnt/swapfile
查看 swappiness 值。
cat /proc/sys/vm/swappiness
如果为 0 ,则将其改为 10
sysctl vm.swappiness=10
永久设置 swappiness,如果该文件里没有,则追加 vm.swappiness = 10
vim /etc/sysctl.conf


1)创建大的swap文件
dd if=/dev/zero of=/mnt/swap bs=1MB count=2048
2)格式化swap空间
/sbin/mkswap /mnt/swap
3)关闭旧swap
swapoff /mnt/swapfile
4)开启新swap
/sbin/swapon /mnt/swap
5)开机挂载
vi /etc/fstab
/mnt/swap swap swap defaults 0 0

unzip -q nltk_data.zip
unzip -qo nltk_data.zip -d nltk_data

LInux上返回到切换目录前的目录
cd -

1、查看文本文件头部n行 
head -n 200 filename   #--200可替换为任一数字

curl -X POST -F 'username=davidwalsh' -F 'password=something' http://domain.tld/post-to-me.php
curl -X POST -H 'Content-Type: application/json' -d '{"username":"davidwalsh","password":"something"}' http://domain.tld/login
curl -X POST -F 'image=@/path/to/pictures/picture.jpg' http://domain.tld/upload

直接输入passwd就能改root密码

服务端启动一个udp的端口监听
nc  -ul  9998

客户端链接:
nc -vuz 47.74.65.123 1701
Connection to 47.74.65.123 1701 port [udp/l2f] succeeded!
nc -vuz 10.0.1.161 9998

l2tp VPN连接上,无法发送接受数据问题:
1,打开防火墙,iptables
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

查看进程的线程:
ps -T -p <pid>
top -H -p <pid>
Htop  F2菜单 F10退出


只删除nginx这个包,不会删除依赖包
rpm -e --nodeps nginx

查看默认服务替换:
 alternatives --config mta
alternatives --display mta

会话中断后接着编辑:
 vim -r xxx.py

rpm -aq|grep openssl

vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/lib64/firebird/bin

source ~/.bash_profile

yum list installed | grep openssl
yum search openssl

strings命令在对象文件或二进制文件中查找可打印的字符串:
strings a.out | grep xxx


mount -o loop  /data/kvm-pool/windows7.img /media/B/ 

fdisk -l /data/kvm-pool/windows7.img
mount -o loop,offset=$((206848*512)) /data/kvm-pool/windows7.img /media/B/ 


losetup [ -e encryption ] [ -o offset ] loop_device file  ||  losetup [-d][-e <加密方式>][-o <平移数目>][循环设备代号][文件]
查看空的 loop设备:   loop_device 循环设备名,在 linux 下如 /dev/loop0 , /dev/loop1 等; 循环设备可把文件虚拟成区块设备,籍以模拟整个文件系统,让用户得以将其视为硬盘驱动器,光驱或软驱等设备,并挂入当作目录来使用。
losetup -f
-d 卸载设备

eg:
dd if=/dev/zero of=floppy.img bs=512 count=2880
mkfs.ext4 floppy.img
file floppy.img
losetup /dev/loop1 floppy.img
mount /dev/loop0 /tmp
umount /tmp
losetup -d /dev/loop1

mount 命令的 -o loop 选项可以将任意一个 loopback 文件系统挂载:
mount -o loop loopfile.img /mnt/loopback
相当下面两条命令
# losetup /dev/loop0 loopfile.img
# mount /dev/loop0 /mnt/loopback
创建一个硬盘文件,然后对该文件进行分区,接着挂载其中一个子分区:
# losetup /dev/loop1 loopfile.img
# fdisk /dev/loop1

制作目录镜像
mirrordir 

制作光盘镜像
mkisofs -o *.iso 文件夹
mount -o loop *.iso /mnt/mydir

docker挂载本地目录
docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash
docker run -d -v /home/dock/Downloads:/usr/Downloads --name ubuntu1 ubuntu64

apt-get remove   *   不删除依赖包
apt-get autoremove *   删除依赖包


在Ubuntu上,iptables不是服务。为了停止它,您必须执行以下操作:
sudo iptables-save > /root/firewall.rules
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
为了恢复您以前的规则: (加载配置策略生效)
iptables-restore < /root/firewall.rules


 

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值