美文网首页Linux小推车Mac ProShell和命令
好玩实用的Linux初学者命令40个,Get!

好玩实用的Linux初学者命令40个,Get!

作者: 胜言_ | 来源:发表于2019-02-15 11:25 被阅读77次
    Wang的胜宴

    最近刚刚开始入门Linux,学了几天,感觉还可以,所以分享一些Linux的干货
    以后有机会还会分享的,有问题可以公众号:Wang的胜宴 消息发给我,我们一起探讨,这个号没有留言功能...
    现在,当下,正着手准备考研的事了。

    Tips:本文命令默认在Red Hat Enterprise Linux 7系统的root超级用户下演示。


    命令的一般格式:命令名称 [命令参数] [命令对象]
    命令名称、命令参数、命令对象之间请用空格键*分隔。

    命令参数还有长格式、短格式之分。

    表格1-1 命令参数的长格式与短格式示例
    长格式 man --help
    短格式 man -h

    - 你需要知道的Linux快捷键
    Ctrl + C 终止命令的执行
    Ctrl + Shift + C 复制
    Ctrl + Shift + V 粘贴

    干货预警!!!

    1. man man

    man 是manual [用户手册] 的简拼,这个命令是用来查看man命令自身的帮助信息,按q键退出帮助信息。

    [root@wang的胜宴 Desktop]# man man
    MAN(1)                                                     Manual pager utils                                                     MAN(1)
    
    NAME
           man - an interface to the on-line reference manuals
    
    SYNOPSIS
           man  [-C  file]  [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M path] [-S list] [-e extension]
           [-i|-I] [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt]  [-7]  [-E  encoding]  [--no-hyphen‐
           ation] [--no-justification] [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
           man -k [apropos options] regexp ...
           man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
           man -f [whatis options] page ...
           man  -l  [-C  file]  [-d]  [-D]  [--warnings[=warnings]]  [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p
           string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
           man -w|-W [-C file] [-d] [-D] page ...
           man -c [-C file] [-d] [-D] page ...
           man [-?V]
    
    ----------- 省略部分信息 ------------
    

    表1-2:man命令帮助信息的结构以及意义,了解一下即可。

    结构名称 代表意义
    NAME 命令的名称
    SYNOPSIS 参数的大致使用方法
    DESCRIPTION 介绍说明
    EXAMPLES 演示(附带简单说明)
    OVERVIEW 概述
    DEFAULTS 默认的功能
    OPTIONS 具体的可用选项(带介绍)
    ENVIRONMENT 环境变量
    FILES 用到的文件
    SEE ALSO 相关的资料
    HISTORY 维护历史与联系方式

    2. echo [字符串 or $变量]

    echo命令用于在终端控制台输出字符串或变量的值。

    Tips:变量名在Linux系统中一般为全大写的形式,echo $SHELL可以查看当前所用的shell解释器。

    [root@wang的胜宴 Desktop]# echo 'wang的胜宴'
    wang的胜宴
    [root@wang的胜宴 Desktop]# echo $SHELL
    /bin/bash
    

    3. date [选项] “+指定的格式”

    date 命令用于显示及设置系统的时间和日期。

    表1-3:date命令中的参数以及作用

    参数 作用
    %t 跳格[Tab键]
    %H 小时(00~23)
    %I 小时(00~12)
    %M 分钟(00~59)
    %S 秒(00~59)
    %j 今年中的第几天
    [root@wang的胜宴 Desktop]# date
    Thu Feb 14 10:56:24 CST 2019
    [root@wang的胜宴 Desktop]# date "+%Y-%m-%d %H:%M:%S"
    2019-02-14 10:57:02
    [root@wang的胜宴 Desktop]# date -s "20180522 08:30:00"
    Tue May 22 08:30:00 CST 2018
    [root@wang的胜宴 Desktop]# date "+%j"
    142
    

    4. reboot

    reboot命令用于重启电脑。

    5. poweroff

    poweroff命令用于关机。

    6. wget [参数] 下载地址

    wget命令用于在终端下载网络文件,类似于Python的爬虫。

    表1-4:wget命令的参数以及作用

    参数 作用
    -b 后台下载模式
    -P 下载到指定目录
    -t 最大尝试次数
    -c 断点续传
    -p 下载页面内所有资源,包括图片、视频等
    -r 递归下载
    [root@wang的胜宴 Desktop]# wget https://www.wang的胜宴.com/docs/wang的胜宴.pdf
    --2017-08-24 19:30:12 -- https://www.wang的胜宴.com/docs/wang的胜宴.pdf
    Resolving www.wang的胜宴.com (www.wang的胜宴.com)... 220.181.105.185
    Connecting to www.wang的胜宴.com (www.wang的胜宴.com)|220.181.105.185|:80...connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 45948568 (44M) [application/pdf]
    Saving to: ‘wang的胜宴.pdf’
    100%[===========================================>] 45,948,568 32.9MB/s in 1.3s
    2017-08-24 19:30:14 (32.9 MB/s) - ‘wang的胜宴.pdf’ saved [45948568/45948568]
    

    7. top

    top命令用于动态地监视进程活动与系统负载等信息。

    [root@wang的胜宴 Desktop]# top
    
    top - 08:34:55 up 17 min,  2 users,  load average: 0.00, 0.08, 0.15
    Tasks: 495 total,   1 running, 492 sleeping,   2 stopped,   0 zombie
    %Cpu(s):  1.1 us,  0.7 sy,  0.0 ni, 98.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem:   2035648 total,   874812 used,  1160836 free,      928 buffers
    KiB Swap:  2097148 total,        0 used,  2097148 free.   272132 cached Mem
    
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                
      1180 root      20   0  217300  31508   7796 S   3.6  1.5   0:11.88 Xorg                                                                   
      2375 root      20   0 1667972 228892  38632 S   3.0 11.2   0:55.15 gnome-shell                                                            
      2645 root      20   0  781580  20108  13064 S   2.0  1.0   0:02.88 gnome-terminal-                                                        
      2408 root      20   0  461356   7648   3364 S   0.7  0.4   0:01.58 ibus-daemon                                                            
       139 root      20   0       0      0      0 S   0.3  0.0   0:00.63 rcuos/1                                                                
       292 root      20   0       0      0      0 S   0.3  0.0   0:00.91 kworker/0:1                                                            
      2896 root      20   0  123920   1956   1152 R   0.3  0.1   0:00.12 top                                                                                                                     
    ----------- 省略部分信息 ------------                             
    

    8. uptime

    显示系统运行时间等信息。

    [root@wang的胜宴 Desktop]# uptime
     08:41:23 up 23 min,  2 users,  load average: 0.06, 0.04, 0.11
    

    9. free -h

    人性化显示系统内存相关信息。
    -m: 内存显示为单位为M的形式。

    [root@wang的胜宴 Desktop]# free -h
                 total       used       free     shared    buffers     cached
    Mem:          1.9G       874M       1.1G       9.9M       928K       267M
    -/+ buffers/cache:       605M       1.4G
    Swap:         2.0G         0B       2.0G
    [root@wang的胜宴 Desktop]# free -m
                 total       used       free     shared    buffers     cached
    Mem:          1987        874       1113          9          0        267
    -/+ buffers/cache:        605       1382
    Swap:         2047          0       2047
    

    10. ps aux

    ps aux命令用于查看系统中的进程状态。

    表1-5:ps命令的参数以及作用

    参数 作用
    -a 显示所有进程(包括其他用户的进程)
    -u 用户以及其他详细信息
    -x 显示没有控制终端的进程

    如前面所提到的,在Linux系统中的命令参数有长短格式之分,长格式和长格式之间不能合并,长格式和短格式之间也不能合并,但短格式和短格式之间是可以合并的,合并后仅保留一个-(减号)即可。另外ps命令可允许参数不加减号(-),因此可直接写成ps aux的样子。

    [root@wang的胜宴 Desktop]# ps aux
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root          1  0.1  0.3  53780  7696 ?        Ss   08:17   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
    root          2  0.0  0.0      0     0 ?        S    08:17   0:00 [kthreadd]
    root          3  0.0  0.0      0     0 ?        S    08:17   0:00 [ksoftirqd/0]
    root          5  0.0  0.0      0     0 ?        S<   08:17   0:00 [kworker/0:0H]
    root          7  0.0  0.0      0     0 ?        S    08:17   0:00 [migration/0]
    root          8  0.0  0.0      0     0 ?        S    08:17   0:00 [rcu_bh]
    ----------- 省略部分信息 ------------            
    

    Linux系统中时刻运行着许多进程,如果能够合理地管理它们,则可以优化系统的性能。在Linux系统中,有5种常见的进程状态,分别为运行、中断、不可中断、僵死与停止,其各自含义如下所示。
    R(Running:运行):进程正在运行或在运行队列中等待。
    S(Sleeping:中断):进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该 状态。
    D(不可中断):进程不响应系统异步信号,即便用kill命令也不能将其中断。
    Z(zombie:僵死):进程已经终止,但进程描述符依然存在, 直到父进程调用wait4()系统函数后将进程释放。
    T(stop:停止):进程收到停止信号后停止运行。

    11. pwd

    查看当前处于的目录路径。

    [root@wang的胜宴 Desktop]# pwd
    /root/Desktop
    

    12. ls -al

    查看当前目录下的所有文件。
    -a:显示所有文件,包括隐藏文件。
    -l:查看文件的属性、大小等详细信息。
    -d:查看目录属性信息。

    [root@wang的胜宴 Desktop]# ls
    chkhost.sh  chkscore.sh  example1.sh  example.sh  genuser.sh  mkcdrom.sh  users.txt
    [root@wang的胜宴 Desktop]# ls -al
    total 36
    drwxr-xr-x.  2 root root 4096 Feb 13  2019 .
    dr-xr-x---. 14 root root 4096 Feb 14  2019 ..
    -rw-r--r--.  1 root root  135 Feb 13  2019 chkhost.sh
    -rw-r--r--.  1 root root  293 Feb 13  2019 chkscore.sh
    -rw-r--r--.  1 root root  127 Feb 13  2019 example1.sh
    -rwxr--r--.  1 root root   54 Feb 13  2019 example.sh
    -rw-r--r--.  1 root root  340 Feb 13  2019 genuser.sh
    -rw-r--r--.  1 root root   98 Feb 13  2019 mkcdrom.sh
    -rw-r--r--.  1 root root   33 Feb 13  2019 users.txt
    [root@wang的胜宴 Desktop]# ls -ld 
    drwxr-xr-x. 2 root root 4096 Feb 13  2019 .
    

    13. cd

    cd命令用于打开指定目录。
    Cd ~:打开用户的home目录。

    Cd ..: 打开该目录的上级目录。

    Cd -: 打开上一个操作目录。

    [root@wang的胜宴 ~]# cd /media/cdrom
    [root@wang的胜宴 cdrom]# cd ..
    [root@wang的胜宴 media]# cd -
    /media/cdrom
    [root@wang的胜宴 cdrom]# cd ~
    [root@wang的胜宴 ~]# 
    

    14. mkdir

    mkdir命令用于创建空目录。
    -p:递归创建层叠目录。

    [root@wang的胜宴 Desktop]# mkdir wang的胜宴
    [root@wang的胜宴 Desktop]# mkdir -p a/b/c
    [root@wang的胜宴 Desktop]# ls
    a  chkhost.sh  chkscore.sh  example1.sh  example.sh  genuser.sh  mkcdrom.sh  users.txt  wang的胜宴
    [root@wang的胜宴 Desktop]# cd a
    [root@wang的胜宴 a]# cd b
    [root@wang的胜宴 b]# cd c
    [root@wang的胜宴 c]# 
    

    15. cat -n

    查看较少的文本文件。
    -n: 显示行号。

    [root@wang的胜宴 Desktop]# cat -n example.sh
         1  #!/bin/bash
         2  #For Example BY wang的胜宴.com
         3  pwd
         4  ls -al
    

    16. more

    可翻页查看文字较多的文本。

    [root@wang的胜宴 Desktop]# more /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    ----------- 省略部分信息 ------------
    chrony:x:994:993::/var/lib/chrony:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
    --More--(77%)
    

    17. head -n20 [文件名称]

    查看文本的前20行。

    [root@wang的胜宴 ~]# head -n20 anaconda-ks.cfg
    #version=RHEL7
    # System authorization information
    auth --enableshadow --passalgo=sha512
    
    # Use CDROM installation media
    cdrom
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
    network  --hostname=wang的胜宴.com
    # Root password
    rootpw --iscrypted $6$/BICADenrxneXgcq$jqSud/wsmjaYjyeHTKQ8QcP2.RJGbPS7.ZH9ICkG5GvikzJWkABG5m2Vd2V7H9mMdYW1/dVre70Eeynwtyrjm1
    # System timezone
    

    18. tail -n20 [文件名称]

    查看文本的后20行。

    [root@wang的胜宴 ~]# tail -n20 anaconda-ks.cfg
    # Partition clearing information
    clearpart --none --initlabel 
    
    %packages
    @base
    @core
    @desktop-debugging
    @dial-up
    @fonts
    @gnome-desktop
    @guest-agents
    @guest-desktop-agents
    @input-methods
    @internet-browser
    @multimedia
    @print-client
    @x11
    
    %end
    
    

    19. tail -f [文件名称]

    实时刷新查看文件,例如日志。

    20. cp [参数] 源文件 目标文件

    cp命令用于复制文件或目录。

    大家对文件复制操作应该不陌生,在Linux系统中,复制操作具体分为3种情况:
    如果目标文件是目录,则会把源文件复制到该目录中;
    如果目标文件也是普通文件,则会询问是否要覆盖它;
    如果目标文件不存在,则执行正常的复制操作。

    表1-6:cp命令的参数及其作用

    参数 作用
    -p 保留原始文件的属性
    -d 若对象为“链接文件”,则保留该“链接文件”的属性
    -r 递归持续复制(用于目录)
    -i 若目标文件存在则询问是否覆盖
    -a 相当于-pdr(p、d、r为上述参数)
    [root@wang的胜宴 ~]# cp -a Desktop/example.sh /etc
    

    上面的命令是将桌面上的example.sh复制到etc目录下。

    21. tar czvf 文件对象 要压缩的目录

    tar命令用于压缩和解压文件或目录。
    表1-7:tar命令的参数及其作用

    参数 作用
    -c 创建压缩文件
    -x 解开压缩文件
    -t 查看压缩包内有哪些文件
    -z 用Gzip压缩或解压
    -j 用bzip2压缩或解压
    -v 显示压缩或解压的过程
    -f 目标文件名
    -p 保留原始的权限与属性
    -P 使用绝对路径来压缩
    -C 指定解压到的目录
    [root@wang的胜宴 ~]# tar czvf example.tar.gz /etc
    tar: Removing leading `/' from member names
    /etc/
    /etc/fstab
    /etc/crypttab
    /etc/mtab
    ----------- 省略部分信息 ------------
    /etc/httpd/modules
    /etc/httpd/run
    /etc/example.sh
    

    22. tar xzvf 要解压的目录 -C 指定解压到的目录

    oot@wang的胜宴 ~]# tar xzvf example.tar.gz -C ~
    etc/
    etc/fstab
    tar: etc/fstab: time stamp 2019-02-12 15:22:40 is 23004119.465139425 s in the future
    ----------- 省略部分信息 ------------
    tar: etc/X11/xinit: time stamp 2019-02-12 19:49:10 is 23020107.551471209 s in the future
    tar: etc/X11/fontpath.d: time stamp 2019-02-12 19:50:35 is 23020192.551458642 s in the future
    tar: etc/X11: time stamp 2019-02-12 19:48:19 is 23020056.551446702 s in the future
    tar: etc/fonts/conf.d: time stamp 2019-02-12 19:50:48 is 23020205.551434998 s in the future
    tar: etc/fonts: time stamp 2019-02-12 19:47:00 is 23019977.551409038 s in the future
    

    23. grep [参数] 关键词 文本文件

    grep命令用于在文本中执行关键词搜索,并显示匹配的结果。
    表1-8:grep命令的参数及其作用

    参数 作用
    -b 将可执行文件(binary)当作文本文件(text)来搜索
    -c 仅显示找到的行数
    -i 忽略大小写
    -n 显示行号
    -v 反向选择——仅列出没有“关键词”的行。
    [root@wang的胜宴 ~]# grep /sbin/nologin /etc/passwd
    bin:x:1:1:bin:/bin:/sbin/nologin
    ----------- 省略部分信息 ------------
    tcpdump:x:72:72::/:/sbin/nologin
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    [root@wang的胜宴 ~]# grep -v /sbin/nologin /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    ----------- 省略部分信息 ------------
    eric:x:1005:1005::/home/eric:/bin/bash
    george:x:1006:1006::/home/george:/bin/bash
    

    24. find [查找路径] [寻找条件] [操作]

    find命令用于按照指定条件来查找文件。

    表1-9:find命令中的参数以及作用

    参数 作用
    -name 匹配名称
    -perm 匹配权限(mode为完全匹配,-mode为包含即可)
    -user 匹配所有者
    -group 匹配所有组
    -mtime -n +n 匹配修改内容的时间(-n指n天以内,+n指n天以前)
    -atime -n +n 匹配访问文件的时间(-n指n天以内,+n指n天以前)
    -ctime -n +n 匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
    -nouser 匹配无所有者的文件
    -nogroup 匹配无所有组的文件
    -newer f1 !f2 匹配比文件f1新但比f2旧的文件
    --type b/d/c/p/l/f 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
    -size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
    -prune 忽略某个目录
    -exec …… {}; 后面可跟用于进一步处理搜索结果的命令(下文会有演示)
    [root@wang的胜宴 ~]# find / -user wang的胜宴 -exec cp -a {} /root/findresults \;
    find: ‘/proc/3745/task/3745/fd/6’: No such file or directory
    find: ‘/proc/3745/task/3745/fdinfo/6’: No such file or directory
    find: ‘/proc/3745/fd/6’: No such file or directory
    find: ‘/proc/3745/fdinfo/6’: No such file or directory
    cp: cannot copy a directory, ‘/root/findresults’, into itself, ‘/root/findresults/findresults’
    cp: ‘/root/findresults/.mozilla’ and ‘/root/findresults/.mozilla’ are the same file
    ----------- 省略部分信息 ------------
    cp: ‘/root/findresults/webkit’ and ‘/root/findresults/webkit’ are the same file
    cp: ‘/root/findresults/icondatabase’ and ‘/root/findresults/icondatabase’ are the same file
    cp: ‘/root/findresults/WebpageIcons.db’ and ‘/root/findresults/WebpageIcons.db’ are the same file
    
    [root@wang的胜宴 ~]# ls /root/findresults
    2075                                                          first-index.txt                photos
    91fd3dc4769d4584b378532f02ffb133-card-database.tdb            folks                          Pictures
    ----------- 省略部分信息 ------------
    calendar                                                      input-sources-converted        
    findresults                                                   ontologies.gvdb
    

    find /* -user wang的胜宴 -exec cp -a {} /root/findresults \;:查找系统中的所有文件中所有者为wang的胜宴的文件,并将这些文件复制到/root/finresults目录下。

    25. history

    history命令用于查看用户输入命令的记录。

    [root@wang的胜宴 ~]# history
        1  ifconfig
        2  cat /etc/hostname
        3  cd /etc/sysconfig/network-scripts
        4  vim ifcfg-eno1677778
        5  rm -f ifcfg-eno1677778
    ----------- 省略部分信息 ------------
      163  grep -v /sbin/nologin /etc/passwd
      164  find / -user wang的胜宴 -exec cp -a {} /root/findresults \;
      165  cat /etc/findresualts
      166  cat /etc/findresults
      167  cat /root/findresults
      168  ls /root
      169  ls /root/findresults
      170  history
    

    26. wc [参数] 文本

    wc命令用于统计指定文本的行数、字数、字节数。
    表1-10:wc的参数以及作用

    参数 作用
    -l 只显示行数
    -w 只显示单词数
    -c 只显示字节数
    [root@wang的胜宴 Desktop]# wc example.sh
     4  8 54 example.sh
    

    27. tr [原始字符] [目标字符]

    tr命令用于替换文本中的字符。

    [root@wang的胜宴 Desktop]# cat users.txt
    andy
    barry
    carl
    duke
    eric
    george
    [root@wang的胜宴 Desktop]#  cat users.txt | tr 'a-z' 'A-Z'
    ANDY
    BARRY
    CARL
    DUKE
    ERIC
    GEORGE
    

    ' | ':管道符,用于把该符前面命令的输出内容作为后面命令的输入。
    'a-z' 'A-Z':通配符(和正则表达式差不多),表示a到z的26个小写字母,后者同理。

    28. stat [文件名称]

    stat命令用于查看文件的具体存储信息和时间等信息。

    [root@wang的胜宴 Desktop]# stat example.sh
      File: ‘example.sh’
      Size: 54          Blocks: 8          IO Block: 4096   regular file
    Device: fd01h/64769d    Inode: 71432578    Links: 1
    Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2019-02-14 20:52:41.398015290 +0800
    Modify: 2019-02-13 16:50:34.579017291 +0800
    Change: 2019-02-13 16:56:30.486020308 +0800
     Birth: -
    

    29. touch [选项] [文件]

    touch命令用于创建空目录或设置文件的时间。
    表1-11:touch命令的参数及其作用

    参数 作用
    -a 仅修改“读取时间”(atime)
    -m 仅修改“修改时间”(mtime)
    -d 同时修改atime与mtime
    [root@wang的胜宴 Desktop]# touch wang的胜宴
    [root@wang的胜宴 Desktop]# cd wang的胜宴
    [root@wang的胜宴 wang的胜宴]# cd -
    /root/Desktop
    [root@wang的胜宴 Desktop]# ls -l example.sh
    -rwxr--r--. 1 root root 54 Feb 13 16:50 example.sh
    [root@wang的胜宴 Desktop]# touch -d "20190210 05:20" example.sh
    [root@wang的胜宴 Desktop]# ls -l example.sh
    -rwxr--r--. 1 root root 54 Feb 10 05:20 example.sh
    

    30. dd

    dd命令用于按照指定大小和个数的数据块来复制文件或转换文件。

    dd命令是一个比较重要而且比较有特色的一个命令,它能够让用户按照指定大小和个数的数据块来复制文件的内容。
    Linux系统中有一个名为/dev/zero的设备文件,充满着哲学理论色彩,因为这个文件不会占用系统存储空间,但却可以提供无穷无尽的数据,因此可以使用它作为dd命令的输入文件,来生成一个指定大小的文件。

    表2-13:dd命令的参数及其作用如表2-13所示。

    参数 作用
    if 输入的文件名称
    of 输出的文件名称
    bs 设置每个“块”的大小
    count 设置要复制“块”的个数
    [root@wang的胜宴 Desktop]# dd if=/dev/zero of=800M_file count=1 bs=800M
    1+0 records in
    1+0 records out
    838860800 bytes (839 MB) copied, 34.7179 s, 24.2 MB/s
    

    31. diff [参数] 文件1 文件2 ...

    diff命令用于比较多个文本文件的差异。
    --brief:显示比较后的结果,判断文件是否相同,相同则无显示。

    [root@wang的胜宴 Desktop]# diff --brief example.sh example1.sh
    Files example.sh and example1.sh differ
    [root@wang的胜宴 Desktop]# diff -c example.sh example1.sh
    *** example.sh  2019-02-10 05:20:00.000000000 +0800
    --- example1.sh 2019-02-13 17:06:50.050025560 +0800
    ***************
    *** 1,4 ****
      #!/bin/bash
    ! #For Example BY wang的胜宴.com
    ! pwd
    ! ls -al
    --- 1,5 ----
      #!/bin/bash
    ! echo "当前脚本名称为$0"
    ! echo "总共有$#个参数,分别是$*"
    ! echo "第1个参数为$1,第五个为$5"
    ! 
    

    32. cut

    cut命令用于按“列”提取文本字符。
    -d:用于指定每列的间隔符。
    -f:用于指定输出第几列的内容。

    [root@wang的胜宴 Desktop]# cut -d: -f1 /etc/passwd
    root
    wang的胜宴
    ----------- 省略部分信息 ------------
    duke
    eric
    george
    

    33. mv [选项] 源文件 [目标路径|目标文件名]

    mv命令用于剪切文件或将文件重命名。

    剪切操作不同于复制操作,因为它会默认把源文件删除掉,只保留剪切后的文件。如果在同一个目录中对一个文件进行剪切操作,其实也就是对其进行重命名。

    [root@wang的胜宴 Desktop]# ls
    800M_file  a  chkhost.sh  chkscore.sh  example1.sh  example.sh  genuser.sh  mkcdrom.sh  users.txt  wang的胜宴
    [root@wang的胜宴 Desktop]# mv /etc/passwd ~/Desktop
    [root@wang的胜宴 Desktop]# ls
    800M_file  a  chkhost.sh  chkscore.sh  example1.sh  example.sh  genuser.sh  mkcdrom.sh  passwd  users.txt  wang的胜宴
    

    34. rm

    rm命令用于删除文件或目录。
    -f:删除免提示,直接删除。
    -r:删除目录,需带此参数。

    [root@wang的胜宴 Desktop]# rm -fr wang的胜宴
    [root@wang的胜宴 Desktop]# ls
    800M_file  a  chkhost.sh  chkscore.sh  example1.sh  example.sh  genuser.sh  mkcdrom.sh  users.txt
    

    35. file

    file命令用于查看文件所属的具体类型。

    [root@wang的胜宴 Desktop]# file example.sh
    example.sh: Bourne-Again shell script, ASCII text executable
    

    36. type

    type命令用于查看命令所属的具体类型。

    [root@wang的胜宴 Desktop]# type rm
    rm is aliased to `rm -i'
    

    这里显示rm命令实际为rm -i的别名,可用alias rm查看。

    Wang的胜宴

    相关文章

      网友评论

        本文标题:好玩实用的Linux初学者命令40个,Get!

        本文链接:https://www.haomeiwen.com/subject/hvdpeqtx.html