學習大數據——linux文件目錄類命令(包含修改提示符和修改歷史格式)

pwd 顯示當前工作目錄的絕對路徑

pwd:print working directory 打印工作目錄

  1. 基本語法
    pwd (功能描述:顯示當前工作目錄的絕對路徑)
  2. 案例實操
    (1)顯示當前工作目錄的絕對路徑
    [root@hadoop101 ~]# pwd
    /root
    

ls 列出目錄的內容

ls:list 列出目錄內容

  1. 基本語法
    ls [選項] [目錄或是文件]
  2. 選項說明
選項 功能
-a 全部的文件,連同隱藏檔( 開頭爲 . 的文件) 一起列出來(常用)
-l 長數據串列出,包含文件的屬性與權限等等數據;(常用)
  1. 顯示說明
    每行列出的信息依次是: 文件類型與權限 鏈接數 文件屬主 文件屬組 文件大小用byte來表示 建立或最近修改的時間 名字
  2. 案例實操
    (1)查看當前目錄的所有內容信息
[root@hadoop100 ~]# ls
anaconda-ks.cfg      install.log.syslog  模板  文檔  桌面
forlearnsmartd.conf  learndir            視頻  下載
install.log          公共的              圖片  音樂
[root@hadoop100 ~]# ll
總用量 108
-rw-------. 1 root root  1248 2月  23 08:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  6745 2月  23 14:40 forlearnsmartd.conf
-rw-r--r--. 1 root root 41954 2月  23 08:15 install.log
-rw-r--r--. 1 root root  9154 2月  23 08:11 install.log.syslog
drwxr-xr-x. 3 root root  4096 2月  24 10:39 learndir
drwxr-xr-x. 2 root root  4096 2月  23 00:28 公共的
drwxr-xr-x. 2 root root  4096 2月  23 00:28 模板
drwxr-xr-x. 2 root root  4096 2月  23 00:28 視頻
drwxr-xr-x. 2 root root  4096 2月  23 00:28 圖片
drwxr-xr-x. 2 root root  4096 2月  23 00:28 文檔
drwxr-xr-x. 2 root root  4096 2月  23 00:28 下載
drwxr-xr-x. 2 root root  4096 2月  23 00:28 音樂
drwxr-xr-x. 3 root root  4096 2月  23 18:23 桌面

cd 切換目錄

cd:Change Directory切換路徑

  1. 基本語法
    cd [參數]
  2. 參數說明
參數 功能
cd 絕對路徑 切換路徑
cd相對路徑 切換路徑
cd ~或者cd 回到自己的家目錄
cd - 回到上一次所在目錄
cd … 回到當前目錄的上一級目錄
cd -P 跳轉到實際物理路徑,而非快捷方式路徑

3.案例實操
(1)使用絕對路徑切換到root目錄

[root@hadoop101 ~]# cd /root/

(2)使用相對路徑切換到“公共的”目錄

[root@hadoop101 ~]# cd 公共的/

(3)表示回到自己的家目錄,亦即是 /root 這個目錄

[root@hadoop101 公共的]# cd ~

(4)cd- 回到上一次所在目錄

[root@hadoop101 ~]# cd -

(5)表示回到當前目錄的上一級目錄,亦即是 “/root/公共的”的上一級目錄的意思;

[root@hadoop101 公共的]# cd ..

mkdir 創建一個新的目錄

mkdir:Make directory 建立目錄

  1. 基本語法
    mkdir [選項] 要創建的目錄
  2. 選項說明
選項 功能
-p 創建多層目錄
  1. 案例實操
    (1)創建一個目錄
[root@hadoop101 ~]# mkdir xiyou
[root@hadoop101 ~]# mkdir xiyou/mingjie

(2)創建一個多級目錄

[root@hadoop101 ~]# mkdir -p xiyou/dssz/meihouwang

rmdir 刪除一個空的目錄

rmdir:Remove directory 移動目錄

  1. 基本語法:
    rmdir 要刪除的空目錄
  2. 案例實操
    (1)刪除一個空的文件夾
[root@hadoop101 ~]# rmdir xiyou/dssz/meihouwang

touch 創建空文件

  1. 基本語法
    touch 文件名稱
  2. 案例實操
[root@hadoop101 ~]# touch xiyou/dssz/sunwukong.txt

cp 複製文件或目錄

  1. 基本語法
    cp [選項] source dest (功能描述:複製source文件到dest)
  2. 選項說明
選項 功能
-r 遞歸複製整個文件夾
  1. 參數說明
參數 功能
source 源文件
dest 目標文件
  1. 經驗技巧
    強制覆蓋不提示的方法:\cp
  2. 案例實操
    (1)複製文件
 [root@hadoop101 ~]# cp xiyou/dssz/suwukong.txt xiyou/mingjie/

(2)遞歸複製整個文件夾

 [root@hadoop101 ~]# cp -r xiyou/dssz/ ./
 

rm 移除文件或目錄

  1. 基本語法
    rm [選項] deleteFile (功能描述:遞歸刪除目錄中所有內容)
  2. 選項說明
選項 功能
-r 遞歸刪除目錄中所有內容
-f 強制執行刪除操作,而不提示用於進行確認。
-v 顯示指令的詳細執行過程
  1. 案例實操
    (1)刪除目錄中的內容
[root@hadoop101 ~]# rm xiyou/mingjie/sunwukong.txt

(2)遞歸刪除目錄中所有內容

[root@hadoop101 ~]# rm -rf dssz/

mv 移動文件與目錄或重命名

  1. 基本語法
    (1)mv oldNameFile newNameFile (功能描述:重命名)
    (2)mv /temp/movefile /targetFolder (功能描述:移動文件)
  2. 案例實操
    (1)重命名
[root@hadoop101 ~]# mv xiyou/dssz/suwukong.txt xiyou/dssz/houge.txt

(2)移動文件

[root@hadoop101 ~]# mv xiyou/dssz/houge.txt ./

cat 查看文件內容

查看文件內容,從第一行開始顯示。

  1. 基本語法
    cat [選項] 要查看的文件
  2. 選項說明
選項 功能描述
-n 顯示所有行的行號,包括空行。
  1. 經驗技巧
    一般查看比較小的文件,一屏幕能顯示全的。
  2. 案例實操
    (1)查看文件內容並顯示行號
[atguigu@hadoop101 ~]$ cat -n houge.txt 

more 文件內容分屏查看器

more指令是一個基於VI編輯器的文本過濾器,它以全屏幕的方式按頁顯示文本文件的內容。more指令中內置了若干快捷鍵,詳見操作說明。

  1. 基本語法
    more 要查看的文件
  2. 操作說明
操作 功能說明
空白鍵 (space) 代表向下翻一頁;
Enter 代表向下翻『一行』;
q 代表立刻離開 more ,不再顯示該文件內容。
Ctrl+F 向下滾動一屏
Ctrl+B 返回上一屏
= 輸出當前行的行號
:f 輸出文件名和當前行的行號

3.案例實操
(1)採用more查看文件

[root@hadoop101 ~]# more smartd.conf

less 分屏顯示文件內容

less指令用來分屏查看文件內容,它的功能與more指令類似,但是比more指令更加強大,支持各種顯示終端。less指令在顯示文件內容時,並不是一次將整個文件加載之後才顯示,而是根據顯示需要加載內容,對於顯示大型文件具有較高的效率。

  1. 基本語法
    less 要查看的文件
  2. 操作說明
操作 功能說明
空白鍵 向下翻動一頁;
[pagedown] 向下翻動一頁
[pageup] 向上翻動一頁;
/字串 向下搜尋『字串』的功能;n:向下查找;N:向上查找;
?字串 向上搜尋『字串』的功能;n:向上查找;N:向下查找;
q 離開 less 這個程序;

4.案例實操
(1)採用less查看文件

[root@hadoop101 ~]# less smartd.conf

echo

echo輸出內容到控制檯

  1. 基本語法
    echo [選項] [輸出內容]
    選項:
    -e: 支持反斜線控制的字符轉換
控制字符 作用
\ 輸出\本身
\n 換行符
\t 製表符,也就是Tab鍵
  1. 案例實操
[atguigu@hadoop101 ~]$ echo "hello\tworld"
hello\tworld
[atguigu@hadoop101 ~]$ echo -e "hello\tworld"
hello		world

查看當前的shell

[root@hadoop100 ~]# echo $SHELL
/bin/bash

查看shell的類型,直接輸入即可執行

[root@hadoop100 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
[root@hadoop100 ~]# /bin/bash

顯示10-20之間的數,+2遞增

[root@hadoop100 ~]# echo {10..20..2}
10 12 14 16 18 20

[root@hadoop100 ~]# echo file{a,b,c}
filea fileb filec

笛卡爾積顯示

[root@hadoop100 ~]# echo file{a,b,c}.{log,txt}
filea.log filea.txt fileb.log fileb.txt filec.log filec.txt

創建笛卡爾積中的所有文件

[root@hadoop100 ~]# touch file{a,b,c}.{log,txt}

修改提示符

提示符的格式,PS1變量名

[root@hadoop100 ~]# echo $PS1
[\u@\h \W]\$

提示符的格式,你可以在bash中查找:

[root@hadoop100 ~]# man bash

找到這裏,查找PROMPTING

       PS1    The  value  of  this  parameter  is expanded (see
              PROMPTING below) and used as the  primary  prompt
              string.  The default value is ‘‘\s-\v\$ ’’.
       PS2    The  value  of this parameter is expanded as with
              PS1 and used as the secondary prompt string.  The
              default is ‘‘> ’’.
       PS3    The value of this parameter is used as the prompt
              for the select command (see SHELL GRAMMAR above).
       PS4    The  value  of this parameter is expanded as with
              PS1 and the value is printed before each  command
              bash  displays  during  an  execution trace.  The
              first character of  PS4  is  replicated  multiple
              times,  as necessary, to indicate multiple levels
              of indirection.  The default is ‘‘+ ’’.
       SHELL  The full pathname to the shell is  kept  in  this
              environment  variable.  If it is not set when the
              shell starts, bash assigns to it the  full  path-
              name of the current user’s login shell.
       TIMEFORMAT
              The  value  of this parameter is used as a format
              string specifying how the timing information  for
              pipelines  prefixed  with  the time reserved word
              should be displayed.  The % character  introduces
              an  escape  sequence  that  is expanded to a time
              value or other information.  The escape sequences
              and  their  meanings  are  as follows; the braces
              denote optional portions.
              %%        A literal %.
              %[p][l]R  The elapsed time in seconds.
/PROMPTING

找到,介紹了格式

PROMPTING
       When  executing interactively, bash displays the primary
       prompt PS1 when it is ready to read a command,  and  the
       secondary  prompt  PS2  when it needs more input to com-
       plete a command.  Bash allows these prompt strings to be
       customized  by  inserting  a number of backslash-escaped
       special characters that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the date in "Weekday  Month  Date"  format
                     (e.g., "Tue May 26")
              \D{format}
                     the  format  is  passed to strftime(3) and
                     the result is  inserted  into  the  prompt
                     string;  an  empty  format  results  in  a
                     locale-specific time representation.   The
                     braces are required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first ‘.’
              \H     the hostname
              \j     the  number  of  jobs currently managed by
                     the shell
              \l     the  basename  of  the  shell’s   terminal
                     device name
              \n     newline
              \r     carriage return
              \s     the  name of the shell, the basename of $0
                     (the portion following the final slash)
              \t     the current time in 24-hour HH:MM:SS  for-
                     mat
:

PS1="{}":修改提示符

/etc/profile.d/env.sh文件中
加入:PS1="{}"
:永久修改提示符,寫入到文件中;

head 顯示文件頭部內容

head用於顯示文件的開頭部分內容,默認情況下head指令顯示文件的前10行內容。

  1. 基本語法
    head 文件 (功能描述:查看文件頭10行內容)
    head -n 5 文件 (功能描述:查看文件頭5行內容,5可以是任意行數)
  2. 選項說明
選項 功能
-n <行數> 指定顯示頭部內容的行數
  1. 案例實操
    (1)查看文件的頭2行
[root@hadoop101 ~]# head -n 2 smartd.conf

tail 輸出文件尾部內容

tail用於輸出文件中尾部的內容,默認情況下tail指令顯示文件的後10行內容。

  1. 基本語法
    (1)tail 文件 (功能描述:查看文件後10行內容)
    (2)tail -n 5 文件 (功能描述:查看文件後5行內容,5可以是任意行數)
    (3)tail -f 文件 (功能描述:實時追蹤該文檔的所有更新)
  2. 選項說明
選項 功能
-n<行數> 輸出文件尾部n行內容
-f 顯示文件最新追加的內容,監視文件變化

3.案例實操
(1)查看文件尾1行內容

[root@hadoop101 ~]# tail -n 1 smartd.conf 

(2)實時追蹤該檔的所有更新

[root@hadoop101 ~]# tail -f houge.txt

> 覆蓋 和 >> 追加

  1. 基本語法
    (1)ll >文件 (功能描述:列表的內容寫入文件a.txt中(覆蓋寫))
    (2)ll >>文件 (功能描述:列表的內容追加到文件aa.txt的末尾)
    (3)cat 文件1 > 文件2 (功能描述:將文件1的內容覆蓋到文件2)
    (4)echo “內容” >> 文件
  2. 案例實操
    (1)將ls查看信息寫入到文件中
[root@hadoop101 ~]# ls -l>houge.txt

(2)將ls查看信息追加到文件中

[root@hadoop101 ~]# ls -l>>houge.txt

(3)採用echo將hello單詞追加到文件中

[root@hadoop101 ~]# echo hello>>houge.txt

ln 軟鏈接

軟鏈接也成爲符號鏈接,類似於windows裏的快捷方式,有自己的數據塊,主要存放了鏈接其他文件的路徑。

  1. 基本語法
    ln -s [原文件或目錄] [軟鏈接名] (功能描述:給原文件創建一個軟鏈接)
  2. 經驗技巧
    刪除軟鏈接: rm -rf 軟鏈接名,而不是rm -rf 軟鏈接名/
    查詢:通過ll就可以查看,列表屬性第1位是l,尾部會有位置指向。
  3. 案例實操
    (1)創建軟連接
[root@hadoop101 ~]# mv houge.txt xiyou/dssz/
[root@hadoop101 ~]# ln -s xiyou/dssz/houge.txt ./houzi
[root@hadoop101 ~]# ll
lrwxrwxrwx. 1 root    root      20 6月  17 12:56 houzi -> xiyou/dssz/houge.txt

(2)刪除軟連接

[root@hadoop101 ~]# rm -rf houzi

(3)進入軟連接實際物理路徑

[root@hadoop101 ~]# ln -s xiyou/dssz/ ./dssz
[root@hadoop101 ~]# cd -P dssz/

history 查看已經執行過歷史命令

  1. 基本語法
    history (功能描述:查看已經執行過歷史命令)
  2. 案例實操
    (1)查看已經執行過的歷史命令
[root@hadoop101 test1]# history

補充:
history:列出所有執行的命令,只記錄最近的一千條歷史,在HISTSIZE變量中
HISTSIZE 變量在/etc/prifile中,可以修改。

命令 執行
!+歷史命令編號 重新執行命令
!-編號 執行倒數第編號條命令
ctrl+p+回車 執行前一條命令
!+字符串 重複前一個以字符串開頭的命令
!?+字符串 包含字符串的命令
!+字符串:p 僅打印,並不執行
!$:p 打印輸出!$(上一條命令的最後一個參數)的內容
!*:p 打印輸出!*(上一條命令的所有參數)的內容
^+字符串 刪除上一條命令中的第一個字符串
^+字符串1+(shift+6)字符串2 將上一條命令的第一個字符串1替換爲字符串2
!:gs/字符串/字符串2 將上一條命令中所有字符串1替換爲字符串2
history -d 12 刪除編號爲12的命令歷史
history -a 將內存中的命令寫到文件中
history -r 將文件中的內容寫到內存中
history -w 將當前新命令追加到文件中
history -n 將新增加的歷史增加到歷史列表中
history -p +“command” “command” 執行命令但不寫到歷史中
history -s + “string” 僞造歷史,但並不執行

執行時歷史命令放在內存中,退出系統後,將內存中歷史命令寫進文件中,命令存放在磁盤中用戶家目錄中的.bash_history隱藏文件中,當用戶登錄時,從此文件中加載到內存中,而執行history -c後是清除內存中的命令歷史,從而用戶退出時上一次的命令歷史就被清掉,無法加載到文件中去

1)rm .bash_history
2)history -c
:刪除歷史文件,同時清除內存中的歷史記錄,執行順序很重要,否則會暴露刪除文件命令,在重新生成的.bash_history文件中。

修改歷史格式
修改歷史格式,想要永久更改,需要寫入配置文件/etc/profile.d/env.sh文件中
命令歷史相關的環境變量:
HISTSIZE:命令歷史記錄的條數
HISTFILE:指定歷史文件,默認爲~/.bash_history
HISTFILESIZE:命令歷史文件記錄歷史的條數
HISTTIMEFORMAT="%F %T ":顯示時間
HISTIGNORE="STR1:STR2*…"忽略str1命令,str2開頭的歷史
HISTCONTROL=
ignorespace:忽略所有以空格開頭的命令
ignoredups:忽略重複的命令,連續相同爲重複
ignoreboth:相當於ignoredups和ignorespace的組合
erasedups:刪除重複命令,可以不連續的
系統的配置文件/etc/profile文件中

例如:HISTTIMEFORMAT="%F %T "修改歷史格式

大部分引用自尚硅谷課件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章