Linux基础(2)-基础命令和bash的基础特性(1)

Bash命令与文件管理
本文介绍了Bash命令行的基本操作技巧,包括命令历史管理、快捷操作、目录操作、文件查看与编辑等实用命令。此外,还讲解了如何利用Bash进行文件的时间戳管理以及一些Bash的基础特性。

基础命令

命令历史

  • 命令历史的管理

    登陆 shell 时,会读取命令历史文件中记录下的命令: ~/.bash_history 。

    登陆进 shell 后,新执行的命令只会记录在缓存中,这些命令会在用户退出时追加保存到命令历史文件中。

  • history使用
    [root@zze ~]# history
        1  cd ~
        2  ll
        3  history
    history:查看所有历史。
    [root@zze ~]# history 2
        3  history
        4  history 2
    history #:查看最近 # 条历史。
    [root@zze ~]# history -d 4
    [root@zze ~]# history
        1  cd ~
        2  ll
        3  history
        4  history -d 4
        5  history
    history -d #:删除历史中指定的第 # 条命令。
    [root@zze ~]# history -c
    [root@zze ~]# history
        1  history
    history -c:清空历史。
  • 快捷操作
    [root@zze ~]# history
        1  history
        2  ll
        3  history
    [root@zze ~]# !2
    ll
    total 20
    -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
    !#:调用历史中第 # 条命令。
    [root@zze ~]# !l
    ll
    total 20
    -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
    !string:调用历史中最近一个以 string 开头的命令。
    [root@zze ~]# !!
    ll
    total 20
    -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
    !!:上一条命令。
    [root@zze ~]# ls -l /etc/profile
    -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile
    [root@zze ~]# cat !$
    !$:调用上一条命令的最后一个参数。
    使用 ESC,. 可直接显示上一条命令参数。
  • 控制命令的记录方式

    命令的记录方式通过环境变量 HISTCONTROL 控制,可选三个值:

    ignoredups :忽略重复(连续且相同的命令)。

    ignorespace :忽略以空白字符开头的命令。

    ignoreboth :忽略以上两项。

    修改环境变量值的方式: export 变量名="值" ,默认只对当前会话生效。
    [root@zze ~]#  ls -l /etc/profile
    -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile
    [root@zze ~]# history
        1  history
    修改环境变量 HISTCONTROL
  • 相关环境变量

    HISTSIZE :命令历史记录的条数。

    HISTFILE :命令历史文件路径, ~/.bash_history 。

    HISTFILESIZE :命令历史文件记录历史的条数。

目录操作

  • 切换目录-cd
    [root@zze etc]# cd /etc/
    [root@zze etc]# pwd
    /etc
    cd [path]:切换到指定目录
    [root@zze etc]# cd ..
    [root@zze /]# 
    cd ..:切换到上级目录
  • 显示当前所在路径-pwd
    [root@zze ~]# pwd
    /root
    pwd:显示当前所在路径
  • 显示文件-ls
    [root@zze ~]# ls -a
    .  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .cshrc  install.log  install.log.syslog  .mysql_history  .mysql_secret  .oracle_jre_usage  .pip  .rediscli_history  .tcshrc
    ls -a [path]:显示所有文件,包含隐藏文件,包括 "." 和 ".." 。
    [root@zze ~]# ls -A
    anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .cshrc  install.log  install.log.syslog  .mysql_history  .mysql_secret  .oracle_jre_usage  .pip  .rediscli_history  .tcshrc
    ls -A [path]:显示所有文件,包含隐藏文件,不包括 "." 和 ".." 。
    [root@zze ~]# ls -l
    total 20
    -rw-------. 1 root root 1197 Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9458 Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3161 Jan 14 04:51 install.log.syslog
    ls -l [path]:长格式显示文件,不包含隐藏文件。
    [root@zze ~]# ls -lh
    total 20K
    -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
    [root@zze ~]# ll -h
    total 20K
    -rw-------. 1 root root 1.2K Jan 14 04:52 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9.3K Jan 14 04:52 install.log
    -rw-r--r--. 1 root root 3.1K Jan 14 04:51 install.log.syslog
    ls -h [path]:单位换算,通常和 "-l" 一起使用。
    [root@zze local]# ls -ld /etc/
    drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/
    [root@zze local]# ll -d /etc/
    drwxr-xr-x. 55 root root 4096 Jan 14 06:35 /etc/
    [root@zze local]# ll -d
    drwxr-xr-x. 15 root root 4096 Jan 14 05:46 .
    ls -d [path]:显示目录自身相关属性,通常和 "-l" 一起使用。
    [root@zze local]# ls
    bin  etc  games  include  java  lib  lib64  libexec  redis  sbin  share  src  tomcat
    [root@zze local]# ls -r
    tomcat  src  share  sbin  redis  libexec  lib64  lib  java  include  games  etc  bin
    ls -r [path]:反序显示。
    [root@zze ~]# ls -R /root/
    /root/:
    anaconda-ks.cfg  install.log  install.log.syslog
    ls -R [path]:递归显示文件夹及子文件夹所有文件。
  • 创建目录-mkdir
    [root@zze ~]# mkdir testdir
    [root@zze ~]# ls
    anaconda-ks.cfg  install.log  install.log.syslog  testdir
    mkdir <path>:创建目录。
    [root@zze testdir]# mkdir a/b
    mkdir: cannot create directory `a/b': No such file or directory
    [root@zze testdir]# mkdir -p a/b
    [root@zze testdir]# ls -R 
    .:
    a
    
    ./a:
    b
    mkdir -p <path>:递归创建目录(不存在则创建)。
    [root@zze testdir]# ll
    total 0
    [root@zze testdir]# mkdir -pv a/b
    mkdir: created directory `a'
    mkdir: created directory `a/b'
    mkdir -v <path>:显示详细信息。
    [root@zze testdir]# mkdir -m 664 a
    [root@zze testdir]# ll
    total 4
    drw-rw-r--. 2 root root 4096 Jan 15 11:51 a
    mkdir -m <path>:创建目录时直接指定权限。
    [root@zze testdir]# mkdir a/ a/b a/b/c
    [root@zze testdir]# tree a/
    a/
    └── b
        └── c
    mkdir <path...>:同时创建多个目录
  • 删除目录-rmdir
    [root@zze testdir]# ls -R a/
    a/:
    b
    
    a/b:
    [root@zze testdir]# rmdir a/
    rmdir: failed to remove `a/': Directory not empty
    [root@zze testdir]# rmdir a/b/
    [root@zze testdir]# rmdir a/
    [root@zze testdir]# ll
    total 0
    rmdir <path>:删除空目录。
    [root@zze testdir]# rmdir -p a/b/c/
    [root@zze testdir]# ll
    total 0
    rmdir -p <path>:递归删除所有空目录。
  • 树状显示目录结构-tree
    [root@zze testdir]# tree a/
    a/
    └── b
        ├── c
        └── test.txt
    
    2 directories, 1 file
    tree <path>:树状显示目录及目录下的文件。
    [root@zze testdir]# tree -d a/
    a/
    └── b
        └── c
    
    2 directories
    tree -d <path>:树状显示目录结构,不包含文件。
    [root@zze testdir]# tree -L 1 a/
    a/
    └── b
    tree -L # <path>:树状显示仅 # 深度结构。

查看文本文件

  • 正序输出文件内容-cat
    [root@zze testdir]# cat test.txt 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat <filename>:正序查看指定文件。
  • 反序输出文件内容-tac
    [root@zze testdir]# tac test.txt 
    9
    8
    7
    6
    5
    4
    3
    2
    1
    tac <filename>:反序查看指定文件。
  • 按页查看-more
    [root@zze ~]# more /etc/profile
    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
    
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }
    
    
    if [ -x /usr/bin/id ]; then
        if [ -z "$EUID" ]; then
            # ksh workaround
            EUID=`id -u`
            UID=`id -ru`
        fi
        USER="`id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
    fi
    
    # Path manipulation
    if [ "$EUID" = "0" ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
    else
        pathmunge /usr/local/sbin after
        pathmunge /usr/sbin after
        pathmunge /sbin after
    fi
    
    HOSTNAME=`/bin/hostname 2>/dev/null`
    HISTSIZE=1000
    if [ "$HISTCONTROL" = "ignorespace" ] ; then
        export HISTCONTROL=ignoreboth
    else
        export HISTCONTROL=ignoredups
    fi
    
    export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
    --More--(63%)
    more <filename>:按页查看指定文件。
    [root@zze ~]# more -d /etc/rc.d/rc.sysinit 
    #!/bin/bash
    #
    # /etc/rc.d/rc.sysinit - run once at boot time
    #
    # Taken in part from Miquel van Smoorenburg's bcheckrc.
    #
    
    HOSTNAME=$(/bin/hostname)
    
    set -m
    
    if [ -f /etc/sysconfig/network ]; then
        . /etc/sysconfig/network
    fi
    if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
        HOSTNAME=localhost
    fi
    
    if [ ! -e /proc/mounts ]; then
        mount -n -t proc /proc /proc
        mount -n -t sysfs /sys /sys >/dev/null 2>&1
    fi
    if [ ! -d /proc/bus/usb ]; then
        modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
    else
        mount -n -t usbfs /proc/bus/usb /proc/bus/usb
    fi
    
    #remount /dev/shm to set attributes from fstab #669700
    mount -n -o remount /dev/shm >/dev/null 2>&1
    #remount /proc to set attributes from fstab #984003
    mount -n -o remount /proc >/dev/null 2>&1
    
    . /etc/init.d/functions
    
    PLYMOUTH=
    [ -x /bin/plymouth ] && PLYMOUTH=yes
    
    # Check SELinux status
    SELINUX_STATE=
    if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then
        if [ -r "/selinux/enforce" ] ; then
            SELINUX_STATE=$(cat "/selinux/enforce")
        else
            # assume enforcing if you can't read it
            SELINUX_STATE=1
        fi
    fi
    
    if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then
        /sbin/restorecon -R -F /dev 2>/dev/null
    fi
    
    disable_selinux() {
        echo $"*** Warning -- SELinux is active"
    more -d <filename>:带提示信息查看,显示翻页及退出信息。
    [root@zze temp]# more +5 test.txt 
    55555555555555555
    666666666666666666666
    77777777777777777777
    88888888888888888888
    more +# <filename>:从第 # 行开始查看。
    操作方式:
    B :向前翻页,SPACE :向后翻页,RETURN :下一行,Q:退出。
    比例显示:
    按字符统计。
    一旦翻完不可回转。
  • 按页查看-less

    按页查看,操作方式和 man 一致,因为 man 内部实际上也是使用 less 打开文本文件。

    操作:
    Space,^V,^F:向文件尾部翻屏。
    b,^B:向文件首部翻屏。
    d,^D:向文件尾部翻半屏。
    u,^U:向文件首部翻半屏。
    Return,^N,e,^E,j,^J:向文件尾部翻一行。
    y,^Y,^P,k,^K:向文件首部翻一行。
    q:退出。
    #:跳转到第 # 行。
    1G:回到文件首部。
    G:跳至文件尾部。
    /KEYWORD:以 KEYWORD 指定的字符串为关键字,从当前位置向文件尾部搜索,不区分大小写。n:下一个,N:上一个。
    ?KEYWORD:以 KEYWORD 指定的字符串为关键字,从当前位置向文件首部搜索,不区分大小写。n:下一个,N:上一个。
    翻完可回转。
  • 查看文件头部-head
    [root@zze temp]# head test.txt 
    11111111111111111111111111111111111
    22222222222222222222222222222222222
    333333333333333
    4444444444444444444
    55555555555555555
    666666666666666666666
    77777777777777777777
    88888888888888888888
    9999999999
    10
    head <filename>:默认输出前 10 行。
    [root@zze temp]# head -c 10 test.txt 
    1111111111
    head -c # <filename>:指定输出前 # 字节。
    [root@zze temp]# head -n 5 test.txt 
    11111111111111111111111111111111111
    22222222222222222222222222222222222
    333333333333333
    4444444444444444444
    55555555555555555
    head [-n #|-#] <filename>:指定输出前 # 行。
  •  查看文件尾部-tail
    [root@zze temp]# tail test.txt 
    11
    1222222222222222
    1333333333333333
    1444444444444
    15
    16
    177777777
    18
    19
    2000
    tail <filename>:默认输出后 10 行。
    [root@zze temp]# tail -c 10 test.txt 
    8
    19
    2000
    tail -c # <filename>:指定输出后 # 字节。
    [root@zze temp]# tail -n 5 test.txt 
    16
    177777777
    18
    19
    2000
    tail [-n #|-#] <filename>:指定输出后 # 行。
    [root@zze temp]# tail -f test.txt 
    11
    1222222222222222
    1333333333333333
    1444444444444
    15
    16
    177777777
    18
    19
    2000
    new line 
    tail -f <filename>:跟踪显示文件新追加的内容。

文件时间戳管理工具

  • 数据
    文件的数据分为元数据和内容数据,元数据可以看做文件的状态描述信息,内容数据则是文件的真实内容。
  • 查看文件状态-stat
    [root@zze temp]# stat test.txt 
      File: `test.txt'
      Size: 292           Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 1966102     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-01-13 09:57:08.339394446 +0800
    Modify: 2019-01-13 09:57:08.339394446 +0800
    Change: 2019-01-13 09:57:08.339394446 +0800
    stat <filename>
    三个时间戳:

    access time (atime):访问时间,读取文件内容时。

    modify time (mtime):修改时间,修改文件内容时。

    change time (ctime):改变时间,元数据发生改变。

  • 修改时间戳-touch
    [root@zze temp]# stat test.txt 
      File: `test.txt'
      Size: 292           Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 1966102     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-01-13 09:57:08.339394446 +0800
    Modify: 2019-01-13 09:57:08.339394446 +0800
    Change: 2019-01-13 09:57:08.339394446 +0800
    [root@zze temp]# touch test.txt 
    [root@zze temp]# stat test.txt 
      File: `test.txt'
      Size: 292           Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 1966102     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-01-13 10:10:07.019393669 +0800
    Modify: 2019-01-13 10:10:07.019393669 +0800
    Change: 2019-01-13 10:10:07.019393669 +0800
    touch [-t time] <filename>:修改 atime 和 mtime 为指定时间,不指定 time 则默认修改为当前时间。
    [root@zze temp]# touch -m -t  201810101200.59 test.txt 
    [root@zze temp]# stat test.txt 
      File: `test.txt'
      Size: 292           Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 1966102     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-01-13 10:10:07.019393669 +0800
    Modify: 2018-10-10 12:00:59.000000000 +0800
    Change: 2019-01-13 10:18:00.999393544 +0800
    touch -m [-t time] <filename>:修改 mtime 为指定时间,不指定 time 则默认修改为当前时间。
    [root@zze temp]# touch -a -t  201811101200.59 test.txt 
    [root@zze temp]# stat test.txt 
      File: `test.txt'
      Size: 292           Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 1966102     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-11-10 12:00:59.000000000 +0800
    Modify: 2018-10-10 12:00:59.000000000 +0800
    Change: 2019-01-13 10:23:43.380393790 +0800
    touch -a [-t time] <filename>:修改 atime 为指定时间,不指定 time 则默认修改为当前时间。
    [root@zze temp]# ls
    test.txt
    [root@zze temp]# touch a.txt
    [root@zze temp]# ls
    a.txt  test.txt
    touch <filename>:当 filename 指定的文件不存在时则创建文件。
    [root@zze temp]# touch -c a.txt 
    [root@zze temp]# ls
    a.txt  test.txt
    [root@zze temp]# touch -c b.txt
    [root@zze temp]# ls
    a.txt  test.txt
    touch -c <filename>:当 filename 指定的文件不存在时不创建文件。
    time 的格式为 "yyyyMMddHHmmss.ss" ,例:“2019年1月1号2点35分34秒”为 "201901010235.34"。

bash的基础特性

补全-TAB键

  • 命令补全

    一次 TAB:如果用户给定的字符串只有一条唯一对应的命令,一次 TAB 则直接补全。

    两次 TAB:如果用户给定的字符串开头对应的命令不唯一,再次 TAB 则会显示所有以该字符串开头的命令。

    [root@zze ~]# cl
    clear      clock      clockdiff  cloog   
    两次 TAB
  • 路径补全

    把用户给定的字符串当做路径开头,并在其指定目录下搜索以指定字符串开头的文件名。如果唯一,则直接补全,否则再次 TAB 显示列表。

命令行展开

[root@zze testdir]# cd ~
[root@zze ~]# 
~:展开为当前用户主目录。
[root@zze testdir]# cd ~root/
[root@zze ~]# 
~[user]:展开为 user 的主目录。
/tmp/{a,b} -> /tmp/a , /tmp/b
/tmp/{a,b}/c -> /tmp/a/c , /tmp/b/c
{}:可承载一个逗号分隔的列表,并将其展开为多个路径。
练习1:如何一次性创建 'tmp/x/y1' , 'tmp/x/y2' , 'tmp/x/y1/a' , 'tmp/x/y1/b' , 'tmp/x/y2/a' , 'tmp/x/y2/b' ?
[root@zze testdir]# mkdir tmp/x/{y1,y2}/{a,b}
mkdir: cannot create directory `tmp/x/y1/a': No such file or directory
mkdir: cannot create directory `tmp/x/y1/b': No such file or directory
mkdir: cannot create directory `tmp/x/y2/a': No such file or directory
mkdir: cannot create directory `tmp/x/y2/b': No such file or directory
[root@zze testdir]# mkdir -p tmp/x/{y1,y2}/{a,b}
[root@zze testdir]# tree tmp/
tmp/
└── x
    ├── y1
    │   ├── a
    │   └── b
    └── y2
        ├── a
        └── b

7 directories, 0 files
result

练习2:如何一次性创建 'x_m' , 'y_m' , 'x_n' , 'y_n' ?

[root@zze testdir]# mkdir -p {x,y}_{m,n}
[root@zze testdir]# ls
x_m  x_n  y_m  y_n
result

练习3:如何一次性创建 'tmp/bin' , 'tmp/sbin' , 'tmp/usr/bin' , 'tmp/usr/sbin' ?

[root@zze testdir]# mkdir -p tmp/{bin,sbin,usr/{bin,sbin}}
[root@zze testdir]# tree tmp
tmp
├── bin
├── sbin
└── usr
    ├── bin
    └── sbin

5 directories, 0 files
result

命令的执行状态结果

bash 使用特殊变量 $? 保存最近一条命令的执行状态结果:

[root@zze testdir]# mkdir tmp
[root@zze testdir]# echo $?
0
[root@zze testdir]# mkdir tmp
mkdir: cannot create directory `tmp': File exists
[root@zze testdir]# echo $?
1
0:成功,1-255:失败
程序执行有两类结果:执行的返回值结果和执行的状态结果。

转载于:https://www.cnblogs.com/zze46/p/10271641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值