linux常用命令

linux特点: 多用户,多任务,安全系数高,稳定

ctrl+w撤销输入

用户管理

查看用户:

创建用户: useradd 用户名

修改用户密码: passwd 用户名

删除用户: userdel

修改用户所属组:

切换用户switch user: su 用户名

普通用户切换为root: su

修改自己的密码: passwd

文件管理

创建含有子目录的文件夹: mkdir a/b -p

备份 cp xxx.txt xxx.bak

删除文件: rm -i带提示

删除指定文件之外的所有文件: rm -rf `ls | grep -v demo.txt`

查看内容:

  1. cat # -b显示行号(不包含空白行) -n也显示行号(包含空白行) , 显示行号查看内容还有nl命令

  2. tac 从最后一行开始显示

  3. more

  4. less

    总结下more 和 less的区别:
    \1. less可以按键盘上下方向键显示上下内容,more不能通过上下方向键控制显示
    \2. less不必读整个文件,加载速度会比more更快

    \3. less退出后shell不会留下刚显示的内容,而more退出后会在shell上留下刚显示的内容

  5. head查看前n行,n默认为10

    head -n 2 a #查看a文件的前2行
    head -2 a #简写
    
  6. tail查看后n行,n默认为10

    tail -n 2 a #查看a文件的后2行
    tail -f 文件名 #实时更新查看最后文件的n行内容,比如查看日志
    
  7. sed -n ‘1,5p’ 1.txt 显示1.txt文件1到5行内容

  8. stat 文件名

    查看文件信息

查找文件:

find [查找路径] [查查找方式] [查找条件]

find . -name '*.txt' #查找当前路径下(包括下级目录)所有的.txt文件与目录
find . -iname #不区分大小写
find . -name "t*" -type f #查找易t开头的文件
find . -name "t*" -type d #查找易t开头的目录

-size 数字[bcwkMG]
-size 3M #查找大小为3M的
-size -4k #查找小于4kB的
-size +4k #查找大于4kB的

-ctime 天数 #查找3天的
-cmin 分钟

-maxdepth 查找深度

find -name "a" -exec cp {} b \; #找到的a复制为b

history: 查看输入过的命令

用户权限

chmod u-x,g+x,o+x 文件
chmod u=rwx 文件
chmod a=rwx 文件
chmod 644 文件
rwx 二进制: 000-111 十进制: 0-7

执行文件

source xxx.sh 或 . xxx.sh, 不需要文件有可执行权限, 通过 ./xxx.sh执行需要可执行权限.

打包压缩

c:create f:file z:gzip x:extract v:verbose啰嗦模式
打包压缩为gzip: 
tar -czf xxx.tar.gz file...
解压:
tar -xvf file.tar //解压 tar包
tar -xzvf file.tar.gz //解压tar.gz
tar -xzvf new.tar.gz  -C ./b/ #解压到./b/目录下
查看包内容:
tar -tf xxx.tar
tar -tf xxx.tar.gz    #或-tzf
增加文件:
#这条命令是将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思
tar -rf all.tar *.gif 

将找到的文件打包:

#方式1
find -type f -name "test*" | xargs -exec tar -czvf test.tar.gz
#方式2

#方式3
tar -czvf test.tar.gz `find -type f -name "test*" `

多命令执行

| #管道
ls;pwd;date #按顺序执行,互不影响
ls&&pwd #ls执行正确之后,pwd才能执行
ls||pwd #ls不成功则pwd执行;ls成功,pwd不执行(短路).也就是,只有一个命令执行

链接

硬链接: ln 源文件 链接文件

软连接:ln -s 源文件 链接文件

目录不能创建硬链接,为什么不能,看http://c.biancheng.net/linux/ln.html.删除源文件,硬链接还在,软链接找不到源文件而失效.

查看占用空间

查看文件/目录占的空间

du #查看当前目录
du -h #以human可读形式输出
du /home/ #查看 /home
du a #查看a

查看文件系统,分区,磁盘

f: filesystem

[root@localhost ~]# df -h   #diskfree?
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  485848       0   485848    0% /dev
tmpfs                     497836       0   497836    0% /dev/shm
tmpfs                     497836    7804   490032    2% /run
tmpfs                     497836       0   497836    0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 1649944 16161512   10% /
/dev/sda1                1038336  140280   898056   14% /boot
tmpfs                      99568       0    99568    0% /run/user/0

获取键盘输入

#一个变量
[root@localhost ~]# read name
123
[root@localhost ~]# echo $name
123

#多个变量
[root@localhost ~]# read a1 a2
h k
[root@localhost ~]# echo $a1
h
[root@localhost ~]# echo $a2
k

网络

centos常用网络命令

https://www.cnblogs.com/lakeslove/p/7234914.html

#临时设置
ifconfig 网卡名 新ip地址
ifconfig eth0 192.168.0.99
#禁用网卡
ifdown 网卡名
ifdown lo
ifdown eth0
#启用网卡
ifup 网卡名

ping可以检查网络是否通,以及响应速度

ping -c 4  127.0.0.1  #(linux系统)ping 4次

网络状态

旧版使用netstat
新版使用ss
参数: 
-t, --tcp           display only TCP sockets
-u, --udp           display only UDP sockets
-l, --listening     display listening sockets  #显示正在监听(正在使用)的端口
-p, --processes     show process using socket
-a, --all           display all sockets
-n, --numeric       don't resolve service names 让端口显示为数字,如ssh没有-n, Port显示为ssh,有-n显示为22
#常用写法
[root@localhost ~]# ss -tunl
Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port        
udp    UNCONN     0      0      127.0.0.1:323                        *:*
udp    UNCONN     0      0              *:68                         *:*
udp    UNCONN     0      0          [::1]:323                     [::]:*
tcp    LISTEN     0      128            *:22                         *:*
#查看端口是否被占用
ss -tunl | grep 80

ss -anp

服务

service [start, stop, restart, try-restart, reload, force-reload, status)] 服务名
更多操作使用systemctl
查看防火墙
systemctl  status firewalld 或 service firewalld status #新
systemctl  status iptables 或 service iptables status #旧

自启动管理

[root@localhost ~]# chkconfig

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。

设置自启动
chkconfig 服务名 on 
关闭自启动
chkconfig 服务名 on 

下载文件

curl -O http://cn.wordpress.org/wordpress-3.1-zh_CN.zip
wget   http://cn.wordpress.org/wordpress-3.1-zh_CN.zip

抓包

监视指定主机的数据包
https://blog.csdn.net/u011630575/article/details/48271711
tcpdump host 地址
tcpdump -i eth0 host www.baidu.com #监听网卡eth0, www.baidu.com

请求

curl 地址 #get
curl -d "参数" 地址 #地址

进程

查看进程

ps (Process Status)

#参数说明
-A, -e               all processes
-f                   full-format, including command lines
====================================================
[root@localhost Desktop] ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 08:47 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize
root          2      0  0 08:47 ?        00:00:00 [kthreadd]
=====================================================

#参数说明
a                   all with tty, including other users
u                   user-oriented format
x                   processes without controlling ttys
[root@localhost Desktop] ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.6 128180  6800 ?        Ss   08:47   0:02 /usr/lib/systemd/systemd --switched-root --syste
root          2  0.0  0.0      0     0 ?        S    08:47   0:00 [kthreadd]

2. ps aux 和ps -ef
两者的输出结果差别不大,但展示风格不同。aux是BSD风格,-ef是System V风格。这是次要的区别,一个影响使用的区别是aux会截断command列,而-ef不会。当结合grep时这种区别会影响到结果。

杀进程

kill 进程编号
kill -9 进程编号 #强制杀死
pkill 进程名

安装与卸载

安装
rpm -ivh rpm包
yum install xxx #在线安装
升级
tmp -Uvh rpm包

卸载
rpm -e 软件包

centos开启mariadb

systemctl start mariadb

ls -l 等同于 ll

awk, sed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值