Linux常用命令

第零章 绪论

以下命令都是基于 Centos7

第一章 Linux 常用目录结构

在这里插入图片描述

第二章 Linux 常用基本命令

2.1 文件查看

  • pwd (显示当前路径)
  • whoami (显示自身用户名称)
  • ls
  • ll(ls -l缩写)列出当前目录下的文件
  • ll -a 列出当前目录下的所有文件(包括隐藏文件)
  • which 查找文件(一般用来查找文件或命令所处位置)
[root@bigdata01 ~]# pwd
/root
[root@bigdata01 ~]# whoami
root
[root@bigdata01 ~]# ls
abx  anaconda-ks.cfg  hello-bak.txt  hello.txt  history.txt  hlink  num2.txt  num.txt  shell  test.txt  vlink
[root@bigdata01 ~]# ll -a
total 56
dr-xr-x---.  5 root root 4096 Dec 27 14:21 .
dr-xr-xr-x. 18 root root  236 Dec 27 18:59 ..
drwxr-xr-x.  4 root root   49 Dec 25 16:49 abx
-rw-------.  1 root root 1243 Dec 25 13:14 anaconda-ks.cfg
-rw-------.  1 root root 6832 Dec 29 08:47 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
-rw-r--r--.  1 root root    0 Dec 25 16:21 hello-bak.txt
-rw-r--r--.  1 root root  232 Dec 27 13:20 hello.txt
-rw-------.  1 root root  704 Dec 25 16:46 history.txt
-rw-r--r--.  1 root root    0 Dec 25 15:24 hlink
-rw-r--r--.  1 root root   15 Dec 27 10:22 num2.txt
-rw-r--r--.  1 root root   11 Dec 27 10:19 num.txt
drwxr-xr-x.  2 root root  224 Dec 27 18:55 shell
drwx------.  2 root root   25 Dec 25 16:26 .ssh
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
-rw-r--r--.  1 root root   34 Dec 27 10:34 test.txt
lrwxrwxrwx.  1 root root    9 Dec 25 16:17 vlink -> hello.txt
[root@bigdata01 ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@bigdata01 ~]# which pwd
/usr/bin/pwd

2.2 创建,重命名文件\文件夹

  • touch
  • mkdir
  • mkdir -p 目标目录不存在也不报错
  • mv
[root@bigdata04 shell]# mkdir -p test
[root@bigdata04 shell]# ll
total 4
-rwxr--r--. 1 root root 30 Dec 27 14:46 hello.sh
drwxr-xr-x. 2 root root  6 Dec 29 10:42 test
[root@bigdata04 shell]# cd test
[root@bigdata04 test]# touch text.txt
[root@bigdata04 test]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 29 10:42 text.txt
[root@bigdata04 test]# mv test.txt newtest.txt
mv: cannot stat ‘test.txt’: No such file or directory
[root@bigdata04 test]# mv text.txt newtext.txt  
[root@bigdata04 test]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 29 10:42 newtext.txt

2.3 链接文件

  • 硬链接;硬链接相当于对原始文件的一个复制,不能对目录使用硬链接
    • ln hello.txt hlink
  • 软链接;类似windows的快捷方式,主要用于节省磁盘空间
    • ln -s hello.txt vlink
[root@bigdata04 test]# ln newtext.txt newhlink
[root@bigdata04 test]# ll
total 0
-rw-r--r--. 2 root root 0 Dec 29 10:42 newhlink
-rw-r--r--. 2 root root 0 Dec 29 10:42 newtext.txt
[root@bigdata04 test]# ln -s newtext.txt newvlink
[root@bigdata04 test]# ll
total 0
-rw-r--r--. 2 root root  0 Dec 29 10:42 newhlink
-rw-r--r--. 2 root root  0 Dec 29 10:42 newtext.txt
lrwxrwxrwx. 1 root root 11 Dec 29 10:45 newvlink -> newtext.txt
[root@bigdata04 test]# 

2.4 目录切换

  • 当前目录 .
  • 切换目录 cd
  • 进入当前家目录 cd ~
[root@bigdata04 test]# cd .
[root@bigdata04 test]# pwd
/root/shell/test
[root@bigdata04 test]# cd ~
[root@bigdata04 ~]# pwd
/root
[root@bigdata04 ~]# 

2.5 删除文件\文件夹

  • 删除文件 rm
  • 删除目录 rm -r
  • 强制删除 rm -f
  • 递归删除目录及其文件 rm -rf

2.6 复制\粘贴\剪切

  • 复制文件 cp hello.txt hello_bak.txt
  • 复制粘贴文件或目录 cp -r
  • 移动或剪切目录 mv

2.7 远程复制

  • scp
  • scp -v 显示复制进度
  • scp -r 复制目录
  • scp -q 静默复制
scp -rq /root/hello.txt 192.168.182.130:/root

2.8 文件属性

2.9 更改文件权限

  • chmod
  • r 4 w 2 X 1
  • chmod u+x

2.10 内容查看

  • more 分页显示,回车显示下一行 b 显示下一页 空格显示下一页 q 推出
  • head 显示文件前几行
  • tail -f 常用于查阅正在改变的日志文件,一般用来监控
  • cat -b 显示行号查看
[root@bigdata04 ~]# cat -b anaconda-ks.cfg 
     1  #version=DEVEL
     2  # System authorization information
     3  auth --enableshadow --passalgo=sha512
     4  # Use CDROM installation media
     5  cdrom
     6  # Use graphical install
     7  graphical
     8  # Run the Setup Agent on first boot
     9  firstboot --enable
    10  ignoredisk --only-use=sda
    11  # Keyboard layouts
    12  keyboard --vckeymap=us --xlayouts='us'
    13  # System language
    14  lang en_US.UTF-8

2.11 压缩解压

  • 压缩 tar -zcvf abc.tar.gz abc
  • 解压 tar -zxvf abc.tar.gz

2.12 输出及显示

  • echo

  • echo -e 解析转义字符

    [root@bigdata04 test]# echo -e "666\nabc\n77" >echotest1.txt 
    [root@bigdata04 test]# cat echotest1.txt 
    666
    abc
    77
    
  • echo $PATH 输出环境变量

2.13 软件安装和卸载

  • yum install -y 安装
  • yum update 不跟则更新全部
  • yum info 显示包信息
  • yum list 不跟则显示已安装或可安装包
  • yum remove 删除程序
  • yum clear all 清除所有缓存

2.14 帮助信息

  • --help
[root@bigdata04 ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

With no FILE, or when FILE is -, read standard input.

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cat invocation'

2.15 查看操作历史

  • history
    • history -w history.txt 保存输过的命令

2.16 磁盘使用情况

  • df
[root@bigdata04 ~]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   919532       0    919532   0% /dev
tmpfs                      931536       0    931536   0% /dev/shm
tmpfs                      931536    9696    921840   2% /run
tmpfs                      931536       0    931536   0% /sys/fs/cgroup
/dev/mapper/centos-root  17811456 2198348  15613108  13% /
/dev/sda1                 1038336  152644    885692  15% /boot
tmpfs                      186308       0    186308   0% /run/user/0

2.17 清屏命令

  • clear

2.18 查看内存使用情况

  • free -m 显示内存的单位为MB
  • free -h 根据值的大小,显示易于识别的单位
[root@bigdata04 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         147         590           9        1081        1510
Swap:          2047           0        2047
[root@bigdata04 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        147M        590M        9.5M        1.1G        1.5G
Swap:          2.0G          0B        2.0G
[root@bigdata04 ~]# 

2.19 关机重启快捷命令

  • 重启 reboot -h now
  • 关机 shutdown -h now
  • 退出 exit

2.20 文件查找

  • 准确查找文件名
  • 模糊查询文件名 (使用通配符)
[root@bigdata04 test]# find / -name '*newtext*'
/root/shell/test/newtext.txt
[root@bigdata04 test]# find / -name newhlink
/root/shell/test/newhlink

Linux 查找命令博文 :https://blog.csdn.net/xxmonstor/article/details/80507769

2.21 文件快速查找替换

  • sed grep
sed -i "s/oldString/newString/g" `grep oldString -rl /path`
# sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl /usr/aa`
# sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl ./`

2.22 目录常用命令

  • cd: 切换到 /usr/local/bin 目录。
cd /usr/local/bin
  • pwd: 显示当前所在目录。
pwd
  • ls: 列出当前目录下的文件和子目录。
ls
  • mkdir: 创建新的目录。
mkdir mydir
  • rmdir: 删除空目录。
rmdir mydir
  • rm -r: 删除目录及其所有内容。
rm -r mydir
  • cp -r: 复制目录及其所有内容。
cp -r mydir mydir_backup
  • mv: 移动或重命名目录。
mv mydir mydir_newname
  • tree: 以树形结构显示目录及其子目录。
tree /usr/local
  • du: 显示目录的磁盘使用情况。
du -h mydir
  • df: 显示文件系统的磁盘使用情况。
df -h
  • find: 在目录中查找文件或目录。
find /usr/local -name myfile.txts

find命令和locate命令都是用于在Linux系统中查找文件和目录的命令,但它们有一些区别。

  1. find命令需要指定搜索的路径,而locate命令搜索的是系统中预先建立好的数据库,所以locate命令速度更快。

  2. locate命令只能搜索文件名,而不能搜索文件内容,而find命令可以搜索文件名、文件内容、文件大小、修改时间等。

  3. find命令的搜索结果是实时的,而locate命令的搜索结果是基于数据库的,需要定期更新。

  4. find命令可以使用更多的搜索条件,比如按照文件类型、权限、所有者等搜索,而locate命令只能按照文件名搜索。

综上所述,如果需要搜索文件名以外的其他属性或需要实时更新搜索结果,那么使用find命令更为合适;如果只需要搜索文件名,且希望搜索速度更快,那么使用locate命令比较合适。

find命令详解

  • locate: 快速搜索文件或目录。
locate myfile.txt
  • ln -s: 创建符号链接。
ln -s /usr/local/myfile.txt myfile_link
  • tar: 打包和压缩目录。
tar -czvf mydir.tar.gz mydir

2.23Centos中的用户和组

CentOS 中,用户组和文件权限都是用于管理文件和目录访问权限的重要概念。下面分别进行详细解释。

  1. 用户组

用户组是指一组用户的集合,可以将多个用户归为同一组,便于管理和授权。在 CentOS 中,每个用户可以属于一个或多个用户组,用户组可以拥有自己的权限。

  • 创建用户组:
groupadd group_name
  • 将用户添加到用户组:
usermod -aG group_name user_name
  • 查看用户所属的用户组:
groups user_name
  1. 文件权限

文件权限是指对文件或目录的访问权限控制,包括读取、写入和执行等操作。在 CentOS 中,文件权限由文件所有者、所属用户组和其他用户三个方面进行控制。

文件权限的表示方法:

-rwxrwxrwx

其中,第一个字符表示文件类型,后面三组字符分别表示文件所有者、所属用户组和其他用户的权限。每组字符包括读(r)、写(w)和执行(x)三个权限,用横线(-)表示无权限。

修改文件权限:

chmod [options] mode file_name

例如,将文件 file.txt 的所有者和所属用户组的读、写和执行权限设置为可读可写可执行,其他用户的权限设置为只读,可以使用以下命令:

chmod 744 file.txt

其中,7 表示读、写和执行权限,4 表示只读权限。

除了 chmod 命令,还可以使用 chown 命令和 chgrp 命令修改文件的所有者和所属用户组。例如,将文件 file.txt 的所有者修改为 user1,所属用户组修改为 group1,可以使用以下命令:

chown user1:group1 file.txt

综上所述,用户组和文件权限是 CentOS 中管理文件和目录访问权限的重要概念,了解和掌握它们的使用方法对于系统管理员和用户都非常重要。

2.24 man 和 --help区别

CentOS 中,man 和 --help 都是用于获取命令帮助信息的方式,但它们有一些区别。

  1. man:man 是 manual 的缩写,是 Linux 下的手册页,可以查看系统中安装的命令、函数、配置文件等的详细说明。man 命令一般提供了完整的命令用法、参数、示例等详细信息,是一种非常全面的命令帮助方式。

例如,要查看 ls 命令的手册页,可以使用以下命令:

man ls
  1. –help:–help 是命令行参数,大多数 Linux 命令都支持 --help 参数,用于获取命令的简要帮助信息。–help 参数一般只提供了命令的基本用法和参数列表,没有详细的示例和说明,但足以满足大多数用户的需求。

例如,要查看 ls 命令的简要帮助信息,可以使用以下命令:

ls --help

综上所述,如果需要查看命令的详细说明、示例和其他相关信息,可以使用 man 命令;如果只需要获取命令的基本用法和参数列表,可以使用 --help 参数。

2.25 Linux中常见的网络命令

  1. ping:用于测试网络连接是否正常,可以测试目标主机是否可达、网络延迟等信息。举例:ping www.baidu.com
  2. traceroute:用于跟踪数据包在网络中的路径,可以查看数据包经过的路由器和延迟时间等信息。举例:traceroute www.baidu.com
  3. netstat:用于查看当前系统中开放的端口和被占用的端口,可以查看网络连接状态和进程信息等。举例:netstat -anp
  4. nslookup:用于查询域名对应的IP地址,可以查看DNS解析结果和网络连接状态等。举例:nslookup www.baidu.com
  5. ifconfig:用于查看网络接口的配置信息,可以查看IP地址、子网掩码、网关等信息。举例:ifconfig
  6. route:用于查看和修改路由表,可以查看网络路由信息和修改默认路由等。举例:route -n
  7. ssh:用于远程登录到其他主机,可以在远程主机上执行命令和管理文件等。举例:ssh user@192.168.1.1

2.26 Windows系统中常用的网络命令

  1. ping:用于测试网络连接是否正常,可以测试目标主机是否可达、网络延迟等信息。举例:ping www.baidu.com

  2. tracert:用于跟踪数据包在网络中的路径,可以查看数据包经过的路由器和延迟时间等信息。举例:tracert www.baidu.com

  3. netstat:用于查看当前系统中开放的端口和被占用的端口,可以查看网络连接状态和进程信息等。举例:netstat -ano

  4. nslookup:用于查询域名对应的IP地址,可以查看DNS解析结果和网络连接状态等。举例:nslookup www.baidu.com

  5. ipconfig:用于查看网络接口的配置信息,可以查看IP地址、子网掩码、网关等信息。举例:ipconfig /all

  6. route print:用于查看和修改路由表,可以查看网络路由信息和修改默认路由等。举例:route print

  7. telnet:用于远程登录到其他主机,可以在远程主机上执行命令和管理文件等。举例:telnet 192.168.1.1 80

2.27 Linux命令在线查询

Linux命令在线查询

第三章 Linux常用高级命令

3.1 vi 常用操作

  • 编辑模式 i
  • 保存推出 wq
  • 退出 q
  • 复制粘贴 yy p
  • 删除 dd
  • 快速定位到文件末尾 G
  • 快速定位到文件首行 gg
  • 在vi 中进行查找 /
  • 设置行号 :set nu
  • 定位到指定的行 :行号
  • 删除所有内容: :%d

3.2 文件内容统计相关命令

  • wc

    • wc -c 获取文件的字节数量
    • wc -m 获取文件的字符数量
    • wc -l 获取文件的行数
    • wc -L 获取最长的一行内容的长度
    • wc -w 表示文件中的单词的个数,默认使用空白符切割
    [root@bigdata04 test]# cat hello1.txt
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    hello world!!
    [root@bigdata04 test]# wc -c hello1.txt 
    126 hello1.txt
    [root@bigdata04 test]# wc -m hello1.txt 
    126 hello1.txt
    [root@bigdata04 test]# wc -l hello1.txt 
    9 hello1.txt
    [root@bigdata04 test]# wc -w hello1.txt 
    18 hello1.txt
    
  • sort

    • sort -n 按照数字进行升序
    • sort -rn 倒序
    • sort -k k代表第几列,按照第K列进行排序
    [root@bigdata04 test]# cat num.txt 
    abc 789
    bcd 678
    cde 567
    def 456
    efg 345
    fgh 234
    ghi 123
    [root@bigdata04 test]# sort -k 2 -n num.txt
    ghi 123
    fgh 234
    efg 345
    def 456
    cde 567
    bcd 678
    abc 789
    [root@bigdata04 test]# sort -k 1 -n num.txt
    abc 789
    bcd 678
    cde 567
    def 456
    efg 345
    fgh 234
    ghi 123
    [root@bigdata04 test]# 
    
  • uniq 检查重复的列 ,uniq只能对连续一起的数据进行去重(如果遇到重复的数据不连续应该先进行去重)

    • uniq -u 返回文件中不重复的行
    • uniq -c 在输出行的前面加上数据在文件中重复的次数
    [root@bigdata04 test]# cat -b uniq.txt 
         1  hello
         2  hello
         3  hello
         4  hello
         5  hello
         6  hello
         7  hello
         8  abc
         9  abc
        10  abc
        11  hello
        12  hello
        13  666
    [root@bigdata04 test]# uniq -c uniq.txt 
          7 hello
          3 abc
          2 hello
          1 666
    [root@bigdata04 test]# uniq -u uniq.txt 
    666
    [root@bigdata04 test]# uniq uniq.txt 
    hello
    abc
    hello
    666
    [root@bigdata04 test]# sort uniq.txt | uniq
    666
    abc
    hello
    [root@bigdata04 test]# 
    
  • head 取前N条数据

    [root@bigdata04 test]# cat number.txt 
    1
    2
    4
    3
    9
    6
    7
    10
    [root@bigdata04 test]# sort -n number.txt | head -3
    1
    2
    3
    [root@bigdata04 test]# sort -nr number.txt | head -3
    10
    9
    7
    [root@bigdata04 test]# sort -n number.txt 
    1
    2
    3
    4
    6
    7
    9
    10
    [root@bigdata04 test]# sort -nr number.txt 
    10
    9
    7
    6
    4
    3
    2
    1
    [root@bigdata04 test]# 
    

3.3 Linux 日期操作

  • 显示当前时间

    [root@bigdata04 test]# date
    Wed Dec 29 13:36:47 CST 2021
    
  • 设置日期格式

    [root@bigdata04 test]# date +"%Y-%m-%d %H:%M:%S"
    2021-12-29 13:37:23
    
  • 常见操作

    • 获取秒数
    [root@bigdata04 test]# date +"%s"
    1640756353
    
    • 获取毫秒数
    [root@bigdata04 test]# date +"%s""000"
    1640756366000
    
    • 获取指定时间的毫秒数
    [root@bigdata04 test]# date --date="1970-01-01 08:00:00" +%s  
    0
    [root@bigdata04 test]# 
    
    • --date 获取前一天日期(前一月,后一月,前一周)
    [root@bigdata04 test]# date --date="1 days agO" +"%Y-%m-%d %H:%M:%S" 
    2021-12-28 13:47:22
    [root@bigdata04 test]# date --date="-1 days agO" +"%Y-%m-%d %H:%M:%S"
    2021-12-30 13:47:31
    [root@bigdata04 test]# date ^C
    [root@bigdata04 test]# date +"%Y-%m-%d %H:%M:%S"
    2021-12-29 13:47:50
    [root@bigdata04 test]# date --date="1 years agO" +"%Y-%m-%d %H:%M:%S"      
    2020-12-29 13:48:11
    [root@bigdata04 test]# date --date="1 week agO" +"%Y-%m-%d %H:%M:%S"     
    2021-12-22 13:48:21
    [root@bigdata04 test]# date --date="1 hours agO" +"%Y-%m-%d %H:%M:%S"    
    2021-12-29 12:48:31
    [root@bigdata04 test]# date --date="1 weeks agO" +"%Y-%m-%d %H:%M:%S" 
    [root@bigdata04 test]# date --date="2 minutes agO" +"%Y-%m-%d %H:%M:%S"    
    2021-12-29 13:48:05
    

3.4 进程相关

  • jps JAVA 相关的进程

    [root@bigdata04 ~]# jps
    13722 Jps
    
  • ps 显示进程信息

    [root@bigdata04 ~]# ps -ef | grep java
    root      13306  13269  0 16:53 pts/1    00:00:00 grep --color=auto java
    

    grep --color=auto这一行信息表示是grep这个命令本身

  • netstat 显示进程相关的信息,相比较ps命令可以额外显示端口的信息

    • 这个命令默认没有安装 需要先执行 yum install -y net-tools
    • netstat -anp
    [root@bigdata04 ~]# netstat -anp | grep 22
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1060/sshd           
    tcp        0     52 192.168.35.103:22       192.168.35.1:49641      ESTABLISHED 13265/sshd: root@pt 
    tcp        0      0 192.168.35.103:22       192.168.35.1:56576      ESTABLISHED 3131/sshd: root@pts 
    tcp6       0      0 :::22                   :::*                    LISTEN      1060/sshd           
    unix  3      [ ]         STREAM     CONNECTED     14322    1/systemd            /run/systemd/journal/stdout
    unix  2      [ ]         DGRAM                    20422    1062/rsyslogd        
    unix  3      [ ]         STREAM     CONNECTED     21122    1295/master          
    unix  2      [ ]         DGRAM                    22970    1/systemd   
    

查看ssh 服务端口的使用情况

  • top 动态显示系统消耗资源最多的进程信息

    top - 19:15:02 up 1 day,  1:14,  2 users,  load average: 0.00, 0.01, 0.05
    Tasks:  96 total,   1 running,  95 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.0 us,  6.2 sy,  0.0 ni, 93.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :  1863076 total,   574812 free,   161012 used,  1127252 buff/cache
    KiB Swap:  2097148 total,  2097148 free,        0 used.  1526920 avail Mem 
    
       PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND  
    

q退出监控 ,如果是多核CPU 按数字1 2 3 切换

  • kill

    • kill -9 PID 强制杀除PID
    • kill PID
  • pstree 查看当前进程树

    这个命令默认没有 使用yum 安装 yum -install -y psmisc

  • 端口查看的三种方式

    • netstat

      netstat -tnlp
      
      • -t:显示 TCP 连接。
      • -u:显示 UDP 连接。
      • -n:以数字形式显示端口号。
      • -p:显示占用端口的进程信息。
      • -l 参数的含义是仅显示正在监听的端口,不显示客户端连接的状态。这个参数可以帮助用户快速了解当前系统中哪些端口正在被服务占用。
    • lsof

      lsof -i -P -n
      
      • -i:显示网络连接信息。
      • -n:以数字形式显示端口号。
      • -P:不解析端口号对应的服务名称。
      • -p:显示指定进程打开的文件和网络连接信息。
    • ss

      ss -tnlp
      
      • -t:显示 TCP 连接。
      • -u:显示 UDP 连接。
      • -n:以数字形式显示端口号。
      • -p:显示占用端口的进程信息。

第四章 Linux三剑客

4.1 查找 grep

  • grep -v 过滤
  • grep -n 显示行号
  • grep -i 忽略大小写
[root@bigdata04 ~]# ps -ef | grep python
root       1061      1  0 Dec29 ?        00:00:13 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      60139  60113  0 10:04 pts/0    00:00:00 grep --color=auto python
[root@bigdata04 ~]# ps -ef | grep python | grep -v python
[root@bigdata04 ~]# ps -ef | grep python | grep -v grep
root       1061      1  0 Dec29 ?        00:00:13 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
[root@bigdata04 test]# cat hello3.txt
Hello HELLO world
Hello HELLO world
 6666
 hello world
7777
[root@bigdata04 test]# grep -ni Hello hello3.txt
1:Hello HELLO world
2:Hello HELLO world
4: hello world

4.2 编辑 sed

  • sed -i 修改源文件
  • sed 'na\xxx' 文件名 在指定文件名下指定n行追加内容 xxx
  • sed 'ni\xxx' 文件名 在指定文件名下指定n行增加内容 xxx
  • sed '$i\xxx' 文件名 在指定文件名末尾前增加内容xxx,同理末尾后 i 改为a
  • sed '$d' 删除最后一行
  • sed '$n' 删除指定的第n行
  • sed 's/search/replace/1(g)'

注意行号从1开始

[root@bigdata04 test]# cat -b sedtest.txt 
     1  author:Justin
     2  hello world!!
     3  hello world!!
     4  hello world!!
     5  hello world!!
     6  hello world!!
     7  hello world!!
     8  hello world!!
     9  hello world!!
    10  hello world!!
    11  hello world!!
    12  insert update delete
    13  date `date
    14  date `date`
    15  date `date`
[root@bigdata04 test]# sed -i '13,15d' sedtest.txt  
[root@bigdata04 test]# cat -b sedtest.txt 
     1  author:Justin
     2  hello world!!
     3  hello world!!
     4  hello world!!
     5  hello world!!
     6  hello world!!
     7  hello world!!
     8  hello world!!
     9  hello world!!
    10  hello world!!
    11  hello world!!
    12  insert update delete
[root@bigdata04 test]# cat -b sedtest.txt 
     1  author:Justin
     2  hello world!!
     3  hello world!!
     4  hello world!!
     5  hello world!!
     6  hello world!!
     7  hello world!!
     8  hello world!!
     9  hello world!!
    10  hello world!!
    11  hello world!!
    12  insert update delete
[root@bigdata04 test]# sed -i 's/hello/HELLO/g' sedtest.txt 
[root@bigdata04 test]# cat -b sedtest.txt 
     1  author:Justin
     2  HELLO world!!
     3  HELLO world!!
     4  HELLO world!!
     5  HELLO world!!
     6  HELLO world!!
     7  HELLO world!!
     8  HELLO world!!
     9  HELLO world!!
    10  HELLO world!!
    11  HELLO world!!
    12  insert update delete
[root@bigdata04 test]# sed -i 's/!/?/2' sedtest.txt 
[root@bigdata04 test]# cat -b sedtest.txt 
     1  author:Justin
     2  HELLO world!?
     3  HELLO world!?
     4  HELLO world!?
     5  HELLO world!?
     6  HELLO world!?
     7  HELLO world!?
     8  HELLO world!?
     9  HELLO world!?
    10  HELLO world!?
    11  HELLO world!?
    12  insert update delete
[root@bigdata04 test]# 

4.3 分析 awk

  • awk -F 指定分割符
  • awk 中的变量 $0 代表整个文件 $1 第一列
  • awk 过滤 /
[root@bigdata04 test]# cat -b awktest.txt 
     1  hello world
     2  hello world
     3  hello world
     4  hello world
     5  hello world
     6  hello world
     7  hello world
     8  hello world
     9  hello world
    10  abc
    11  def
[root@bigdata04 test]# awk '{print$0}' awktest.txt      
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
abc
def
[root@bigdata04 test]# awk '{print$1}' awktest.txt  
hello
hello
hello
hello
hello
hello
hello
hello
hello
abc
def
[root@bigdata04 test]# awk '/hello/{print$0}' awktest.txt  
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
[root@bigdata04 test]# vi awktest1.txt
[root@bigdata04 test]# cat -b awktest1.txt                 
     1  abc:123
     2  bcd:234
     3  cde:345
     4  def:456
[root@bigdata04 test]# awk -F: '{print$1}' awktest1.txt          
abc
bcd
cde
def
[root@bigdata04 test]# awk -F: '{print$2}' awktest1.txt  
123
234
345
456
[root@bigdata04 test]# awk -F: '{print$0}' awktest1.txt  
abc:123
bcd:234
cde:345
def:456
# 指定分隔符 : 对第一列 含有b 过滤
[root@bigdata04 test]# awk -F: '($1~/b/){print$1}' awktest1.txt    
abc
bcd
[root@bigdata04 test]# 

第五章shell

5.1 shell

shell 是用户与Linux系统沟通的桥梁

shell脚本shell编程其实就是把之前在shell中执行的单个命令按照一定的逻辑和规则,组装到一个文件中,后面执行的时候就可以直接执行这个文件了,这个文件我们称之为shell脚本。

5.2 shell 格式

#!/bin/bash

5.3 shell 执行方式

  • 未添加执行权限时的执行方式

    • bash xxx.sh
    • sh xxx.sh
  • 添加执行权限执行方式

    • chmod u+x xxx.sh—>执行方式 ./xxx.sh

    • xxx.sh 配置环境变量,将当前目录加入环境变量 vi /etc/profile

      export PATH=.:$PATH
      

      重新加载环境变量配置文件 生效配置 source /etc/profile

  • shell 脚本的单步执行

    [root@bigdata01 shell]# bash -x hello.sh 
    + echo hello 'world!' hello world!
    

5.4 shell 中的变量

  • 变量命名规范

    数字,字母下划线组成,且不能以数字开头

  • 变量的赋值

    变量的赋值通过=,在变量,等号和值之间不能出现空格

  • 变量分类

    • 本地变量

    • 环境变量

    • 位置变量

      [root@bigdata04 test]# cat echotest2.sh
      #!/bin/bash
      echo $0
      echo $1
      echo $2
      echo $3
      [root@bigdata04 test]# echotest2.sh 123 456 abc
      ./echotest2.sh
      123
      456
      abc
      [root@bigdata04 test]# 
      
    • 特殊变量

      • $? 表示上一条命令的返回状态码,状态码在0~255之间
      [root@bigdata04 ~]# pstree
      systemd─┬─NetworkManager───2*[{NetworkManager}]
              ├─VGAuthService
              ├─agetty
              ├─auditd───{auditd}
              ├─crond
              ├─dbus-daemon───{dbus-daemon}
              ├─lvmetad
              ├─master─┬─pickup
              │        └─qmgr
              ├─polkitd───6*[{polkitd}]
              ├─rsyslogd───2*[{rsyslogd}]
              ├─sshd─┬─sshd───bash
              │      └─sshd───bash───pstree
              ├─systemd-journal
              ├─systemd-logind
              ├─systemd-udevd
              ├─tuned───4*[{tuned}]
              └─vmtoolsd
      [root@bigdata04 ~]# echo $?
      0
      

      常见的状态码 0–命令成功结束 1–通用未知错误 127–没有找到命令 128–无效退出参数 128+x --Linux 信号x的严重错误 130–命令通过Ctrl+c控制码越界 255–退出码越界 这个状态码在工作中的应用场景是这样的,我们有时候会根据上一条命令的执行结果来执行后面不同的业务逻辑

      • $# 表示shell 脚本中所有参数的个数

        这个特殊变量的应用场景是这样的,假设我们的脚本在运行的时候需要从外面动态获取三个参数,那么在执行脚本之前就需要先判断一下脚本后面有没有指定三个参数,如果就指定了1个参数,那这个脚本就没有必要执行了,直接停止就可以了,参数个数都不够,执行是没有意义的。

    • shell 变量中的引号

      • 单引号 '' 单引号不解析变量
      • 双引号 "" 解析变量
      • 反引号 `` 解析变量如果变量是shell 命令还会执行
      [root@bigdata04 test]# parname=pwd
      [root@bigdata04 test]# echo '$parname'
      $parname
      [root@bigdata04 test]# echo "$parname"
      pwd
      [root@bigdata04 test]# echo `$parname`
      /root/shell/test
      [root@bigdata04 test]# echo "'$parname'"
      'pwd'
      [root@bigdata04 test]# 
      

5.5 shell 中的循环和判断

  • for
[root@bigdata01 shell]# cat for2.sh 
#!/bin/bash
for i in 1 2 3;do
echo $i
done
[root@bigdata01 shell]# cat for1.sh
#!/bin/bash
for((i=0;i<10;i++))
do
echo $i
done
  • while
[root@bigdata01 shell]# cat while3.sh
#!/bin/bash
while [ "abc" = "abc" ]
do 
echo yes
sleep 1
done
  • if
#!/bin/bash
if [ $# -lt 1 ]
then 
echo "not found"
exit 100
fi

flag=$1
if [ $flag -eq 1 ]
then
echo "one"
elif [ $flag -eq 2 ]
then
echo two
elif [ $flag -eq 3 ]
then 
echo "three"
else
echo "not support!!"
fi

5.6 shell 扩展

  • 让脚本后台一直跑 关键字nohup 阻断信号传输 & 后台运行

    nohup sh while2.sh &
    
  • 标准输出 1

  • 标准错误输出 2

  • 重定向 > 重定向 >>追加重定向

    [root@bigdata04 test]# ll 1>standoutput.txt
    [root@bigdata04 test]# cat standoutput.txt 
    total 56
    -rw-r--r--. 1 root root  32 Dec 31 11:21 awktest1.txt
    -rw-r--r--. 1 root root 116 Dec 31 11:18 awktest.txt
    -rw-r--r--. 1 root root  11 Dec 31 13:44 echotest1.txt
    -rwxr--r--. 1 root root  44 Dec 31 13:49 echotest2.sh
    -rw-r--r--. 1 root root   6 Dec 31 13:40 echotest.txt
    -rw-r--r--. 1 root root 126 Dec 29 12:56 hello1.txt
    -rw-r--r--. 1 root root  25 Dec 29 12:59 hello2.txt
    -rw-r--r--. 1 root root  60 Dec 31 10:14 hello3.txt
    -rw-r--r--. 1 root root 254 Dec 29 12:52 hello.txt
    -rw-r--r--. 2 root root   0 Dec 29 10:42 newhlink
    -rw-r--r--. 2 root root   0 Dec 29 10:42 newtext.txt
    lrwxrwxrwx. 1 root root  11 Dec 29 10:45 newvlink -> newtext.txt
    -rw-r--r--. 1 root root  17 Dec 29 13:24 number.txt
    -rw-r--r--. 1 root root  56 Dec 29 13:13 num.txt
    -rw-r--r--. 1 root root 201 Dec 31 11:13 sedtest.txt
    -rw-r--r--. 1 root root   0 Dec 31 14:21 standoutput.txt
    -rw-r--r--. 1 root root  70 Dec 29 13:20 uniq.txt
    -rwxr--r--. 1 root root  62 Dec 31 13:54 while3.sh
    # 错误追加重定向
    [root@bigdata04 test]# lk 2>>standoutput.txt 
    [root@bigdata04 test]# cat standoutput.txt 
    total 56
    -rw-r--r--. 1 root root  32 Dec 31 11:21 awktest1.txt
    -rw-r--r--. 1 root root 116 Dec 31 11:18 awktest.txt
    -rw-r--r--. 1 root root  11 Dec 31 13:44 echotest1.txt
    -rwxr--r--. 1 root root  44 Dec 31 13:49 echotest2.sh
    -rw-r--r--. 1 root root   6 Dec 31 13:40 echotest.txt
    -rw-r--r--. 1 root root 126 Dec 29 12:56 hello1.txt
    -rw-r--r--. 1 root root  25 Dec 29 12:59 hello2.txt
    -rw-r--r--. 1 root root  60 Dec 31 10:14 hello3.txt
    -rw-r--r--. 1 root root 254 Dec 29 12:52 hello.txt
    -rw-r--r--. 2 root root   0 Dec 29 10:42 newhlink
    -rw-r--r--. 2 root root   0 Dec 29 10:42 newtext.txt
    lrwxrwxrwx. 1 root root  11 Dec 29 10:45 newvlink -> newtext.txt
    -rw-r--r--. 1 root root  17 Dec 29 13:24 number.txt
    -rw-r--r--. 1 root root  56 Dec 29 13:13 num.txt
    -rw-r--r--. 1 root root 201 Dec 31 11:13 sedtest.txt
    -rw-r--r--. 1 root root   0 Dec 31 14:21 standoutput.txt
    -rw-r--r--. 1 root root  70 Dec 29 13:20 uniq.txt
    -rwxr--r--. 1 root root  62 Dec 31 13:54 while3.sh
    -bash: lk: command not found
    

    综合使用 nohup hello.sh >/dev/null 2>&1 & nohup和&:可以让程序一直在后台运行 /dev/null:是linux中的黑洞,任何数据扔进去都找不到了 >/dev/null:把标准输出重定向到黑洞中,表示脚本的输出信息不需要存储 2>&1 :表示是把标准错误输出重定向到标准输出中

第六章 Linux中的定时器

6.1 定时器 crontab

6.2 定时器的配置

  1. 确认crontab 服务是否正常启动

    [root@bigdata04 test]# systemctl status crond
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2021-12-27 14:05:34 CST; 4 days ago
     Main PID: 723 (crond)
       CGroup: /system.slice/crond.service
               └─723 /usr/sbin/crond -n
    
    Dec 27 14:05:34 bigdata04 systemd[1]: Started Command Scheduler.
    Dec 27 14:05:34 bigdata04 crond[723]: (CRON) INFO (RANDOM_DELAY will be sca....)
    Dec 27 14:05:34 bigdata04 crond[723]: (CRON) INFO (running with inotify support)
    Hint: Some lines were ellipsized, use -l to show in full.
    

    systemctl start crond --启动命令 systemctl stop crond --关闭命令

  2. 编辑配置文件 /etc/crontab

    # shell 脚本 显示当前的时间
    [root@bigdata04 test]# cat showTime.sh
    #!/bin/bash
    echo `date +"%Y-%m-%d %H:%M:%S"`
    # 监控变动的日志文件
    [root@bigdata04 test]# tail -f showTime.log
    2021-12-31 14:41:01
    2021-12-31 14:42:01
    2021-12-31 14:43:01
    # 配置 定时任务文件 结果追加重定向到showTime.log
    [root@bigdata04 test]# cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # 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
    * * * * * root sh /root/shell/test/showTime.sh >> /root/shell/test/showTime.log
    

    要将定时任务终止,直接注释配置文件中配置的定时任务

第七章Linux常见配置

7.1 静态IP配置

  1. 修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens33

    [root@bigdata04 test]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
    TYPE="Ethernet"
    PROXY_METHOD="none"
    BROWSER_ONLY="no"
    # 修改为static 
    BOOTPROTO="static"
    DEFROUTE="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6INIT="yes"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_FAILURE_FATAL="no"
    IPV6_ADDR_GEN_MODE="stable-privacy"
    NAME="ens33"
    UUID="2bc41e60-ba09-4989-b408-3b40b89378e7"
    DEVICE="ens33"
    ONBOOT="yes"
    # 添加 ip地址 网关 DNS 参看NAT网络信息
    IPADDR=192.168.35.103
    GATEWAY=192.168.35.2
    DNS1=192.168.35.2
    [root@bigdata04 test]# 
    
  2. 重启网卡 service network restart 查看ip ip addr

    service network restart
    ip addr
    

7.2 hostname配置

  • 临时配置

    hostname bigdata04
    
  • 永久配置

    vi /etc/hostname
    bigdata04
    

bigdata04 为你要设置的主机名称

7.3 防火墙配置

  1. 查看防火墙状态

    [root@bigdata04 test]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    
    Dec 27 14:05:34 bigdata04 systemd[1]: Starting firewalld - dynamic firewall.....
    Dec 27 14:05:34 bigdata04 systemd[1]: Started firewalld - dynamic firewall ...n.
    Dec 27 14:19:41 bigdata04 systemd[1]: Stopping firewalld - dynamic firewall.....
    Dec 27 14:19:42 bigdata04 systemd[1]: Stopped firewalld - dynamic firewall ...n.
    Hint: Some lines were ellipsized, use -l to show in full.
    [root@bigdata04 test]# 
    
  2. 临时关闭防火墙

    [root@bigdata04 ~]# systemctl stop firewalld
    
  3. 永久关闭防火墙

    [root@bigdata04 ~]# systemctl disable firewalld
    
  4. 确认是否在开机启动项中关闭了

    [root@bigdata04 test]# systemctl list-unit-files | grep firewalld
    firewalld.service                             disabled
    [root@bigdata04 test]# 
    

7.4 在Linux 配置JDK

  1. 上传JDK文件 至 /data/soft 先创建文件夹

    上传工具可以选择 wincp ,secureFX,或者使用VirtualBOX 的共享文件夹都是可以的

  2. 解压文件 tar-zxvf xxx.tat.gz

    [root@bigdata04 soft]# tar -zxvf jdk-8u202-linux-x64.tar.gz
    
  3. 重命名 解压文件 为 JDK

    [root@bigdata04 soft]# mv jdk1.8.0_202 jdk1.8
    
  4. 配置环境变量

    [root@bigdata04 soft]# vi /etc/profile 
    ..... 
    export JAVA_HOME=/data/soft/jdk1.8 
    export PATH=.:$JAVA_HOME/bin:$PATH
    
  5. 重新加载配置文件 source /etc/profile

  6. 验证 java -version

    [root@bigdata04 test]# java -version
    java version "1.8.0_311"
    Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
    [root@bigdata04 test]# 
    

7.5 在Linux配置Maven

  • 安装JDK

    Maven需要JDK的支持,所以如果未安装JDK的请先安装JDK

  • 下载Maven压缩包

    下载地址,下载二进制版本

    bin.tar.gz和bin.zip以及src.tar.gz和src.zip以及rpm和dmg的区别

  • 解压Maven压缩包

    [root@gitlab soft]# pwd
    /data/soft
    [root@gitlab soft]# tar -xvf apache-maven-3.9.1-bin.tar.gz
    
  • 配置Maven环境变量

    # 配置Maven环境变量
    vi /etc/profile
    
    # 在配置文件末尾加上maven路径
    # maven
    export MAVEN_HOME=/data/soft/apache-maven-3.9.1
    export PATH=.:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$MAVEN_HOME/bin:$PATH
    # 使配置文件立即生效
    source /etc/profile
    
  • 校验Maven安装

    # 使用查看maven版本命令查看maven安装情况,如正确出现maven版本则表示安装成功
    [root@gitlab soft]# mvn -version
    Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
    Maven home: /data/soft/apache-maven-3.9.1
    Java version: 1.8.0_311, vendor: Oracle Corporation, runtime: /data/soft/jdk1.8/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.10.0-1160.80.1.el7.x86_64", arch: "amd64", family: "unix"
    
  • Maven配置阿里云仓库

    # 进入maven安装目录和配置文件路径
    [root@gitlab apache-maven-3.9.1]# cd /data/soft/apache-maven-3.9.1/conf/
    # 编辑maven的配置文件
    vi settings.xml
    # 增加以下内容
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <mirrorOf>central</mirrorOf>
        <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
    
  • Maven配置依赖下载位置

    # settings.xml 配置文件中修改
    <localRepository>/data/soft/apache-maven-3.9.1/localRepo</localRepository>
    

7.6 在Linux安装docker

docker

7.7 在Linux安装Git

  • yum 安装

    • 安装
    yum intall git
    
    • 查看版本
    [root@gitlab conf]# git --version
    git version 1.8.3.1
    
    • 移除
    yum remove git
    
  • 源码包安装

    CentOS7系统安装Git详细步骤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值