Chapter 8 Common Linux Commands

Chapter 8 Common Linux Commands

learning target

1 Understand Linux help commands

2 Familiar with the switch command

3 Familiar with file and directory commands

4 Familiar with time and date commands

5 Familiar with user management commands

6 Familiarize yourself with group management commands

7 Proficiency in file permission commands

8 Familiar with search and lookup commands

9 Proficiency in compression and decompression commands

10 Familiar with disk partition commands

11 Proficiency in process thread commands

12 Understanding system timing task commands

The first section help commands

man Get help information

  • grammar
    • man [command or configuration file] (function description: get help information)
  • show instructions
    information Function
    NAME The name and one-line description of the command
    SYNOPSIS how to use the command
    DESCRIPTION In-Depth Discussion of Command Functions
    EXAMPLES Example of how to use the command
    SEE ALSO Related topics (usually man pages)
  • Practical case
    • View the help information of the ls command
    [root@hadoop101 ~]# man ls
    

help Get help information for shell built-in commands

  • basic grammar
    • help command (function description: get help information for shell built-in commands)
  • Case Practice
    • View the help information of the cd command
    [root@hadoop101 ~]# help cd
    

Section 2 Power On and Off Commands

Generally, Linux does not shut down frequently. The correct shutdown process is: sync>shutdown> reboot >poweroff

  • basic grammar

    • sync Synchronize the data in the memory to the hard disk
    • poweroff shuts down the system, equivalent to shutdown -h now
    • reboot Restart the system, equivalent to shutdown -r now
    • shutdown[options] time
  • options

    options Function
    -h -h=halt shutdown
    -r -r=reboot restart
  • parameter

    parameter Function
    now shut down immediately
    time How long to wait before shutting down, in minutes
  • Precautions

    In order to improve the read and write efficiency of the disk, the Linux system adopts the "pre-read and write late" operation method for the disk. When the user saves the file, the Linux kernel does not necessarily immediately write the saved data to the physical disk, but saves the data in the In the buffer, write to the disk when the buffer is full. This method can greatly improve the efficiency of writing data to the disk. It also brings security risks. If the data has not been written to the disk, the system will be powered off Or other serious problems cause downtime, which may cause data loss. Use the sync command to immediately write the data in the buffer to the disk

Section 3 File Directory Commands

pwd(print working directory) prints the absolute path of the current working directory

  • basic grammar
    • pwd (function description: display the absolute path of the current working directory)
  • Case Practice
    • Display the absolute path of the current working directory
    [root@hadoop101 ~]# pwd
    /root
    

ls(list) list directory contents

  • basic grammar

    • ls [options] [directory or file]
  • Option Description

    options Function
    -a All files are listed together with hidden files (files beginning with .) (commonly used)
    -l List of long data strings, including data such as file attributes and permissions; (commonly used)
  • show instructions

    The information listed in each line is: file type and permission link number file owner file group file size in bytes to indicate the time of creation or last modification name

  • Practical case

    • View all content information of the current directory
    [atguigu@hadoop101 ~]$ ls -al
    总用量 44
    drwx------. 5 atguigu atguigu 4096 5月  27 15:15 .
    drwxr-xr-x. 3 root    root    4096 5月  27 14:03 ..
    drwxrwxrwx. 2 root    root    4096 5月  27 14:14 hello
    -rwxrw-r--. 1 atguigu atguigu   34 5月  27 14:20 test.txt
    

cd (Change Directory) switch path

  • basic grammar
    • cd [parameters]
  • Parameter Description
    parameter Function
    cd absolute path switch path
    cd relative path switch path
    cd ~ or cd back to your home directory
    cd - Return to the previous directory
    cd … Return to the previous directory of the current directory
    cd -P Jump to the actual physical path, not the shortcut path
    cd / Back to the system root directory
  • Practical case
    • Use an absolute path to switch to the root directory
    [root@hadoop101 ~]# cd /root/
    
    • Change to the "public" directory using a relative path
    [root@hadoop101 ~]# cd 公共的/
    
    • Indicates returning to your home directory, which is the /root directory
    [root@hadoop101 公共的]# cd ~
    
    • cd - Back to the last directory
    [root@hadoop101 ~]# cd -
    
    • Indicates returning to the upper-level directory of the current directory, that is, the upper-level directory of "/root/public";
    [root@hadoop101 公共的]# cd ..
    

mkdir (Make directory) to create a directory

  • basic grammar
    • mkdir [options] directory to create
  • Option Description
    options Function
    -p Create multi-level directories
  • Practical case
    • create a directory
    [root@hadoop101 ~]# mkdir xiyou
    
    [root@hadoop101 ~]# mkdir xiyou/mingjie
    
    • Create a multi-level directory
    [root@hadoop101 ~]# mkdir -p xiyou/dssz/meihouwang
    

rmdir(Remove directory) delete directory

  • basic grammar
    • rmdir Empty directory to delete
  • Practical case
    • delete an empty folder
    [root@hadoop101 ~]# rmdir xiyou/dssz/meihouwang
    

touch creates an empty file

  • basic grammar

    • touch file name
  • Practical case

    [root@hadoop101 ~]# touch xiyou/dssz/sunwukong.txt
    
  • Precautions

    vim can also create files, vim atguigu.txt enters the editing mode, then save and exit the input content, but if you exit directly with an empty file without outputting the content, the file will not be created

cp copies a file or directory

  • basic grammar

    • cp [option] source dest (function description: copy source file to dest)
  • Option Description

    options Function
    -r copy entire folder recursively
  • Parameter Description

    parameter Function
    source Source File
    dest Target file
  • Practical case

    • copy files
    [root@hadoop101 ~]# cp xiyou/dssz/suwukong.txt xiyou/mingjie/
    
    • copy entire folder recursively
    [root@hadoop101 ~]# cp -r a/b/ ./
    
  • Precautions

    Forcibly override methods that do not prompt: \cp

rm removes a file or directory

  • basic grammar
    • rm [options] deleteFile
  • Option Description
    options Function
    -r Recursively delete all contents of a directory
    -f Force delete without prompting user for confirmation
    -v Display the detailed execution process of the command
  • Practical case
    • delete the contents of the directory
    [root@hadoop101 ~]# rm xiyou/mingjie/sunwukong.txt
    
    • Recursively delete all contents of a directory
    [root@hadoop101 ~]# rm -rf  dssz/
    

mv moves files and directories or renames them

  • basic grammar
    • double naming
    mv oldNameFile newNameFile
    
    • move files
    mv /temp/movefile /targetFolder
    
  • Practical case
    • double naming
    [root@hadoop101 ~]# mv xiyou/dssz/suwukong.txt xiyou/dssz/houge.txt
    
    • move files
    [root@hadoop101 ~]# mv xiyou/dssz/houge.txt ./
    

cat view file content

  • basic grammar
    • cat [option] file to view the contents of the file, starting from the first line
  • Option Description
    options Functional description
    - n Display line numbers for all lines, including blank lines
  • Skills
    一般查看比较小的文件,一屏幕能显示全的
    
  • Practical case
    • View file contents and display line numbers
    [atguigu@hadoop101 ~]$ cat -n houge.txt 
    

more file split screen viewer

  • basic grammar
     more 要查看的文件
    
    
    more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键,详见操作说明。
    
  • Instructions
    operate Function Description
    blank key (space) Represents turning down one page;
    Enter Represents turning down "one line";
    q The representative leaves more immediately, and no longer displays the content of the file.
    Ctrl+F scroll down one screen
    Ctrl+B back to previous screen
    = output the line number of the current line
    :f output the filename and line number of the current line
  • Practical case
    • (1) Use more to view files
    [root@hadoop101 ~]# more smartd.conf
    

less Display file content in split screen

  • basic grammar
    less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。
    
    less 要查看的文件
    
  • Instructions
    operate Function Description
    blank key turn down one page;
    [pagedown] scroll down one page
    [shut up] turn up one page;
    /string The function of searching "string" downward; n: search downward; N: search upward;
    ?string The function of searching "string" upward; n: search upward; N: search downward;
    q Leave the less program;
  • Practical case
    • (1) Use less to view files
    [root@hadoop101 ~]# less smartd.conf
    

head displays the contents of the file header

  • basic grammar
    head用于显示文件的开头部分内容,默认情况下head指令显示文件的前10行内容。
    
    head 文件      (功能描述:查看文件头10行内容)
    head -n 5 文件   (功能描述:查看文件头5行内容,5可以是任意行数)
    
  • Option Description
    options Function
    -n <number of lines> Specifies the number of lines to display header content
  • Practical case
    • (1) View the first 2 lines of the file
    [root@hadoop101 ~]# head -n 2 smartd.conf
    

tail output the tail of the file

  • basic grammar
    tail用于输出文件中尾部的内容,默认情况下tail指令显示文件的后10行内容。
    (1)tail 文件          (功能描述:查看文件后10行内容)
    (2)tail -n 5 文件     (功能描述:查看文件后5行内容,5可以是任意行数)
    (3)tail -f 文件      (功能描述:实时追踪该文档的所有更新)
    
  • Option Description
    options Function
    -n<number of lines> Output the content of n lines at the end of the file
    -f Display the latest appended content of the file and monitor file changes
  • Practical case
    • (1) View the content of the first line of the file
    [root@hadoop101 ~]# tail -n 1 smartd.conf 
    
    • (2) Track all updates of the file in real time
    [root@hadoop101 ~]# tail -f houge.txt
    

echo print information

  • basic grammar
     echo输出内容到控制台  System.out.println();
     
     echo [选项] [输出内容]
    
  • Option Description
    options Function
    -e Support character conversion controlled by backslash
    control character effect
    ---- -----------
    \\ output\itself
    \n line break
    \t Tab character, that is, the Tab key
  • Practical case
    • (1) Print text information
    [atguigu@hadoop101 ~]$ echo "hello\tworld"
    hello\tworld
    [atguigu@hadoop101 ~]$ echo -e "hello\tworld"
    hello   world
    
    • (2) Print environment variables
    [atguigu@hadoop101 ~]$ echo $PATH
    

> overwrite and >>append

  • basic grammar
    (1)ll >文件       (功能描述:列表的内容写入文件a.txt中(**覆盖写**))
    (2)ll >>文件      (功能描述:列表的内容**追加**到文件aa.txt的末尾)
    (3)cat 文件1 > 文件2 (功能描述:将文件1的内容覆盖到文件2)
    (4)echo “内容” >> 文件
    
  • Practical case
    • (1) Write the ls view information to the file
    [root@hadoop101 ~]# ls -l>houge.txt
    
    • (2) Append the ls view information to the file
    [root@hadoop101 ~]# ls -l>>houge.txt
    
    • (3) Use echo to append the word hello to the file
    [root@hadoop101 ~]# echo hello>>houge.txt
    

ln creates links and soft links

  • basic grammar
    链接表示目标资源的另外的访问方式,表示一种路径
    软链接也称为符号链接,类似于windows里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。
    ln [-s] [原文件或目录] [链接名]       (功能描述:给原文件创建一个链接)
    
  • Option Description
    options Function
    -s 创建的链接为 软连接
  • 经验技巧
    删除软链接: rm -rf 软链接名,而不是rm -rf 软链接名/
    查询:通过ll就可以查看,列表属性第1位是l,尾部会有位置指向。
    
  • 实操案例
    • (1)创建软连接
    [root@hadoop101 ~]# mv houge.txt xiyou/dssz/
    [root@hadoop101 ~]# ln -s xiyou/dssz/houge.txt houzi
    [root@hadoop101 ~]# ll
    lrwxrwxrwx. 1 root  root   20 6月 17 12:56 houzi -> xiyou/dssz/houge.txt
    
    • (2)删除软连接
    [root@hadoop101 ~]# rm -rf houzi
    
    • (3)进入软连接实际物理路径
    [root@hadoop101 ~]# ln -s xiyou/dssz/ ./dssz
    [root@hadoop101 ~]# cd -P dssz/
    

history查看历史命令

  • 基本语法
     history                    (功能描述:查看已经执行过历史命令)
    
  • 实操案例
    • (1)查看已经执行过的历史命令
    [root@hadoop101 test1]# history
    
    • (2) /root/.bash_history文件中也是历史命令
    less /root/.bash_history
    

第四节 时间日期类命令

date显示当前时间

  • 基本语法
    (1)date                            (功能描述:显示当前时间)
    ​(2)date +%Y                        (功能描述:显示当前年份)
    ​(3)date +%m                        (功能描述:显示当前月份)
    ​(4)date +%d                        (功能描述:显示当前是哪一天)
    ​(5)date "+%Y-%m-%d %H:%M:%S"       (功能描述:显示年月日时分秒)
    
  • 实操案例
    • (1)显示当前时间信息
    [root@hadoop101 ~]# date
    2017年 06月 19日 星期一 20:53:30 CST
    
    
    • (2)显示当前时间年月日
    [root@hadoop101 ~]# date +%Y%m%d
    20170619
    
    
    • (3)显示当前时间年月日时分秒
    [root@hadoop101 ~]# date "+%Y-%m-%d %H:%M:%S"
    2017-06-19 20:54:58
    
    

date显示非当前时间

  • 基本语法
    (1)date -d '1 days ago'          (功能描述:显示前一天时间)
    (2)date -d '-1 days ago'         (功能描述:显示明天时间)
    
  • 实操案例
    • (1)显示前一天
    [root@hadoop101 ~]# date -d '1 days ago'
    2017年 06月 18日 星期日 21:07:22 CST
    
    
    • (2)显示明天时间
    [root@hadoop101 ~]#date -d '-1 days ago'
    2017年 06月 20日 星期日 21:07:22 CST
    
    

date设置系统时间

  • 基本语法
      date -s 字符串时间
    
  • 实操案例
    • (1)设置系统当前时间
    [root@hadoop101 ~]# date -s "2017-06-19 20:52:18"
    

cal查看日历

  • 基本语法
    cal [选项]          (功能描述:不加选项,显示本月日历)
    
  • 选项说明
    选项 功能
    具体某一年 显示这一年的日历
  • 实操案例
    • (1)查看当前月的日历
    [root@hadoop101 ~]# cal
    
    • (2)查看2017年的日历
    [root@hadoop101 ~]# cal 2017
    

第五节 用户管理命令

useradd添加新用户

  • 基本语法
    useradd 用户名                (功能描述:添加新用户)
    ​useradd -g 组名 用户名         (功能描述:添加新用户到某个组)
    
  • 实操案例
    • (1)添加一个用户
    [root@hadoop101 ~]# useradd tangseng
    [root@hadoop101 ~]#ll /home/
    

passwd设置用户密码

  • 基本语法
    passwd 用户名   (功能描述:设置用户密码)
    
  • 实操案例
    • (1)设置用户的密码
    [root@hadoop101 ~]# passwd tangseng
    

id查看用户是否存在

  • 基本语法
    id 用户名
    
  • 实操案例
    • (1)查看用户是否存在
    [root@hadoop101 ~]#id tangseng
    

cat /etc/passwd 查看创建的所有用户

  • 实操案例
    • (1) 查看创建的所有用户
    [root@hadoop101 ~]# cat /etc/passwd
    

su(switch user )切换用户

  • 基本语法
    su 用户名称      (功能描述:切换用户,只能获得用户的执行权限,不能获得环境变量)
    su - 用户名称    (功能描述:切换到用户并获得该用户的环境变量及执行权限)
    
  • 实操案例
    • (1)切换用户
    [root@hadoop101 ~]#su tangseng
    [root@hadoop101 ~]#echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [root@hadoop101 ~]#exit
    [root@hadoop101 ~]#su - tangseng
    [root@hadoop101 ~]#echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/tangseng/bin
    
    • (2) exit 回退到上一个用户
    [root@hadoop101 ~]#exit
    

userdel删除用户

  • 基本语法
    (1)userdel 用户名          (功能描述:删除用户但保存用户主目录)
    ​(2)userdel -r 用户名       (功能描述:用户和用户主目录,都删除)
    
  • 选项说明
    选项 功能
    -r 删除用户的同时,删除与用户相关的所有文件。
  • 实操案例
    • (1)删除用户但保存用户主目录
    [root@hadoop101 ~]#userdel tangseng
    [root@hadoop101 ~]#ll /home/
    
    • (2)删除用户和用户主目录,都删除
    [root@hadoop101 ~]#useradd zhubajie
    [root@hadoop101 ~]#ll /home/
    [root@hadoop101 ~]#userdel -r zhubajie
    [root@hadoop101 ~]#ll /home/
    

who 查看登录用户信息

  • 基本语法
    ​(1)whoami           (功能描述:显示自身用户名称)
    ​(2)who am i         (功能描述:显示登录用户的用户名)
    
  • 案例实操
    • (1)显示自身用户名称
    [root@hadoop101 opt]# whoami
    
    • (2)显示登录用户的用户名
    [root@hadoop101 opt]# who am i
    

sudo 设置普通用户具有root权限

  • 基本语法
    sudo 命令
    
  • 实操案例
    • (1) 添加atguigu用户,并对其设置密码
    [root@hadoop101 ~]#useradd atguigu
    [root@hadoop101 ~]#passwd atguigu
    
    • (2)修改配置文件
    [root@hadoop101 ~]#vi /etc/sudoers
    
    修改 /etc/sudoers 文件,找到下面一行(91行),在root下面添加一行,如下
    
    \## Allow root to run any commands anywhere
    root  ALL=(ALL)   ALL
    atguigu  ALL=(ALL)   ALL
    
    或者配置成采用sudo命令时,不需要输入密码
    
    \## Allow root to run any commands anywhere
    root   ALL=(ALL)   ALL
    atguigu  ALL=(ALL)   NOPASSWD:ALL
    
    修改完毕,现在可以用atguigu帐号登录,然后用命令 sudo ,即可获得root权限进行操作。
    
    • (3)用普通用户在/opt目录下创建一个文件夹
    [atguigu@hadoop101 opt]$ sudo mkdir module
    

usermod修改用户

  • 基本语法
    usermod -g 用户组 用户名
    
  • 选项说明
    选项 功能
    -g 修改用户的初始登录组,给定的组必须存在
  • 实操案例
    • (1)将用户加入到用户组
    [root@hadoop101 opt]#usermod -g root zhubajie
    

第六节 组管理类命令

每个用户都有一个用户组,系统可以对一个用户组中的所有用户进行集中管理。不同Linux 系统对用户组的规定有所不同,如Linux下的用户属于与它同名的用户组,这个用户组在创建用户时同时创建。用户组的管理涉及用户组的添加、删除和修改。组的增加、删除和修改实际上就是对/etc/group文件的更新。

groupadd新增组

  • 基本语法
    groupadd 组名
    
  • 实操案例
    • (1)添加一个xitianqujing组
    [root@hadoop101 opt]#groupadd xitianqujing
    

groupdel删除组

  • 基本语法
    groupdel 组名
    
  • 实操案例
    • (1)删除xitianqujing组
    [root@hadoop101 opt]# groupdel xitianqujing
    

查看创建了那些组

  • 实操案例
    [root@hadoop101 atguigu]# cat  /etc/group
    

第七节 文件权限类命令

文件属性信息解读

  • 文件类型和权限的表示

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    • (1) 0首位表示类型 在Linux中第一个字符代表这个文件是目录、文件或链接文件等等
      符号 对应文件类型
      - 代表文件
      d d 代表目录
      l 链接文档(link file);
    • (2)第1-3位确定属主(该文件的所有者)拥有该文件的权限。U →User
    • (3)第4-6位确定属组(所有者的同组用户)拥有该文件的权限,G→Group
    • (4)第7-9位确定其他用户拥有该文件的权限 , O →Other
  • rwx作用到目录和文件的不同含义

    • 作用到文件
    [ r ]代表可读(read): 可以读取,查看
    ​[ w ]代表可写(write): 可以修改,但是不能删除该文件,对该文件所在的目录有写权限,才能删除.
    ​[ x ]代表可执行(execute):可以被系统执行
    
    • 作用到目录
    [ r ]代表可读(read): 可以读取,ls查看目录内容
    ​[ w ]代表可写(write): 可以修改,目录内创建+删除+重命名目录
    [ x ]代表可执行(execute):可以进入该目录
    
  • 实操案例

    • (1)查看文件权限信息
    [root@hadoop101 ~]# ll
    总用量 104
    -rw-------. 1 root root 1248 1月  8 17:36 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 4096 1月 12 14:02 dssz
    lrwxrwxrwx. 1 root root  20 1月 12 14:32 houzi -> xiyou/dssz/houge.tx
    
    
    • (2)文件属性介绍
    ls -l
    

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    ** 如果查看到是文件:链接数指的是硬链接个数**
    ** 如果查看的是文件夹:链接数指的是子文件夹个数 **​

chmod改变文件权限

  • 基本语法

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    • 第一种方式变更权限
    chmod [{ugoa}{+-=}{rwx}] 文件或目录
    
    • 第二种方式变更权限
    chmod [mode=421 ] [文件或目录]
    
  • 经验技巧

    u:所有者 g:所有组 o:其他人 a:所有人(u、g、o的总和)
    ​r=4 w=2 x=1         
    rwx=4+2+1=7
    
  • 实操案例

    • (1)修改文件使其所属主用户具有执行权限
    [root@hadoop101 ~]# cp xiyou/dssz/houge.txt ./
    [root@hadoop101 ~]# chmod u+x houge.txt
    
    • (2)修改文件使其所属组用户具有执行权限
    [root@hadoop101 ~]# chmod g+x houge.txt
    
    • (3)修改文件所属主用户执行权限,并使其他用户具有执行权限
    [root@hadoop101 ~]# chmod u-x,o+x houge.txt
    
    • (4)采用数字的方式,设置文件所有者、所属组、其他用户都具有可读可写可执行权限。
    [root@hadoop101 ~]# chmod 777 houge.txt
    
    • (5)修改整个文件夹里面的所有文件的所有者、所属组、其他用户都具有可读写执行权限。
    [root@hadoop101 ~]# chmod -R 777 xiyou/
    

chown 改变所有者

  • 基本语法
    chown [选项] [最终用户] [文件或目录]     (功能描述:改变文件或者目录的所有者)
    
  • 选项说明
    选项 功能
    -R 递归操作
  • 实操案例
    • (1)修改文件所有者
    [root@hadoop101 ~]# chown atguigu houge.txt 
    [root@hadoop101 ~]# ls -al
    -rwxrwxrwx. 1 atguigu root 551 5月 23 13:02 houge.txt
    
    • (2)递归改变文件所有者和所有组
    [root@hadoop101 xiyou]# ll
    drwxrwxrwx. 2 root root 4096 9月  3 21:20 xiyou
    [root@hadoop101 xiyou]# chown -R atguigu:atguigu xiyou/
    [root@hadoop101 xiyou]# ll
    drwxrwxrwx. 2 atguigu atguigu 4096 9月  3 21:20 xiyou
    

chgrp改变所属组

  • 基本语法
    chgrp [最终用户组] [文件或目录]   (功能描述:改变文件或者目录的所属组)
    
  • 实操案例
    • (1)修改文件的所属组
    [root@hadoop101 ~]# chgrp root houge.txt
    [root@hadoop101 ~]# ls -al
    -rwxrwxrwx. 1 atguigu root 551 5月 23 13:02 houge.txt
    

第八节 搜索查找类命令

find 查找文件或者目录

  • 基本语法
    find指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件显示在终端。
    find [搜索范围] [选项]
    
    
  • 选项说明
    选项 功能
    -name<查询方式> 按照指定的文件名查找模式查找文件
    -user<用户名> 查找属于指定用户名所有文件
    -size<文件大小> 按照指定的文件大小查找文件。
  • 实操案例
    • (1)按文件名:根据名称查找/目录下的filename.txt文件。
    [root@hadoop101 ~]# find xiyou/ -name “*.txt”
    
    • (2)按拥有者:查找/opt目录下,用户名称为-user的文件
    [root@hadoop101 ~]# find opt/ -user atguigu
    
    • (3)按文件大小:在/home目录下查找大于200m的文件(+n 大于 -n小于 n等于)
    [root@hadoop101 ~]find /home -size +204800 
    

grep 过滤查找及“|”管道符

  • 基本语法
    管道符,“|”,表示将前一个命令的处理结果输出传递给后面的命令处理
    grep 选项 查找内容 源文件    select * from  emp where ename like '%a%'
    
    
  • 选项说明
    选项 功能
    -n 显示匹配及行号
  • 实操案例
    • (1)查找某文件在第几行
    [root@hadoop101 ~]# ls | grep -n test
    

上传下载命令

  • 基本语法
    上传:rz
    下载:sz 文件名
    

第九节 压缩和解压缩命令

gzip/gunzip 压缩

  • 基本语法
    gzip 文件       (功能描述:压缩文件,只能将文件压缩为*.gz文件)
    gunzip 文件.gz  (功能描述:解压缩文件命令)
    
  • 经验技巧
    (1)只能压缩文件,不能压缩目录
    (2)不保留原来的文件
    
  • 实操案例
    • (1)gzip压缩
    [root@hadoop101 ~]# ls
    houge.txt
    [root@hadoop101 ~]# gzip houge.txt
    [root@hadoop101 ~]# ls
    houge.txt.gz
    
    • (2)gunzip解压缩文件
    [root@hadoop101 ~]# gunzip houge.txt.gz 
    [root@hadoop101 ~]# ls
    houge.txt
    

zip/unzip压缩

  • 基本语法
    zip [选项] XXX.zip 将要压缩的内容     (功能描述:压缩文件和目录的命令)
    ​unzip [选项] XXX.zip                (功能描述:解压缩文件)
    
  • 选项说明
    zip选项 功能
    -r 压缩目录
    unzip选项 功能
    ------- ------------
    -d<目录> 指定解压后文件的存放目录
  • 经验技巧
    zip 压缩命令在window/linux都通用,**可以压缩目录且保留源文件**。
    
  • 实操案例
    • (1)压缩 1.txt 和2.txt,压缩后的名称为mypackage.zip
    [root@hadoop101 opt]# touch bailongma.txt
    [root@hadoop101 ~]# zip houma.zip houge.txt bailongma.txt 
     adding: houge.txt (stored 0%)
     adding: bailongma.txt (stored 0%)
    [root@hadoop101 opt]# ls
    houge.txt bailongma.txt  houma.zip 
    
    • (2)解压 mypackage.zip
    [root@hadoop101 ~]# unzip houma.zip 
     Archive: houma.zip
     extracting: houge.txt        
     extracting: bailongma.txt    
    [root@hadoop101 ~]# ls
    houge.txt bailongma.txt  houma.zip
    
    • (3)解压mypackage.zip到指定目录-d
    [root@hadoop101 ~]# unzip houma.zip -d /opt
    [root@hadoop101 ~]# ls /opt/
    

tar打包

  • 基本语法
    tar [选项] XXX.tar.gz 将要打包进去的内容  (功能描述:打包目录,压缩后的文件格式.tar.gz)
    
  • 选项说明
    选项 功能
    -z 打包同时压缩
    -c 产生.tar打包文件
    -v 显示详细信息
    -f 指定压缩后的文件名
    -x 解包.tar文件
  • 实操案例
    • (1)压缩多个文件
    [root@hadoop101 opt]# tar -zcvf houma.tar.gz houge.txt bailongma.txt 
    houge.txt
    bailongma.txt
    [root@hadoop101 opt]# ls
    houma.tar.gz houge.txt bailongma.txt 
    
    • (2)压缩目录
    [root@hadoop101 ~]# tar -zcvf xiyou.tar.gz xiyou/
    xiyou/
    xiyou/mingjie/
    xiyou/dssz/
    xiyou/dssz/houge.txt
    
    • (3)解压到当前目录
    [root@hadoop101 ~]# tar -zxvf houma.tar.gz
    
    • (4)解压到指定目录
    [root@hadoop101 ~]# tar -zxvf xiyou.tar.gz -C /opt
    [root@hadoop101 ~]# ll /opt/
    

第十节 磁盘分区类命令

df (disk free 空余硬盘)查看磁盘空间使用情况

  • 基本语法
     df 选项 (功能描述:列出文件系统的整体磁盘使用量,检查文件系统的磁盘空间占用情况)
    
  • 选项说明
    选项 说明
    -h 以人们较易阅读的 GBytes, MBytes, KBytes 等格式自行显示;
  • 实操案例
    • (1)查看磁盘使用情况
    [root@hadoop101 ~]# df -h
    Filesystem   Size Used Avail Use% Mounted on
    /dev/sda2    15G 3.5G  11G 26% /
    tmpfs      939M 224K 939M  1% /dev/shm
    

fdisk 查看分区

  • 基本语法
     fdisk -l         (功能描述:查看磁盘分区详情)
    
  • 选项说明
    选项 功能
    -l 显示所有硬盘的分区列表
  • 经验技巧
    • 该命令必须在root用户下才能使用
  • 功能说明
    • (1)Linux分区
    Device:分区序列
    ​Boot:引导
    ​Start:从X磁柱开始
    ​End:到Y磁柱结束
    ​Blocks:容量
    ​Id:分区类型ID
    ​System:分区类型
    
    
    
    • (2)windows分区
      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
  • 实操案例
    • (1)查看系统分区情况
    [root@hadoop101 /]# fdisk -l
    Disk /dev/sda: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0005e654
    
      Device Boot   Start     End   Blocks  Id System
    /dev/sda1  *      1     26   204800  83 Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2       26    1332  10485760  83 Linux
    /dev/sda3      1332    1593   2097152  82 Linux swap / Solaris
    

mount/umount 挂载/卸载

  • 什么是挂载卸载

    对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录、一个独立且唯一的文件结构。
    Linux中每个分区都是用来组成整个文件系统的一部分,它在用一种叫做“挂载”的处理方法,它整个文件系统中包含了一整套的文件和目录,并将一个分区和一个目录联系起来,要载入的那个分区将使它的存储空间在这个目录下获得。
    
    • (1)挂载前准备(必须要有光盘或者已经连接镜像文件)
      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
  • 基本语法

    mount [-t vfstype] [-o options] device dir (功能描述:挂载设备)
    umount 设备文件名或挂载点         (功能描述:卸载设备)
    
  • 参数说明

    参数 功能
    -t vfstype 指定文件系统的类型,通常不必指定。mount 会自动选择正确的类型。常用类型有: 1)光盘或光盘镜像:iso9660 2)DOS fat16文件系统:msdos 3) Windows 9x fat32文件系统:vfat 4) Windows NT ntfs文件系统:ntfs 5)Mount Windows文件网络共享:smbfs 6) UNIX (LINUX) 文件网络共享:nfs
    -o options 主要用来描述设备或档案的挂接方式。常用的参数有: loop:用来把一个文件当成硬盘分区挂接上系统 ro:采用只读方式挂接设备 rw:采用读写方式挂接设备   iocharset:指定访问文件系统所用字符集
    device 要挂接(mount)的设备
    dir 设备在系统上的挂接点(mount point)
  • 实操案例

    • (1)挂载光盘镜像文件
    [root@hadoop101 ~]# mkdir /mnt/cdrom/          (建立挂载点)
    [root@hadoop101 ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom/  (设备/dev/cdrom挂载到/mnt/cdrom中)
    [root@hadoop101 ~]# ll /mnt/cdrom/
    
    • (2)卸载光盘镜像文件
    [root@hadoop101 ~]# umount /mnt/cdrom
    
    • (3)设置开机自动挂载
    [root@hadoop101 ~]# vi /etc/fstab
    

    添加红框中内容,保存退出

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

第十一节 进程线程类命令

进程是正在执行的一个程序或命令,每一个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源。

ps (process status 进程状态)查看当前系统进程状态

  • 基本语法
    ps -aux | grep xxx     (功能描述:查看系统中所有进程)
    ​ps -ef  | grep xxx     (功能描述:可以查看子父进程之间的关系)
    
  • 选项说明
    选项 功能
    -a 选择所有进程
    -u 显示所有用户的所有进程
    -x 显示没有终端的进程
  • 功能说明
    • (1)ps -aux显示信息说明
    USER:该进程是由哪个用户产生的
    PID:进程的ID号
    ​%CPU:该进程占用CPU资源的百分比,占用越高,进程越耗费资源;
    ​%MEM:该进程占用物理内存的百分比,占用越高,进程越耗费资源;
    ​VSZ:该进程占用虚拟内存的大小,单位KB;
    ​RSS:该进程占用实际物理内存的大小,单位KB;
    ​TTY:该进程是在哪个终端中运行的。其中tty1-tty7代表本地控制台终端,tty1-tty6是本地的字符界面终端,    tty7是图形终端。pts/0-255代表虚拟终端。
    ​STAT:进程状态。常见的状态有:R:运行、S:睡眠、T:停止状态、s:包含子进程、+:位于后台
    ​START:该进程的启动时间
    ​TIME:该进程占用CPU的运算时间,注意不是系统时间
    ​COMMAND:产生此进程的命令名
    
    • (2)ps -ef显示信息说明
    UID:用户ID 
    ​PID:进程ID 
    ​PPID:父进程ID 
    ​C:CPU用于计算执行优先级的因子。数值越大,表明进程是CPU密集型运算,执行优先级会降低;数值越小,表明进程是I/O密集型运算,执行优先级会提高 
    ​STIME:进程启动的时间 
    ​TTY:完整的终端名称 
    ​TIME:CPU时间 
    ​CMD:启动进程所用的命令和参数
    
  • 经验技巧
    ​如果想查看进程的**CPU**占用率和内存占用率,可以使用aux;
    ​如果想查看**进程的父进程ID**可以使用ef;
    
  • 实操案例
    [root@hadoop101 datas]# ps -aux
    
    
    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
    [root@hadoop101 datas]# ps -ef
    
    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

kill终止进程

  • 基本语法
     kill [选项] 进程号 (功能描述:通过进程号杀死进程)
    ​ killall 进程名称   (功能描述:通过进程名称杀死进程,也支持通配符) 
    
  • 选项说明
    选项 功能
    -9 表示强迫进程立即停止
  • 实操案例
    • (1)杀死浏览器进程
    [root@hadoop101 桌面]# kill -9 5102
    
    • (2)通过进程名称杀死进程
    [root@hadoop101 桌面]# killall firefox
    

第十二节 系统定时任务命令

crond服务管理

  • 重新启动crond服务
    [root@hadoop101 ~]# systemctl restart crond
    

crontab 定时任务设置

  • 基本语法
    crontab [选项]
    
  • 选项说明
    选项 功能
    -e 编辑crontab定时任务
    -l 查询crontab任务
    -f 删除当前用户所有的crontab任务
  • 参数说明
    • (1)进入crontab编辑界面。会打开vim编辑你的工作,通过 "* " 定义任务周期。
      [root@hadoop101 ~]# crontab -e 
      
      项目 选项 范围
      第一个“ *” 一小时当中的第几分钟 0-59
      第二个“ *” 一天当中的第几小时 0-23
      第三个“ *” 一个月当中的第几天 1-31
      第四个“ *” 一年当中的第几月 1-12
      第五个“ *” 一周当中的星期几 0-7(0和7都代表星期日)
    • (2)每个星号上可以使用的特殊符号
      特殊符号 含义
      * 代表任何时间。比如第一个“ *”就代表一小时中每分钟都执行一次的意思。
      , 代表不连续的时间。比如“0 8,12,16 * * * 命令”,就代表在每天的8点0分,12点0分,16点0分都执行一次命令
      - 代表连续的时间范围。比如“0 5 * * 1-6命令”,代表在周一到周六的凌晨5点0分执行命令
      */n 代表每隔多久执行一次。比如“ */10 命令”,代表每隔10分钟就执行一遍命令
    • (3)特定时间执行命令
      时间 含义
      45 22 * * * 命令 在22点45分执行命令
      0 17 * * 1 命令 每周1 的17点0分执行命令
      0 5 1,15 * * 命令 每月1号和15号的凌晨5点0分执行命令
      40 4 * * 1-5 命令 每周一到周五的凌晨4点40分执行命令
      */10 4 * * * 命令 每天的凌晨4点,每隔10分钟执行一次命令
      0 0 1,15 * 1 命令(错误案例) 每月1号和15号,每周1的0点0分都会执行命令。
      注意:星期几和几号最好不要同时出现,因为他们定义的都是天。非常容易让管理员混乱。
      
  • 实参案例
    • (1)每隔1分钟,向/root/bailongma.txt文件中添加一一些内容
    */1 * * * * /bin/echo ”11” >> /root/bailongma.txt
    

Guess you like

Origin blog.csdn.net/leader_song/article/details/132575102