linux基础 02

目录

1.基本命令

9.mv + cp

1.mv 剪切

2.cp 复制

    mv =》 文件个数 始终是1份
    cp =》 文件个数 始终是2份

1.移动

        [root@liunxandshell bigdata]# mv 1.log ../1.log 
        [root@liunxandshell bigdata]# cp ../1.log ./

2.改名字

        [root@liunxandshell ~]# mv ./1.log ./1.log_blk
        [root@liunxandshell ~]# cp 1.log_blk 1.log

标准写法:

        mv xx.log bigdata/xx.log
        cp xx.log bigdata/xx.log

不标准写法:

        mv xx.log bigdata/
        cp xx.log bigdata/

思考:

        mv 和 cp 哪个块?
            mv 速度快

10.上传 下载

1. xhell工具 xftp

2. 命令

        linux 安装一个包:
            yum install -y lrzsz

上传

            [root@liunxandshell ~]# rz 【回车】

下载

            [root@liunxandshell ~]# sz 1.log

11.rm 删除文件 或者 文件夹(高危命令)

    生产上轻易不要删数据

1.文件

        [root@liunxandshell ~]# rm 1.log_blk 
        rm:是否删除普通文件 "1.log_blk"?y

        [root@liunxandshell ~]# rm -f 1.log 

2.文件夹 recursive 递归

        [root@liunxandshell ~]# rm -r ./bigdata/
        rm:是否进入目录"./bigdata/"? y
        rm:是否删除普通文件 "./bigdata/3_2.log"?y
        rm:是否删除普通空文件 "./bigdata/2.log"?y
        rm:是否删除目录 "./bigdata/"?y

        [root@liunxandshell ~]# rm -r -f dir1

3.通用写法

        rm -rf xxx

        [root@liunxandshell ~]# rm -rf dir3

坑:【搞不好就管吃管住去了】

        shell:
            dir="$1"
            path=/data/${dir}

            rm -rf ${path}

        ods =>
            parh=/data/ods
        "" =>
            parh=/data/

12.alias (别名)

别名使用:

        alias ll='ls -l --color=auto'
        alias aa='cd /tmp'

1.使用

        [root@liunxandshell ~]# alias aa='cd /tmp'
        【仅仅当前会话生效】

2.aa 命令 在任何地方都能使用?

1.环境变量

            [root@liunxandshell ~]# echo $PATH
            /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.环境变量 配置文件:

            1.全局的【所有用户都可以使用】
                /etc/profile
            2.个人的【只能是当前用户使用,其他用户不能使用】
                ~/.bashrc
                ~/.bash_profile【有坑】

3.环境变量生效

            source /etc/profile
            source ~/.bashrc
            source ~/.bash_profile

eg: => 1.命令 在任何地方都能使用

                [root@liunxandshell ~]# vim /etc/profile
                配置一个aa环境变量别名
                alias aa="cd /tmp"
                [root@liunxandshell ~]# source /etc/profile

eg: => 2.所有用户都可以使用

                [root@liunxandshell ~]# useradd mm 【创建用户】
                [root@liunxandshell ~]# su - mm 【切换用户】
                [mm@liunxandshell ~]$ aa
                [mm@liunxandshell tmp]$ pwd
                /tmp    

13.历史命令

    history
        mysql =>
            mysql -uroot -p =>
                123456
    [root@liunxandshell tmp]# !510
    cd 
    [root@liunxandshell ~]# cd

14.tab 自动补全的功能

    按tab键 按一次 自动补全 或者 过滤
    按tab键 按两次 过滤

15.用户相关的命令

1.用户、用户组

1.用户

            [root@liunxandshell ~]# ll /usr/sbin/user*
            -rwxr-x---. 1 root root 118192 11月  6 2016 /usr/sbin/useradd
            -rwxr-x---. 1 root root  80360 11月  6 2016 /usr/sbin/userdel
            -rwxr-x---. 1 root root 113840 11月  6 2016 /usr/sbin/usermod

2.用户组

            [root@liunxandshell ~]# ll /usr/sbin/group*
            -rwxr-x---. 1 root root 65480 11月  6 2016 /usr/sbin/groupadd
            -rwxr-x---. 1 root root 57016 11月  6 2016 /usr/sbin/groupdel
            -rwxr-x---. 1 root root 57064 11月  6 2016 /usr/sbin/groupmems
            -rwxr-x---. 1 root root 76424 11月  6 2016 /usr/sbin/groupmod

3.创建用户

            [root@liunxandshell ~]# useradd xy
            [root@liunxandshell ~]# id xy
            uid=1004(xy) gid=1006(xy) 组=1006(xy)

4.创建一个用户:

            1.用户名称 xy 
            2.创建了一个用户组 xy 
            3.创建了一个 家目录 : /home/xy

            用户 :用户组 :
                        主组
                        附属组
1.用户信息 =》 文件 记录
            [root@liunxandshell ~]# ll /etc/passwd
            -rw-r--r--. 1 root root 2440 4月  13 13:21 /etc/passwd
2.用户组 =》 文件 记录
            [root@liunxandshell ~]# vim /etc/group

5.切换用户

1.su - xy 【切换到家目录】【加上会默认执行环境变量】
                [root@liunxandshell ~]# su - xy
                [xy@liunxandshell ~]$ 
2.su xy【留在当前的家目录】【不会执行环境变量】
                [root@liunxandshell ~]# su xy
                [xy@liunxandshell root]$ pwd
                /root

6.样式丢失【坑】

原因
                ./.bash* 文件被删了
                [xy@liunxandshell ~]$ rm -rf ./.bash*
                [xy@liunxandshell ~]$ exit
                exit
                [root@liunxandshell ~]# su - xy
                上一次登录:三 4月 13 13:31:35 CST 2022pts/0 上
                -bash-4.2$ 
如何解决?
                1.切换到指定目录
                -bash-4.2$ cd /etc/skel/
                2.拷贝文件回去
                -bash-4.2$ cp ./.* ~
                cp: 略过目录"./."
                cp: 略过目录"./.."
                cp: 略过目录"./.mozilla"
                3.退出
                -bash-4.2$ exit
                登出
                4.重新登录
                [root@liunxandshell ~]# su - xy
                上一次登录:三 4月 13 13:33:04 CST 2022pts/0 上
                [xy@liunxandshell ~]$ 

7.用户组

创建用户组
                [root@liunxandshell ~]# groupadd bigdata
添加附属组
                [root@liunxandshell ~]# usermod -a -G bigdata xy
                [root@liunxandshell ~]# id xy
                uid=1004(xy) gid=1006(xy) 组=1006(xy),1007(bigdata)
需求:主组变成bigdata,附属组xy
                主组变成bigdata,附属组xy
                1.修改主组
                    [root@liunxandshell ~]# usermod -g bigdata xy
                    [root@liunxandshell ~]# id xy
                    uid=1004(xy) gid=1007(bigdata) 组=1007(bigdata)
                2.添加新的附属组
                    [root@liunxandshell ~]# usermod -a -G xy xy
                    [root@liunxandshell ~]# id xy
                    uid=1004(xy) gid=1007(bigdata) 组=1007(bigdata),1006(xy)

8.修改用户的密码

            passed:
1.root
                    [root@liunxandshell ~]# passwd xy
                    更改用户 xy 的密码 。
                    新的 密码:
                    无效的密码: 密码少于 8 个字符
                    重新输入新的 密码:
                    passwd:所有的身份验证令牌已经成功更新。
2.当前用户

9.sudo 普通用户临时使用root的最大权限

1.配置
                [root@liunxandshell ~]# vim /etc/sudoers
                xy      ALL=(ALL)       NOPASSWD: ALL
2.使用
                sudo xxx
思考:
                sudo su - xxx 
                    1.命令报不报错
                        不报错
                    2.需不需要输入密码
                        不需要
                [xy@liunxandshell root]$ sudo su - lxy
                上一次登录:日 4月 10 19:37:38 CST 2022pts/0 上
                [lxy@liunxandshell ~]$ 
            
            cdh : hdfs yarn hive hbase 
                su - yarn 【切换不了】
                su - hive 【有点问题】
大坑【有些用户不能切换】
                mm:x:1003:1005::/home/mm:/sbin/nologin => 好歹还有个提示
                [lxy@liunxandshell ~]$ sudo su - mm
                上一次登录:三 4月 13 13:00:18 CST 2022pts/0 上
                This account is currently not available.
                [lxy@liunxandshell ~]$ 

                mm:x:1003:1005::/home/mm:/usr/bin/false => 提示都没有
                [lxy@liunxandshell ~]$ sudo su - mm
                上一次登录:三 4月 13 14:04:58 CST 2022pts/1 上
                [lxy@liunxandshell ~]$ 

16.文件权限的命令

1.权限

        -rw-r--r--. 1 root root   26 4月  12 15:15 3_1.log
        drwxr-xr-x. 2 root root 4096 4月  10 19:04 app
    
    drwxr-xr-x 10个字母 =》 权限
        第一个字母
            d:文件夹
            -:文件
            l:软连接(win快捷方式)
        
        权限:rwxr-xr-x 9个决定的(三个三个一组) 755
        第一组:rux => 4+2+1 = 7 所属用户的权限
        第二组:r-x => 4+0+1 = 5 所属用户组的权限
        第三组:r-x => 4+0+1 = 5 其他用户组的权限

        权限:
            r 读权限    4
            w 写权限    2
            x 执行权限  1
            - 没权限    0

        -rw-r--r-- => 644 读写 读 读

2.修改权限命令 【文件夹 -R】

        chmod 
        [root@liunxandshell tmp]# chmod 646 ./1.log

3.chown 改变文件所属者 【文件夹 -R】

        [root@liunxandshell tmp]# chown xy:xy 1.log 
        -rw-r--rw-. 1 xy   xy       10 4月  13 14:20 1.log

17.软连接【小盘换大盘】

    1.加磁盘
    2.启动系统的时候,加软连接,小盘换大盘
    [root@liunxandshell ~]# ln -s ./dir4/ ddd

    lrwxrwxrwx. 1 root root    7 4月  13 14:33 ddd -> ./dir4/

    [root@liunxandshell ~]# cd ddd
    [root@liunxandshell ddd]# pwd
    /root/ddd

18.其他命令

1.查找文件:

1.find

            [root@liunxandshell ~]# find / -name "*1.log*"
            /root/dir4/1.log
            /root/3_1.log
            /root/1.log
            /root/app/1.log
            /tmp/1.log

2.locate

2.系统相关的命令:

1.内存

            free [-h]

2.磁盘

            df [-h]

3.负载

            top 
            load average: 0.00, 0.01, 0.05 【三个数各自不要超过10】

3.可能遇到的坑:

        编辑文件: swp文件
        突然掉电了,先删除 .xxx.swp文件
        [root@liunxandshell ~]# rm -rf ./.1.log.swp 

19.进程+端口号:

    yum install -y httpd

1.安装httpd

        [root@liunxandshell ~]# yum install -y httpd

2.查看httpd状态

        [root@liunxandshell ~]# service httpd status

3.启动httpd

        [root@liunxandshell ~]# service httpd start 

1.查看进程号:

        1.java 代码 => jdk => jps
        2.ps -ef | grep 程序的名字(pid) => pid

                   pid    父pid
        root       7349      1  0 14:53 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
        apache     7363   7349  0 14:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
        apache     7365   7349  0 14:54 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

        3.杀死程序
        [root@liunxandshell ~]# kill -9 7363

2.查看端口号 port

        netstat -plt | grep pid

        [root@liunxandshell ~]# netstat -nlp | grep 7349
        tcp6       0      0 :::80                   :::*                    LISTEN      7349/httpd 

20.通讯的命令

1.ping ip

2.telnet ip port

21.下载到本地

    1.wget
    2.curl

    使用:命令 + url

22.tar

语法

tar => 命令 => 归档文件
普通文件: 1.log 100m
归档文件:1.log.tar 100m
归档文件 => 压缩的归档文件:zip gz
1.log.tar.zip
2.log.tar.gz 99.953%

    1.归档文件

    2.1.log.tar.gz => 1.log
        tar -zxvf   xxx.tar.gz  【解压gz的tar文件】
        tar -zcvf   xxx  =>   xxx.tar.gz 【压缩tar文件gz】

        tar -cvf 1.log.tar 1.log 【1.log => 归档】
        tar -xvf 1.log.tar -C dir4/ 【归档 => 1.log】

        -c, --create               创建一个新归档
        -x, --extract, --get       从归档中解出文件
        -f, log.tar => 1.log       将tar文件变成普通文件
        -v, --verbose              详细地列出处理的文件

案例

        [root@liunxandshell ~]# tar -zcvf 12.log.tar.gz 1.log
        1.log
        [root@liunxandshell ~]# tar -zxvf 12.log.tar.gz 
        1.log

        [root@liunxandshell ~]# tar -cvf 1.log.tar 1.log 【1.log => 归档】
        1.log
        [root@liunxandshell ~]# tar -xvf 1.log.tar -C dir4/ 【归档 => 1.log】
        1.log


        注意:  
            不带压缩 z 这个参数 就不要添加

23.tar【teacher】

    tar:
        linux 处理 归档文件的命令
    归档文件:

    普通文件:1.log         1m
    归档文件1.log.tar       1m 
    压缩文件:1.log.tar.gz  0.9m

1.压缩

    普通 => 压缩 归档文件
        1.log => 1.log.tar.gz
        tar -zcvf xxx.tar.gz file...
            -cf => 创建一个新归档 + 使用归档文件
            -z => 压缩gz
            -v => 详细地列出处理的文件

案例

            [root@liunxandshell ~]# tar -zcvf dir.tar.gz 1.log app
            1.log
            app/
            app/1.log

        -cf => 创建一个新归档 + 使用归档文件

2.解压

    压缩 归档文件 => 普通
        tar -zxvf xxx.tar.gz
        -xf => 解压 + 使用归档文件
        -z => 压缩gz
        -v => 详细地列出处理的文件

案例

            [root@liunxandshell ~]# tar -zxvf dir.tar.gz -C ./dir4
            1.log
            app/
            app/1.log
posted @ 2022-04-13 15:34  咸鱼QwQ  阅读(53)  评论(0)    收藏  举报