• Linux命令实践(一)


    1.pwd(printing working directory)打印当前工作目录路径

    [root@test sysconfig]# pwd
    /etc/sysconfig
    

    2.ls(list)列出当前目录下的文件

      语法:ls [OPTION]... [FILE]... 

      选项:-l 长格式显示当前目录下的文件

    [root@test ~]# ls -l
    total 48
    -rw-r--r-- 1 root root     0 Sep 10 09:42  
    -rw-r--r-- 1 root root 20163 Sep 20 18:36 a.txt
    -rw-r--r-- 1 root root   177 Sep 12 10:55 mycron
    drwxr-xr-x 3 root root  4096 Sep 14 10:52 scripts
    drwxr-xr-x 2 root root  4096 Sep 11 15:33 test
    -rw-r--r-- 1 root root   177 Oct 12 08:52 xx.xx
    -rw-r--r-- 1 root root  9607 Sep 11 15:32 xxx.tar.gz    

        文件类型:

          -:普通文件(file)

          d:目录文件(directory)

          b:块设备文件(block)

          c:字符设备文件(character)

          l:符号链接文件(symbolic link file)

          p:命令管道文件(pipe)

          s:套接字文件(socket)

    文件权限:9位,每3位一组,每一组:rwx(读写执行),如果没有相对应的权限用'-'占位
    文件硬链接的次数
    文件的属主(owner)
    文件的属组(group)
    文件的大小(size),默认单位是字节
    时间戳(timestamp):显示是最近一次修改的时间

    访问:access
    修改:modify 这个是文件的内容发生改变
    改变:change,metadata,元数据 或者说改变属性

    当然除此以上权限,还有其他权限,上面是普通权限位的一个简要介绍,我们先了解,后面我们会细说Linux文件普通权限和特殊权限。

    -h:以人类可以读的格式显示文件的大小

    [root@test ~]# ls -lh
    total 48K
    -rw-r--r-- 1 root root    0 Sep 10 09:42  
    -rw-r--r-- 1 root root  20K Sep 20 18:36 a.txt
    -rw-r--r-- 1 root root  177 Sep 12 10:55 mycron
    drwxr-xr-x 3 root root 4.0K Sep 14 10:52 scripts
    drwxr-xr-x 2 root root 4.0K Sep 11 15:33 test
    -rw-r--r-- 1 root root  177 Oct 12 08:52 xx.xx
    -rw-r--r-- 1 root root 9.4K Sep 11 15:32 xxx.tar.gz
    

      说明:-h要和-l一起用才看得到效果。这里说一下Linux里的命令选项,Linux里的命令选项分长选项和短选项,长选项就是以--开头的 通常情况是--一个单词之类的,选项和选项之间不能合并使用,必须有空格给予区分。短选项是以-开头的 通常后面接的是一个字符或单词 ,短选项之间可以合并使用 比如ls -l -h 就可以写成ls -lh。

        -a:显示以.开头的隐藏文件(要显示.或..)

    [root@test ~]# ls -a
        .bash_history  .bashrc   .mysql_history  .viminfo  scripts  xxx.tar.gz
    .   .bash_logout   .cshrc    .ssh            a.txt     test
    ..  .bash_profile  .lesshst  .tcshrc         mycron    xx.xx
    

        -A:显示已.开头的隐藏文件(不显示.或..)

    [root@test ~]# ls -A
                   .bash_logout   .bashrc  .lesshst        .ssh     .viminfo  mycron   test   xxx.tar.gz
    .bash_history  .bash_profile  .cshrc   .mysql_history  .tcshrc  a.txt     scripts  xx.xx
    

      说明:.表示当前目录,..表示当前目录的父目录。

        -d:显示文件自身属性

    [root@test ~]# ls -ld
    dr-xr-x---. 5 root root 4096 Oct 13 20:00 .
    

        -i:显示文件的索引节点号(index node 简称inode)

    [root@test ~]# ls -i
    135914    135474 a.txt  135927 mycron  131075 scripts  135455 test  140544 xx.xx  135923 xxx.tar.gz
    

        -r:逆序显示文件(reversed)

    [root@test ~]# ls
       a.txt  mycron  scripts  test  xx.xx  xxx.tar.gz
    [root@test ~]# ls -r
    xxx.tar.gz  xx.xx  test  scripts  mycron  a.txt   
    

        -R:递归(recursive)显示文件

    [root@test work]# ls -l
    total 40
    -rw-r--r-- 1 root root   56 Jul 13 07:49 list.txt
    drwxr-xr-x 2 root root 4096 Sep  8 20:20 mysql_log
    -rw------- 1 root root    5 Jun 25 11:30 rsync.password
    drwxr-xr-x 2 root root 4096 Aug 31 17:40 scripts
    drwxr-xr-x 2 root root 4096 Aug 31 17:28 test
    -rw-r--r-- 1 root root  221 Jul 13 07:35 test.tar.gz
    -rw-r--r-- 1 root root   79 Sep  8 16:46 test.txt
    -rw-r--r-- 1 root root 3259 Sep 13 15:08 tomcat_star.sh
    -rw-r--r-- 1 root root 2926 Sep 13 15:08 tomcat_star01.sh
    -rw-r--r-- 1 root root  177 Jul 13 07:52 xxxx.tar.gz
    [root@test work]# ls -R
    .:
    list.txt   rsync.password  test         test.txt        tomcat_star01.sh
    mysql_log  scripts         test.tar.gz  tomcat_star.sh  xxxx.tar.gz
    
    ./mysql_log:
    mysql.log
    
    ./scripts:
    auto_bak_log.sh     batch_create_user.sh  clear             rsync_server_config.sh
    auto_delete_log.sh  batch_delete_user.sh  nginx_install.sh
    
    ./test:
    

    3.cd:change directory 切换目录

    cd命令用来切换工作目录至dirname 其中dirName表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的home directory(也就是刚登录时所在的目录)。另外,~也表示为home directory的意思,.则是表示目前所在的目录,..则表示目前目录位置的上一层目录。

    cd 不带任何选项和参数 就回到了家目录

    [root@test work]# pwd
    /work
    [root@test work]# cd
    [root@test ~]# pwd
    /root
    

    cd ~username  切换到指定用户的家目录

    [root@test ~]# cd ~qiuhom
    [root@test qiuhom]# pwd
    /home/qiuhom
    

      提示:切换到指定用户家目录的前提是root用户哟

    cd - 切换到上一次所在目录(在当前目录和前一次所在目录之间来回切换)

    [root@test qiuhom]# cd /etc/init.d
    [root@test init.d]# pwd
    /etc/init.d
    [root@test init.d]# cd /var/log/
    [root@test log]# pwd
    /var/log
    [root@test log]# cd -
    /etc/init.d
    [root@test init.d]# cd -
    /var/log
    [root@test log]#  
    cd ~  进入用户主目录
    [root@test log]# pwd
    /var/log
    [root@test log]# cd ~
    [root@test ~]# pwd
    /root 
    d ..  返回上级目录
    [root@test etc]# cd /etc/init.d/
    [root@test init.d]# pwd
    /etc/init.d
    [root@test init.d]# cd ..
    [root@test etc]# pwd
    /etc
    

    4.type显示指定命令属于那种命令类型

    首先说下Linux命令类型分为内置命令和外部命令两种,内置命令指的是shell内置的命令,用type去查看会返回shell builtin的字符串,说明我们查询的命令是内置shell命令,外部命令用type查看一般在文件系统的某个路径下有一个与命令名称相应的可执行文件。

    [root@test etc]# type cd
    cd is a shell builtin
    [root@test etc]# type type
    type is a shell builtin
    [root@test etc]# type tree
    tree is /usr/bin/tree
    [root@test etc]# type ifconfig
    ifconfig is /sbin/ifconfig
    

    5.hash:查看缓存命令和使用的次数显示了之前命令的操作记录,hits表示我们使用的次数

    [root@test etc]# hash
    hits    command
      11    /usr/bin/cal
       2    /bin/date
       7    /usr/bin/man
       2    /sbin/clock
      28    /bin/ls

      说明:linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样。第一次执行命令shell解释器默认的会从PATH路径下寻找该命令的路径,当你第二次使用该命令时,shell解释器首先会查看hash表有没有缓存该命令,如果没有该命令才会去PATH路径下寻找。

    -d:清空指定命令的缓存记录(后面要指定命令)

    [root@test etc]# hash
    hits    command
      11    /usr/bin/cal
       2    /bin/date
       7    /usr/bin/man
       2    /sbin/clock
      28    /bin/ls
    [root@test etc]# hash -d cal
    [root@test etc]# hash
    hits    command
       2    /bin/date
       7    /usr/bin/man
       2    /sbin/clock
      28    /bin/ls
    

    -p:把指定路径 对应的命令加入到命令缓存表里

    [root@test etc]# hash -p /usr/bin/cal cal
    [root@test etc]# hash
    hits    command
       0    /usr/bin/cal
       2    /bin/date
       7    /usr/bin/man
       2    /sbin/clock
      28    /bin/ls
    

    -l:查看hash表命令的路径和对应的命令

    [root@test etc]# hash -l
    builtin hash -p /usr/bin/cal cal
    builtin hash -p /bin/date date
    builtin hash -p /usr/bin/man man
    builtin hash -p /sbin/clock clock
    builtin hash -p /bin/ls ls
    

    -r:清空命令缓存表(清空全部)  

    [root@test etc]# hash 
    hits    command
       0    /usr/bin/cal
       2    /bin/date
       7    /usr/bin/man
       2    /sbin/clock
      28    /bin/ls
    [root@test etc]# hash -r
    [root@test etc]# hash
    hash: hash table empty
    

    6.date打印或设置系统日期和时间(print or set the system date and time)

    语法:date(选项)(参数)
    选项:
    -d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号;
    -s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号;
    参数:<+时间日期格式>:指定显示时使用的日期时间格式。
    日期格式字符串表
    %H 小时,24小时制(00~23)
    %I 小时,12小时制(01~12)
    %k 小时,24小时制(0~23)
    %l 小时,12小时制(1~12)
    %M 分钟(00~59)
    %p 显示出AM或PM
    %r 显示时间,12小时制(hh:mm:ss %p)
    %s 从1970年1月1日00:00:00到目前经历的秒数
    %S 显示秒(00~59)
    %T 显示时间,24小时制(hh:mm:ss)
    %X 显示时间的格式(%H:%M:%S)
    %Z 显示时区,日期域(CST)
    %a 星期的简称(Sun~Sat)
    %A 星期的全称(Sunday~Saturday)
    %h,%b 月的简称(Jan~Dec)
    %B 月的全称(January~December)
    %c 日期和时间(Tue Nov 20 14:12:58 2012)
    %d 一个月的第几天(01~31)
    %x,%D 日期(mm/dd/yy)
    %j 一年的第几天(001~366)
    %m 月份(01~12)
    %w 一个星期的第几天(0代表星期天)
    %W 一年的第几个星期(00~53,星期一为第一天)
    %y 年的最后两个数字(1999则是99)
    
    -d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号
    显示当前年月日:
    [root@test etc]# date +%Y%m%d
    20181015
    

    显示昨天的日期

    [root@test etc]# date -d "-1 day" +%Y%m%d  
    20181014  

     显示明天的日期

    [root@test etc]# date -d "+1 day" +%Y%m%d
    20181016
    

    显示上一月今天的日期

    [root@test etc]# date -d "-1 month" +%Y%m%d
    20180915

    显示下个月今天的日期

    [root@test etc]# date -d "+1 month" +%Y%m%d 
    20181115

    显示昨年今天的日期

    [root@test etc]# date -d "-1 year" +%Y%m%d
    20171015

    显示明年今天的日期

    [root@test etc]# date -d "+1 year" +%Y%m%d 
    20191015
    

    设定时间:

    设置当前时间,只有root权限才能设置,其他只能查看 

    [root@test etc]# date -s 20191025
    Fri Oct 25 00:00:00 CST 2019
    

      说明:设置成2019年10月25日 这样设置它会默认把时分秒设置成00:00:00

    [root@test etc]# date -s 12:15:30
    Fri Oct 25 12:15:30 CST 2019
    

      说明:设置具体时间,不会对日期做更改

    [root@test etc]# date -s "13:54:20 2018-10-26"
    Fri Oct 26 13:54:20 CST 2018
    [root@test etc]# date -s "15:45:25 20181112"
    Mon Nov 12 15:45:25 CST 2018
    [root@test etc]# date -s "20181113 14:20:20"
    Tue Nov 13 14:20:20 CST 2018
    

      说明:以上3中写法都可以设置完整全部的时间,年月日时分秒。

    7.cal:calendar 显示日历

    [root@test etc]# cal
        November 2018   
    Su Mo Tu We Th Fr Sa
                 1  2  3
     4  5  6  7  8  9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30
    

      说明:默认显示一个月的 当然我们可以指定年月来显示。

    显示2018一年的日历 cal 2018

    [root@test etc]# cal 2018
                                   2018                               
    
           January               February                 March       
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
        1  2  3  4  5  6                1  2  3                1  2  3
     7  8  9 10 11 12 13    4  5  6  7  8  9 10    4  5  6  7  8  9 10
    14 15 16 17 18 19 20   11 12 13 14 15 16 17   11 12 13 14 15 16 17
    21 22 23 24 25 26 27   18 19 20 21 22 23 24   18 19 20 21 22 23 24
    28 29 30 31            25 26 27 28            25 26 27 28 29 30 31
    
            April                   May                   June        
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6  7          1  2  3  4  5                   1  2
     8  9 10 11 12 13 14    6  7  8  9 10 11 12    3  4  5  6  7  8  9
    15 16 17 18 19 20 21   13 14 15 16 17 18 19   10 11 12 13 14 15 16
    22 23 24 25 26 27 28   20 21 22 23 24 25 26   17 18 19 20 21 22 23
    29 30                  27 28 29 30 31         24 25 26 27 28 29 30
    
            July                  August                September     
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
     1  2  3  4  5  6  7             1  2  3  4                      1
     8  9 10 11 12 13 14    5  6  7  8  9 10 11    2  3  4  5  6  7  8
    15 16 17 18 19 20 21   12 13 14 15 16 17 18    9 10 11 12 13 14 15
    22 23 24 25 26 27 28   19 20 21 22 23 24 25   16 17 18 19 20 21 22
    29 30 31               26 27 28 29 30 31      23 24 25 26 27 28 29
                                                  30
           October               November               December      
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
        1  2  3  4  5  6                1  2  3                      1
     7  8  9 10 11 12 13    4  5  6  7  8  9 10    2  3  4  5  6  7  8
    14 15 16 17 18 19 20   11 12 13 14 15 16 17    9 10 11 12 13 14 15
    21 22 23 24 25 26 27   18 19 20 21 22 23 24   16 17 18 19 20 21 22
    28 29 30 31            25 26 27 28 29 30      23 24 25 26 27 28 29
                                                  30 31
    

    显示指定某一年的某个月 如: cal 10 2018 表示显示2018年10月的日历

    [root@test etc]# cal 10 2018
        October 2018    
    Su Mo Tu We Th Fr Sa
        1  2  3  4  5  6
     7  8  9 10 11 12 13
    14 15 16 17 18 19 20
    21 22 23 24 25 26 27
    28 29 30 31
    

    8.hwclock查询并设置硬件时钟(query and set the hardware clock)

     linux系统时钟有两个,一个是硬件时钟,即BIOS时间,另一个是系统时钟,是linux系统Kernel时间,程序运行时读取的时间是系统时间。当Linux启动时,系统Kernel会去读取硬件时钟的设置,然后系统时钟就会独立于硬件运作。

    硬件时钟(hwclock或者clock查看)

    [root@test etc]# clock
    Tue Oct 16 00:21:11 2018  -0.625710 seconds
    [root@test etc]# hwclock
    Tue Oct 16 00:21:18 2018  -0.077961 seconds
    

    系统时钟(date查看) 

    [root@test etc]# date
    Tue Nov 13 14:48:27 CST 2018 

    通常这两个时间是不同步的,我们hwclock命令去同步两者的时间。

    -w:将系统时钟同步到硬件时钟

    [root@test etc]# clock
    Tue Oct 10 00:00:21 2017  -0.769098 seconds
    [root@test etc]# date -s "20181016 00:39:00"
    Tue Oct 16 00:39:00 CST 2018
    [root@test etc]# hwclock -w
    [root@test etc]# clock
    Tue Oct 16 00:39:12 2018  -0.205121 seconds
    

    -s:将硬件时钟同步到系统时钟  

    [root@test etc]# hwclock
    Tue Oct 16 00:40:26 2018  -0.082141 seconds
    [root@test etc]# date
    Tue Oct 10 00:00:09 CST 2017
    [root@test etc]# hwclock -s
    [root@test etc]# date
    Tue Oct 16 00:40:40 CST 2018
    

    -r:读取硬件时钟 

    [root@test etc]# clock
    Tue Oct 16 00:41:17 2018  -0.587698 seconds
    [root@test etc]# hwclock -r
    Tue Oct 16 00:41:24 2018  -0.898271 seconds
    [root@test etc]# hwclock
    Tue Oct 16 00:41:29 2018  -0.498146 seconds
    

      提示:其实不加-r选项也是可以查询的。

    9.获得命令的使用帮助

    内部命令:help command

    外部命令:command --help 获得命令的简要帮助信息

    命令手册:man command (manual)

    首先我们说下man手册里面的章节数字所代表的含义:

    1:用户命令(user command)所有用户都可以使用的非管理员命令,如(/bin,/usr/bin,/usr/local/bin)下的命令。

    2:系统调用(system calls)

    3:c 语言库调用( C Library Functions)

    4:特殊文件(如:设备文件)

    5:文件格式(解释某个命令的配置文件的语法的,如passwd)

    6:游戏相关的

    7:杂项:miscellaneous(不便归类的命令)

    8:系统管理员(超级管理员所用命令,如/sbin,/usr/sbin,/usr/local/sbin)下的命令,一般就是指root用户可以用的命令。

    man手册里特殊符号所代表的意思:

    []:表示可选的

    <>:表示必选的

    ...:表示可以重复多次

    |:表示多选一

    {}:分组,没有特殊意义

    NAME:命令名称及功能简要说明
    SYNOPSIS:用法说明,包括可以用的选项
    DESCRIPTION:命令功能的详尽说明,包括每一个选项的意义
    OPTIONS:说明每一个选项的意义
    FILES:此命令相关的配置文件
    BUGE:报告bug邮箱地址
    EXAMPLES:使用示例
    SEE ALSO:另外参照

    翻屏操作:

    向后翻一屏:space
    向前翻一屏:b
    向后翻一行:enter或者向下放向键 或者j
    向前翻一行:k 或向上方向键

    查找:

    /加要查的内容 按n向下搜索 N 向上搜索 默认从上往下搜索
    ?加要查的内容 按N向下搜索 n 向上搜索 默认从下往上搜索

    q 退出

    10.echo:显示一行文本内容,在shell中常用来打印shell变量的值或者直接输出指定的字符串

    语法:echo(选项)(参数)

    -n:不换行显示内容
    -e:对反斜杠转义的解释

    \ backslash(反斜线,插入字符)
    a alert (BEL)(响铃,发出警告声)
     backspace(退格,相当于backspace键的功能,删除前一个字符)
    c produce no further output(最后不加上换行符号)
    f form feed(换行但光标仍旧停留在原来的位置;)
    new line(换行,且光标移至行首)
    carriage return(光标移至行首,但不换行)
    horizontal tab(水平插入tab)
    v vertical tab(垂直插入tab)

    [root@test etc]# echo -en "this is test..."
    this is test[root@test etc]# echo -e "\"
    
    [root@test etc]# echo -e "this is test
    "
    this is test
    
    [root@test etc]# echo -e "this is testf"
    this is test
    
    [root@test etc]# echo -e "this is ftest"  
    this is 
            test
    [root@test etc]# echo -e "this is vtest" 
    this is 
            test
    [root@test etc]# echo -e "this is 	test" 
    this is         test
    [root@test etc]# echo -e "this is ctest"  
    this is [root@test etc] 
  • 相关阅读:
    命令[34]
    命令[33]
    命令[27]
    命令[38]
    命令[19]
    命令[22]
    命令[30]
    命令[37]
    命令[23]
    命令[26]
  • 原文地址:https://www.cnblogs.com/qiuhom-1874/p/9795687.html
Copyright © 2020-2023  润新知