• linux-1-常用shell命令复习


    一、什么是shell?

    shell 既是一种命令语言,又是一种程序设计语言,能实现人机交互,通常应用在我们平时所说的服务器,服务器可能应用最多的就是centos。

    bash 是shell的一种,应用广泛一切皆文件,那什么是shell脚本?它能实现自动化,实际上就是在文件上编写了shell命令。

    • 单引号:所见即所得,如打印就直接打印出看到的;
    • 双引号:所见非所得,它会先把变量解析之后,再输出(存在变量)
    • 反引号(``) :命令替换,通常用于把命令输出结果传给入变量中
    [root@iZbp19lugf22zbsubsf1y6Z ~]# echo '$home'
    $home
    [root@iZbp19lugf22zbsubsf1y6Z ~]# echo "$home"
    
    [root@iZbp19lugf22zbsubsf1y6Z ~]# 

     二、常用命令之文件操作

    1、查看磁盘空间:df -h

    当然也可以直接df、或df -m等等,广深小龙常用就是:df -h

    [root@iZbp19lugf22zbsubsf1y6Z ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        40G  7.9G   30G  21% /
    devtmpfs        909M     0  909M   0% /dev
    tmpfs           920M     0  920M   0% /dev/shm
    tmpfs           920M  824K  919M   1% /run
    tmpfs           920M     0  920M   0% /sys/fs/cgroup
    点开详细查看bash效果

    2、查看当前目录或文件:ls

    可以接目录如:ls ~  但是平时喜欢这用:ll  因为ll=ls -l,既是缩写了方便快捷。

    [root@iZbp19lugf22zbsubsf1y6Z ~]# ls
    djo.out  easymock  hrun_django  hrun_mysql  jenkins  python36  sql  start.sh  swapfile
    [root@iZbp19lugf22zbsubsf1y6Z ~]# ls
    djo.out  easymock  hrun_django  hrun_mysql  jenkins  python36  sql  start.sh  swapfile
    [root@iZbp19lugf22zbsubsf1y6Z ~]# ls -l
    total 1048612
    -rw-r--r--  1 root root         96 Jan 24 17:59 djo.out
    drwxr-xr-x  4 root root       4096 Jan  8 16:24 easymock
    drwxr-xr-x  3 root root       4096 Jan  8 16:24 hrun_django
    drwxr-xr-x  5 root root       4096 Jan  8 15:52 hrun_mysql
    drwxr-xr-x 17 1000 1000       4096 Apr 11 16:05 jenkins
    drwxr-xr-x  6 root root       4096 Jan  8 19:41 python36
    drwxr-xr-x  5 root root       4096 Feb 28 18:16 sql
    -rw-r--r--  1 root root        253 Jan  8 20:32 start.sh
    -rw-r--r--  1 root root 1073741824 Jan  8 15:50 swapfile
    [root@iZbp19lugf22zbsubsf1y6Z ~]# ll
    total 1048612
    -rw-r--r--  1 root root         96 Jan 24 17:59 djo.out
    drwxr-xr-x  4 root root       4096 Jan  8 16:24 easymock
    drwxr-xr-x  3 root root       4096 Jan  8 16:24 hrun_django
    drwxr-xr-x  5 root root       4096 Jan  8 15:52 hrun_mysql
    drwxr-xr-x 17 1000 1000       4096 Apr 11 16:05 jenkins
    drwxr-xr-x  6 root root       4096 Jan  8 19:41 python36
    drwxr-xr-x  5 root root       4096 Feb 28 18:16 sql
    -rw-r--r--  1 root root        253 Jan  8 20:32 start.sh
    -rw-r--r--  1 root root 1073741824 Jan  8 15:50 swapfile
    点开详细查看bash效果

    3、切换目录:cd

    波浪线为当前用户的家目录 ①cd ~    ②返回上级目录:cd .. 

    [root@iZbp19lugf22zbsubsf1y6Z easymock]# cd ~
    [root@iZbp19lugf22zbsubsf1y6Z ~]# cd easymock/
    [root@iZbp19lugf22zbsubsf1y6Z easymock]# cd ..
    [root@iZbp19lugf22zbsubsf1y6Z ~]# 
    点开详细查看bash效果

    4、创建文件夹:mkdir 目录名

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# mkdir test
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# ls
    test

    5、创建文件:  touch 文件名

    [root@iZbp19lugf22zbsubsf1y6Z ~]# touch test
    [root@iZbp19lugf22zbsubsf1y6Z ~]# ll
    total 1048612
    -rw-r--r--  1 root root          0 Apr 11 22:39 test

    6、vi 编辑器:vi 文件名  (无当前文件会新建)

    vi 有三个模式:①默认命令模式:移动光标  ②插入模式:i   ③底行模式:esc

    w:保存   q:退出   叹号:强制退出

    广深小龙
    :wq!

    7、查看当前路径:pwd

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# pwd
    /root/gsxl

    8、复制文件:cp 需复制文件 存在新复制文件路径

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# cp t.txt t
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# ll
    total 12
    -rw-r--r-- 1 root root   46 Apr 11 22:53 t
    drwxr-xr-x 2 root root 4096 Apr 11 22:42 test
    -rw-r--r-- 1 root root   46 Apr 11 22:49 t.txt

    9、移动文件:mv 需移动文件 存在文件路径

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# mv t test/
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# ll test/
    total 4
    -rw-r--r-- 1 root root 46 Apr 11 22:53 t

    10、查找目录或文件存放的路径:find / -name 名称    (可加匹配符)

    根目录:/   意思是根目录下找以gsx开头的目录或文件。

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# find ~ -name gsx*
    /root/gsxl

     11、查看文件内容:cat 文件名

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# cat t.txt 
    广深小龙

    文件内容过多推荐用:less 文件  (可翻页按b,及光标移动)

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# more t.txt 
    广深小龙

    12、添加软连接(类似win的快捷方式):ln -s 文件名称 快捷方式名称

    ln -s /root/python36/bin/pip /usr/bin/pip3

    13、文件权限:chmod -x 文件名

    当然 chmod 还可以带其它参数,比如常用的:chmod 755 文件名

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# chmod 755 test
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# ll
    total 8
    drwxr-xr-x 2 root root 4096 Apr 11 22:55 test

    14、文件内容追加:echo "内容">> 目录文件   (没有文件会创建一个)

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# cat t.txt 
    vm.swappiness = 6661111
    asd
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# echo "hello">> ~/gsxl/t.txt
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# cat t.txt 
    vm.swappiness = 6661111
    asd
    hello

     15、替换文件内容:sed "s/替换内容/新内容/g" 文件名

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# cat t.txt 
    vm.swappiness = 6661111
    asd
    hello
    # 加上 -i 参数既不打印显示
    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# sed "s/asd/gsxl/g" t.txt 
    vm.swappiness = 6661111
    gsxl
    hello

    16、查看程序log日志:tail -f log文件

    上面的是动态打印,也可以打印指定行数如:tail -500f ~/gsxl/out.log

    17、其它命令:

    ①重启:reboot

    ②wget下载:wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

    ③解压:tar -xvf xxx.tgz

                   unzip xxx.zip

    ④yum安装、编译安装:

      yum install java-1.8.0-openjdk-devel.x86_64
      make
      make install

    ⑤ping命令:ping url

    ⑥Django项目后台运行:nohup python3 manage.py runserver 0.0.0.0:8000 >djo.out 2>&1 &

       杀掉进程:ps -aux | grep python|xargs kill -9

    三、常用命令之进程操作

    1、查看进程(类似win的任务管理):top

     2、查找某个服务的进程:ps -ef | grep 服务名称

    [root@iZbp19lugf22zbsubsf1y6Z gsxl]# ps -ef | grep jenkins
    1000     24520 24503  0 Mar30 pts/0    00:00:32 /sbin/tini -- /usr/local/bin/jenkins.sh
    1000     24552 24520  0 Mar30 pts/0    00:23:29 java -Duser.home=/var/jenkins_home -Duser.timezone=Asia/Shanghai -Djenkins.model.Jenkins.slaveAgentPort=50000 -jar /usr/share/jenkins/jenkins.war
    1000     27057 24552  0 Mar30 pts/0    00:00:00 [jenkins.sh] <defunct>

    3、杀掉进程:kill -9 进程号

    如上面的进程:kill - 9 24520

    4、查看所有端口的进程:netstat -tlnp

    查看连接服务的ip:netstat -tnp

    5、查看端口是否被占用:netstat -anp | grep 端口号

    [root@iZbp19lugf22zbsubsf1y6Z ~]# netstat -anp | grep 8000
    tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      4625/python3        
    [root@iZbp19lugf22zbsubsf1y6Z ~]# 

    6、查看机器内存:free -m

    参数可以-g、-m等

    [root@iZbp19lugf22zbsubsf1y6Z ~]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1838        1141         174           4         522         481
    Swap:          1023         112         911

    最后:

    1、一些服务重启的 shell 脚本示例:

    运行shell脚本:. strat.sh

    [root@iZbp19lugf22zbsubsf1y6Z ~]# cat start.sh 
    ps -aux | grep python|xargs kill -9
    cd /root/easymock
    docker-compose down
    docker restart hrun zentao jenkins
    cd /root/hrun_django/httprunnermanger_web
    nohup python3 manage.py runserver 0.0.0.0:8000 >djo.out 2>&1 &
    cd /root/easymock

    2、就好像win的bat文件脚本一样,广深小龙也写了一个git的,运行可以上传or同步代码:

    cd G:/web_django
    git status
    git add *
    git commit -m "Django"
    git pull
    git push origin master
    # pause
    shutdown/s
    
    # 最后关机

    因为我一般是睡觉前习惯运行一遍bat文件,最后自动关机,就可以去刷牙睡觉了。下一节将是三剑客的复习。

    欢迎来大家QQ交流群一起学习:482713805 !!!

  • 相关阅读:
    APPlication,Session和Cookie的区别
    C# 中的Request对象的应用
    从字符串里提取一个列表(数组)
    UDP:用户数据报协议
    反射
    网络编程
    多线程
    final,finally和finalize的区别
    集合
    StringBuffer
  • 原文地址:https://www.cnblogs.com/gsxl/p/12682919.html
Copyright © 2020-2023  润新知