linux基础命令2

1.alias
  设置和查看别名
  例子1:查看别名

    [root@kuying3 ~]# alias
    alias cp=’cp -i’
    alias egrep=’egrep –color=auto’
    alias fgrep=’fgrep –color=auto’
    alias grep=’grep –color=auto’
    alias l.=’ls -d .* –color=auto’
    alias ll=’ls -l –color=auto’
    alias ls=’ls –color=auto’
    alias mv=’mv -i’
    alias rm=’rm -i’
  例子2:设置别名

    [root@kuying3 ~]# alias kuying=”ls -l”‘
    [root@kuying3 ~]# kuying
    total 10852
    -rw——-. 1 root root 1246 Apr 4 06:53 anaconda-ks.cfg
    -rw-r–r– 1 root root 11101732 Jan 25 14:51 apache-tomcat-9.0.14.zip
    -rw-r–r– 1 root root 78 Jul 8 11:37 hello_w.sh
    drwxr-x— 2 root root 24 Jul 8 11:10 logs
    -rw-r–r– 1 root root 0 Jun 18 19:18 test

2. unalias
  删除别名
    [root@kuying3 ~]# unalias rm
    注意:以后在接手一台服务器的时候要看一下已经设置了哪些别名!

3.uname
  查看主机信息
  -a 显示所有的信息
  -r 显示内核版本号

  例子1:查看主机信息
    [root@kuying3 ~]# uname -a
    Linux kuying3 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  例子2:查看内核版本号
    [root@kuying3 ~]# uname -r
    3.10.0-862.el7.x86_64

4. hostname
  查看和设置主机名
    例子1:查看主机名
    [root@kuying3 ~]# hostname
    kuying3

  例子2:设置主机名
    [root@kuying3 ~]# hostname kuying
    注意:hostname修改的主机名是立即生效但是重启系统就失效了
    如果不想开机失效的话可以写入到配置文件/etc/hostname

    [root@kuying ~]# echo “kuying” > /etc/hostname
    [root@kuying ~]# cat /etc/hostname
    kuying

5.hostnamectl
    设置主机名
    设置的主机名立即生效而且重启系统依然有效
    例子1:设置主机名立即且永久有效
    [root@kuying ~]# hostnamectl set-hostname kuying1

6. history
  显示执行过的命令
    [root@kuying1 ~]# history
  特殊用法:
    [root@kuying1 ~]# !c #c可以为任意字母

7.which
  显示命令的完整路径
  命令都是一个可以执行的文件
    [root@kuying1 ~]# which ls
    /usr/bin/ls
    [root@kuying1 ~]# ls -l /usr/bin/ls
    -rwxr-xr-x. 1 root root 117672 Apr 11 2018 /usr/bin/l
  可以查看命令依赖于那个程序!

8.wc
  统计文本信息
  -l 统计行数
  -w 单词数
  -c 统计字符
  例子1:统计当前系统中有多少用户
    [root@kuying1 ~]# wc -l /etc/passwd
    20 /etc/passwd
  例子2:统计单词数
    [root@kuying1 ~]# echo “this is kuying” | wc -w
    3
  例子3:统计字符
    [root@kuying1 ~]# echo “this is kuying” | wc -c
    12
    [root@kuying1 ~]# printf “this is kuying” | wc -c
    11

9. w
  显示当前登录的用户在做什么
    [root@kuying1 ~]# w
    23:08:37 up 9 min, 3 users, load average: 0.00, 0.01, 0.01
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    root pts/0 192.168.163.1 22:59 5.00s 0.36s 0.01s w
    root pts/1 192.168.163.1 23:08 6.00s 0.01s 0.01s -bash
    root pts/2 192.168.163.1 23:08 1.00s 0.00s 0.00s -bash
  主要关注:
    登录用户
    登录IP

10. who
  显示当前登录的用户
    [root@kuying1 ~]# who
    root tty1 2019-07-09 23:11
    root pts/0 2019-07-09 23:12 (192.168.163.1)
    root pts/1 2019-07-09 23:08 (192.168.163.1)
    root pts/2 2019-07-09 23:08 (192.168.163.1)

11.whoami
  显示当前登录的用户
    [root@kuying1 ~]# whoami
    root

12. ping
  测试主机是否在线
    -c 发送包的数量
    -i 发送包的间隔数
    -w 超时等待时间
  例子1:测试本机是否可以通外网
    [root@kuying245 ~]# ping baidu.com
    PING baidu.com (123.125.114.144) 56(84) bytes of data.
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=52 time=33.1 ms
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=52 time=33.1 ms
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=52 time=33.1 ms
  例子2:发送两个包
    [root@kuying245 ~]# ping -c 2 baidu.com
    PING baidu.com (123.125.114.144) 56(84) bytes of data.
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=52 time=33.1 ms
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=52 time=33.1 ms

    — baidu.com ping statistics —
    2 packets transmitted, 2 received, 0% packet loss, time 1001ms
    rtt min/avg/max/mdev = 33.145/33.148/33.151/0.003 ms
  例子3:指定发送包的间隔时间为3秒钟
    [root@kuying245 ~]# ping -c 3 -i 5 baidu.com
    PING baidu.com (123.125.114.144) 56(84) bytes of data.
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=52 time=33.1 ms
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=52 time=33.1 ms
    64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=52 time=33.1 ms

    — baidu.com ping statistics —
    3 packets transmitted, 3 received, 0% packet loss, time 10010ms
    rtt min/avg/max/mdev = 33.127/33.134/33.145/0.007 ms
  例子4:指定超时等待时间
    [root@kuying245 ~]# ping -w 1 google.com
    PING google.com (172.217.24.14) 56(84) bytes of data.

    — google.com ping statistics —
    2 packets transmitted, 0 received, 100% packet loss, time 999ms

13.kill
  终止进程
    -9 强制终止进程
    -15 默认是-15 不是立即退出。完成自己的工作后退出!
  格式:
    kill -9 pid
  例子1:关掉进程
    kill -9 1818

14.du
  查看文件和目录的大小
    -h 人类易读的方式显示文件大小
    -s 目录的总大小
  注意:
    du查看的是文件占用的block块大小
    ls -lh才是文件的实际大小

  例子1:
    [root@kuying1 ~]# du -h anaconda-ks.cfg
    4.0K anaconda-ks.cfg

    [root@kuying1 ~]# ls -lh anaconda-ks.cfg
    -rw——-. 1 root root 1.3K Apr 4 06:53 anaconda-ks.cfg
  例子2:查看目录总大小
    [root@kuying1 ~]# du -sh /etc
    31M /etc

15.df
  显示查看磁盘的大小及挂载点
    -h 人类易读的方式显示文件大小
  例子1:
    [root@kuying1 ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/centos-root 17G 1.3G 16G 8% /
    devtmpfs 224M 0 224M 0% /dev
    tmpfs 236M 0 236M 0% /dev/shm
    tmpfs 236M 5.6M 230M 3% /run
    tmpfs 236M 0 236M 0% /sys/fs/cgroup
    /dev/sda1 1014M 130M 885M 13% /boot
    /dev/sr0 4.2G 4.2G 0 100% /mnt
    tmpfs 48M 0 48M 0% /run/user/0

16.free
  显示内存信息
    -h 人类易读的方式
    -m 以M单位显示
  例子1:
    [root@kuying1 ~]# free -h
    total used free shared buff/cache available
    Mem: 470M 83M 255M 5.5M 131M 341M
    Swap: 2.0G 0B 2.0G

17.date
  显示和设置系统时间
  例子1:查看时间
    [root@kuying1 ~]# date
    Tue Jul 9 23:54:29 CST 2019
  例子2:设置时间
    [root@kuying1 ~]# date -s “2019-7-9 15:54:00”
    Tue Jul 9 15:54:00 CST 2019
    [root@kuying1 ~]# date
    Tue Jul 9 15:54:01 CST 2019

  例子3:格式化输出时间
    %Y 年
    %m 月
    %d 天
    %H 小时
    %M 分钟
    %S 秒

    [root@kuying1 ~]# date “+%Y/%m/%d %H:%M:%S”
    2019/07/09 15:55:55
  例子4:
    %F 年月日
    %T 时分秒
    [root@kuying1 ~]# date “+%F %T”
    2019-07-09 15:57:23

 

绝对路径vs相对路径
  在Linux系统中另外还有一个重要的概念—路径。
  路径指的是如何定位到某个文件,分为绝对路径与相对路径。
  绝对路径指的是从根目录(/)开始写起的文件或目录名称
  相对路径则指的是相对于当前路径的写法。

输出重定向
  >:覆盖输出,会覆盖掉原先的文件内容
  >>:追加输出,不会覆盖原始文件内容,会在原始内容末尾继续添加
  2>:错误输出,会覆盖掉原先的文件内容
  2>>:错误追加输出,会覆盖掉原始文件内容,会在原始内容末尾继续添加
  &>:将标准输出与错误输出共同写入到文件中。覆盖原有内容
  &>>:将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)

  例子1:覆盖输出
    [root@kuying1 ~]# echo “222” > test
    [root@kuying1 ~]# cat test
    222
  例子2:追加输出
    [root@kuying1 ~]# echo “222” >> test
    [root@kuying1 ~]# echo “222” >> test
    [root@kuying1 ~]# echo “222” >> test
    [root@kuying1 ~]# echo “222” >> test
    [root@kuying1 ~]# cat test
    222
    222
    222
    222
  例子3:错误覆盖
    [root@kuying1 ~]# kuying 2> test
    [root@kuying1 ~]# kuying 2> test
    [root@kuying1 ~]# kuying 2> test
    [root@kuying1 ~]# kuying 2> test
    [root@kuying1 ~]# kuying 2> test
    [root@kuying1 ~]# cat test
    -bash: kuying: command not found
  例子4:错误追加
    [root@kuying1 ~]# kuying 2>> test
    [root@kuying1 ~]# kuying 2>> test
    [root@kuying1 ~]# kuying 2>> test
    [root@kuying1 ~]# kuying 2>> test
    [root@kuying1 ~]# kuying 2>> test
    [root@kuying1 ~]# cat test
    -bash: kuying: command not found
    -bash: kuying: command not found
    -bash: kuying: command not found
    -bash: kuying: command not found
    -bash: kuying: command not found
    -bash: kuying: command not found

    总结:只要是一个>都是覆盖,
    只要是两个>>都是追加,无论是正确还是错误输出
  例子5:无论是错误的还是正确的都输出倒文件中
    [root@kuying1 ~]# ls &>> test
    [root@kuying1 ~]# cat test
    2
    anaconda-ks.cfg
    test
    test2
    -bash: kuying: command not found
    2
    anaconda-ks.cfg
    test
    test2
输入重定向
  <
    [root@kuying1 ~]# wc -l < /etc/passwd
    23

管道符

    管道命令符(|)的作用是把前一个命令原本要输出到屏幕的标准正常数据当作是后一个命令的标准输入。
    一条命令中管道符可以使用多个,只要前面的命令有输出结果,管道符后面的命令即可再执行!

  例子1:
    [root@kuying1 ~]# cat /etc/passwd | wc -l
    23
  例子2:
    [root@kuying245 ~]# cat /var/log/nginx/access.log | awk ‘{print $1}’ | sort | uniq -c | sort -rn | head -3
    501 61.148.243.143
    118 114.242.248.85
    110 122.71.224.158

通配符
    * 表示匹配的任意内容,可以是0个也可以是很多个字符
    ? 表示匹配单个字符,必须存在
    [] 表示匹配括号里面的任意一个字符
  例子1:找到所有以/dev/sda开头的文件
    [root@kuying1 ~]# ls /dev/sda*
    /dev/sda /dev/sda1 /dev/sda2 /dev/sdakuying
  例子2:找到以/dev/sda开头并且后面跟一个字符的文件
    [root@kuying1 ~]# ls /dev/sda?
    /dev/sda1 /dev/sda2
  例子3:找到以/dev/sda开头并且后面跟一个数字的文件
    [root@kuying1 ~]# ls /dev/sda[0-9]
    /dev/sda1 /dev/sda2
  例子4:找到以/dev/sda开头并且后面跟一个字母的文件
    [root@kuying1 ~]# ls /dev/sda[a-z]
    /dev/sdan
  例子5.
    [root@kuying1 ~]# ls /dev/sda*
    /dev/sda /dev/sda1 /dev/sda2 /dev/sdakuying /dev/sdan
    [root@kuying1 ~]# ls /dev/sda[kuying]
    /dev/sdan
  注意:不要把中括号里面的字符当做一个整体,二是匹配中括号里面的单个字符
  例子6:删除当前目录中所有以test开头的文件
    [root@kuying1 ~]# ls
    2 anaconda-ks.cfg test test1 test2
    [root@kuying1 ~]# rm -rf test*
    [root@kuying1 ~]# ls
    2 anaconda-ks.cfg

posted @ 2019-07-11 17:42  kuying  阅读(342)  评论(0编辑  收藏  举报