天天看點

Linux基礎04 移動(剪切)mv, 删除rm, 看檔案内容cat, tac, more, less, 看前後n行head, tail, 三劍客grep

移動(剪切)mv

mv:移動 move    # 系統預設别名mv -i 為mv(-i互動,在覆寫前會進行提醒),經測試我的centos7虛拟機并沒有該别名...
    
    mv [OPTION]... 源檔案 目标
    -i:互動(在覆寫前會進行提醒)
    -t:調換目标和源檔案位置
    
    1.如果目标是一個目錄,就把檔案放入目錄
    2.如果目标是一個檔案,就會被覆寫
    
    [root@db04 ~]# mv file /tmp/
    [root@db04 ~]# mv file1 test1    # 如test1不存在, file1改名為test1。test1存在且為檔案,覆寫; 如為目錄, 放到該目錄下
    [root@db04 ~]# mv dir1 /tmp/
    [root@db04 ~]# mv dir2 file4 file5 file6 /opt/ /usr/local/    # 移動dir2..., opt檔案夾到/usr/local/路徑下
    [root@db04 ~]# mv /tmp/dir1 ./
    [root@db04 ~]# mv /tmp/dir1 .
    [root@db04 ~]# mv /tmp/dir2 ../
    [root@db04 ~]# mv -t /tmp/ file1 file2 ./file3
    
    # 注: 在移動很多内容的檔案夾時使用下方指令
    絕對路徑執行指令:就是執行指令本身,不經過alias(别名)    # which cp擷取指令位置,使用 /usr/bin/cp ...
    指令前面加上轉義符'\':就是執行指令本身,不經過alias    # \mv hig qqq 或者 /bin/mv hig qqq      

删除 rm

rm:删除 remove
    -i:互動
    -r:遞歸
    -f:強制删除(force)
    -v:顯示删除過程
    
    [root@oldboyedu ~]# rm -r zls
    rm: descend into directory ‘zls’? y
    rm: descend into directory ‘zls/zls1’? y
    rm: descend into directory ‘zls/zls1/zls2’? y
    rm: remove directory ‘zls/zls1/zls2/oldboy’? y
    rm: remove directory ‘zls/zls1/zls2’? y
    rm: remove directory ‘zls/zls1’? y
    rm: remove directory ‘zls’? y
    注: 最好别名永久禁止rm指令,寫入/etc/bashrc重新開機也會生效, 再執行source /etc/bashrc即可      

檢視檔案内容cat tac

cat:檢視
    -n:檢視檔案内容并顯示行号
    -A:顯示檔案的結束符和tab鍵 相當于-vET
    ----------------------------------------
    -b:空行不顯示編号
    -E:顯示檔案的結束符($)
    -T:顯示檔案的tab鍵(^I)
    
    cat >> zls.txt << EOF    # 寫入檔案
    卧槽
    作者:曾老濕
    江山風景美如畫,
    本想吟詩贈天下。
    奈何自己沒文化,
    一句卧槽浪好大。
    謝謝大家。
    EOF

  >  :輸入重定向 
  >> :追加重定向
      
  [root@oldboyedu ~]# > /etc/bashrc  # 相當于清空檔案,非常危險
      
用法:cat [選項]... [檔案]...
    
    [root@oldboyedu ~]# cat 123
    file1
    [root@oldboyedu ~]# cat 456
    file2
    [root@oldboyedu ~]# cat 123 456
    file1
    file2
    
    tac    # 檔案内容從尾到頭反向輸出      

檢視檔案指令 more,less

more    # 按空格是翻頁, 按回車是一行一行顯示, 按q退出(cat把所有内容全加載到記憶體裡,太大會崩。用more沒事)
    [root@oldboyedu ~]# more /etc/services
    
    less    # 有查找功能
    [root@oldboyedu ~]# less /etc/services
    / + 關鍵字  回車
    n:向下搜尋
    N:向上搜尋(shift + n) 或者開啟大寫鍵也行
    [root@oldboyedu ~]# less -m -N /etc/services        # 其中-m顯示百分比,-N顯示行号。
    輸入操作指令G轉到最後一行。(輸入g跳轉到第一行)      
head + 檔案名
    預設顯示前10行
    -n:顯示前n行,n可以不寫,為數字,n後面加數字
    [root@oldboyedu ~]# head /etc/services
    #檢視檔案前20行
    [root@oldboyedu ~]# head -n 20 /etc/services 
    [root@oldboyedu ~]# head -20 /etc/services
    
    tail + 檔案名
    預設顯示後10行
    
    -f:追蹤檔案末尾 相當于 tailf    (通過再開一個視窗,echo 123 >> services左右對比測試)
    [root@oldboyedu ~]# tail -f services    等同于    tailf services
    -n:顯示後n行,n可以不寫,為數字,n後面加數字
    tail -n 5 services    或    tail -5 services      
grep:過濾關鍵字
    -n:顯示行号
    -o:隻顯示關鍵字        # grep -on 'root' /etc/passwd顯示行号不然分不清
    -B:顯示關鍵字的前n行,n為數字 (before)# grep -B 2 '呵呵' zls.txt
    -A:顯示關鍵字的後n行,n為數字 (after)   
    -C:顯示關鍵字的前後n行,n為數字(center)# grep -C 1 '想' zls.txt 等于 grep -B 1 -A 1 '想' zls.txt
   # grep -2 本想 zls.txt   # 可以不加直接 -數字
    -v:取反        # grep -v '^[a-f]' /etc/passwd
    -i:忽略大小寫,不區分大小寫
    -E:支援多關鍵字,多個關鍵字之間要用豎線 '|'分隔 相當于egrep  # grep -E 'root|bash|mail' /etc/passwd
                    # egrep '^root|^bash|^mail' /etc/passwd 以root,bash或mail開頭
    ^ :以...開頭
    $ :以...結尾
    . :代表任意字元
    * :代表所有    # grep '.*' /etc/passwd 比對所有
    | :管道符,将前面指令輸出的結果,交給後面的指令去處理  # ifconfig |grep eno
   -r, -R:遞歸過濾 某一目錄下的 檔案中的關鍵字
    
    [root@oldboyedu ~]# grep '^root' /etc/passwd    # 以root開頭
    [root@oldboyedu ~]# grep 'bash$' /etc/passwd    # 以bash結尾
    [root@oldboyedu ~]# grep -n '我' zls.txt    # -n顯示行數(從1開始計數)
    [root@oldboyedu ~]# grep -n '^$' zls.txt    # 查找空行
    
    #以數字的範圍開頭
    [root@oldboyedu ~]# grep -n '^[0-9]' passwd 
    26:1
    27:2
    28:3
    [root@oldboyedu ~]# grep -n '^[6-9]' passwd 
    [root@oldboyedu ~]# grep -n '^[1-2]' passwd 
    26:1
    27:2
    
    #以字母的範圍開頭
    [root@oldboyedu ~]# grep -n '^m' passwd 
    13:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    [root@oldboyedu ~]# grep -n '^[m-n]' passwd 
    13:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    17:nobody:x:99:99:Nobody:/:/sbin/nologin
    [root@oldboyedu ~]# grep -n '^[a-z]' passwd    # 開頭a-z範圍 
    [root@oldboyedu ~]# grep -n '^[a-Z]' passwd 
    [root@oldboyedu ~]# grep -i 'ftp' passwd      

繼續閱讀