shell常用的命令,Linux基礎(2)-基礎命令和bash的基礎特性(1)

 2023-11-07 阅读 24 评论 0

摘要:基礎命令 命令歷史 命令歷史的管理 登陸 shell 時,會讀取命令歷史文件中記錄下的命令: ~/.bash_history 。 登陸進 shell 后,新執行的命令只會記錄在緩存中,這些命令會在用戶退出時追加保存到命令歷史文件中。 history使用 [root@zze ~]# his

基礎命令

命令歷史

  • 命令歷史的管理

    登陸 shell 時,會讀取命令歷史文件中記錄下的命令: ~/.bash_history 。

    登陸進 shell 后,新執行的命令只會記錄在緩存中,這些命令會在用戶退出時追加保存到命令歷史文件中。

  • history使用

    [root@zze ~]# history1  cd ~2  ll3  history
    history:查看所有歷史。
    [root@zze ~]# history 23  history4  history 2
    history #:查看最近 # 條歷史。
    [root@zze ~]# history -d 4
    [root@zze ~]# history1  cd ~2  ll3  history4  history -d 45  history
    history -d #:刪除歷史中指定的第 # 條命令。
    [root@zze ~]# history -c
    [root@zze ~]# history1  history
    history -c:清空歷史。
  • 快捷操作

    [root@zze ~]# history1  history2  ll3  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?:忽略重復(連續且相同的命令)。

    shell常用的命令。ignorespace?:忽略以空白字符開頭的命令。

    ignoreboth?:忽略以上兩項。

    修改環境變量值的方式:?export 變量名="值"?,默認只對當前會話生效。
    [root@zze ~]#  ls -l /etc/profile
    -rw-r--r--. 1 root root 2008 Jan 14 05:28 /etc/profile
    [root@zze ~]# history1  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/:
    ba/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.txt2 directories, 1 file
    tree <path>:樹狀顯示目錄及目錄下的文件。
    [root@zze testdir]# tree -d a/
    a/
    └── b└── c2 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" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac
    }if [ -x /usr/bin/id ]; thenif [ -z "$EUID" ]; then# ksh workaroundEUID=`id -u`UID=`id -ru`fiUSER="`id -un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER"
    fi# Path manipulation
    if [ "$EUID" = "0" ]; thenpathmunge /sbinpathmunge /usr/sbinpathmunge /usr/local/sbin
    elsepathmunge /usr/local/sbin afterpathmunge /usr/sbin afterpathmunge /sbin after
    fiHOSTNAME=`/bin/hostname 2>/dev/null`
    HISTSIZE=1000
    if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth
    elseexport HISTCONTROL=ignoredups
    fiexport 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 -mif [ -f /etc/sysconfig/network ]; then. /etc/sysconfig/network
    fi
    if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; thenHOSTNAME=localhost
    fiif [ ! -e /proc/mounts ]; thenmount -n -t proc /proc /procmount -n -t sysfs /sys /sys >/dev/null 2>&1
    fi
    if [ ! -d /proc/bus/usb ]; thenmodprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
    elsemount -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/functionsPLYMOUTH=
    [ -x /bin/plymouth ] && PLYMOUTH=yes# Check SELinux status
    SELINUX_STATE=
    if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; thenif [ -r "/selinux/enforce" ] ; thenSELINUX_STATE=$(cat "/selinux/enforce")else# assume enforcing if you can't read itSELINUX_STATE=1fi
    fiif [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then/sbin/restorecon -R -F /dev 2>/dev/null
    fidisable_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

    shell和bash、按頁查看,操作方式和 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 則直接補全。

    shell基礎?兩次 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└── b7 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└── sbin5 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:失敗
程序執行有兩類結果:執行的返回值結果和執行的狀態結果。

一個bash shell腳本的第一行是。轉載于:https://www.cnblogs.com/zze46/p/10271641.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/166902.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息