Linux基礎(day7)

2.10 環境變量PATH

which命令介紹

  • which 能夠查看命令所在的路徑
  • which查詢的路徑是經過如下的路徑,進行查詢
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • 這個路徑又以 : 分割 若是命令在以上路徑內,能夠不用輸入絕對路徑,真正使用命令是經過絕對路徑進行操做

which例子

[root@hf-01 ~]# which ls	查看ls所在路徑
alias ls='ls --color=auto'
    /usr/bin/ls
[root@hf-01 ~]# ls /usr/bin/ls	
/usr/bin/ls
[root@hf-01 ~]# cp /usr/bin/ls /tmp/ls2     複製/usr/bin/ls路徑到/tmp/ls2下
[root@hf-01 ~]# /tmp/ls2	使用命令/tmp/ls2查看結果,會發現和ls命令出來的結果同樣
anaconda-ks.cfg
[root@hf-01 ~]# ls		
anaconda-ks.cfg
[root@ahf-01 ~]# ls2	執行ls2命令,會提示未找到命令,由於這個命令不在上述目錄裏面
-bash: ls2:未找到命令
[root@hf-01 ~]# PATH=$PATH:/tmp/    若想直接運行ls2命令,須要改變環境變量	從新給PATH賦值
[root@hf-01 ~]# echo $PATH         會發現多出了/tmp/
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[root@hf-01 ~]# ls2	這時在運行發現就能夠執行了
anaconda-ks.cfg
[root@hf-01 ~]# which ls2	
/tmp/ls2

新建的環境變量在終端失效了

  • 這時,在終端,複製SSH渠道,打開一個終端,執行ls2命令,會發現又失效了 (打開終端方法,鼠標右擊-複製SSH渠道)
  • 若想一直生效,

解決方法

1.在系統 vi /etc/profile		(在開機、打開終端都會加載這個命令)
2.結尾處加上PATH=$PATH:/tmp/ 並保存退出
3.在使用cat /etc/profile查看下是否加載成功
4.這時echo $PATH會獲得/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/tmp/:/root/bin
如今無論哪個終端均可以執行ls2命令,都會執行成功

解除ls2命令,方法

若不想要這個ls2命令了,有兩種方法。mysql

  1. 方法一:從新賦值linux

    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin		(去除:/tmp/)
    這時在執行ls2命令,就會失效
  2. 方法二:從新編輯文件/etc/profileredis

    快捷鍵dd命令,刪除PATH=$PATH:/tmp/並:wq保存
    在去新建終端,執行echo $PATH
    就會發現/tmp/目錄消失了

2.11 cp命令

cp = copysql

使用方法

將源文件 拷貝成目標文件
cp -r 拷貝目錄
統一約定, 使用cp 和其餘命令的時候,把 路徑後的/補充完整
!$  上一條命令中最後的一個參數,以空格或歎號分割

[root@hf-01 ~]# which cp
alias cp='cp -i'
	/usr/bin/cp
這裏會看到cp  命令默認帶有一個 -i 選項,
它是屬於安全選項,詢問操做是否進行下一步操做

若不想複製的時候,天天去詢問,能夠按以下複製
[root@hf-01 ~]# /usr/bin/cp /etc/passwd /tmp/1.txt

cp例子

[root@hf-01 ~]# cp /etc/passwd /tmp/1.txt      拷貝文件直接複製便可
cp:是否覆蓋"/tmp/1.txt"? y
[root@hf-01 ~]# cp -r /tmp/aminglinux/ /tmp/amning       拷貝目錄須要加-r選項
[root@hf-01 ~]# cp -r /tmp/aminglinux/ /tmp/aming1/
[root@hf-01 ~]# tree !$     這表示上一條命令的最後一條參數
tree /tmp/aming1/
/tmp/aming1/
└── 2
    └── 2.txt

1 directory, 1 file
[root@hf-01 ~]# !tree
tree /tmp/aming1/
/tmp/aming1/
└── 2
    └── 2.txt

1 directory, 1 file
[root@hf-01 ~]# tree /tmp/aming1/
/tmp/aming1/
└── 2
    └── 2.txt

1 directory, 1 file
[root@hf-01 ~]# cp -r /tmp/aminglinux/ /tmp/aming1/
[root@hf-01 ~]# ls /tmp/aming1/
2  aminglinux   這說明,當目標目錄已經存在的時候,他會把源目錄放在目標目錄下面去,
若是目標目錄不存在,他會把源目錄拷貝過來,並修更名稱
[root@hf-01 ~]# tree /tmp/aming1/
/tmp/aming1/
├── 2
│   └── 2.txt
└── aminglinux
    └── 2
        └── 2.txt

3 directories, 2 files
[root@hf-01 ~]# cp -r /tmp/aminglinux/ /tmp/aming1/
cp:是否覆蓋"/tmp/aming1/aminglinux/2/2.txt"? n
當再次拷貝相同目錄的時候,就會提示是否須要覆蓋,這時由於源目錄已經存在

!$和!tree命令的含義

  • !$ 執行上一條命令的最後一條參數
  • !tree 執行歷史中,最後一次執行tree的命令

cp拷貝目錄加/

在cp拷貝目錄的時候,在目錄後面統一加上/,由於在後面有一種命令中加/和不加/是有很大區別的安全

總結

  • 當目標目錄已經存在的時候,他會把源目錄放在目標目錄下面去,若是目標目錄不存在,他會把源目錄拷貝過來,並修更名稱
  • 當再次拷貝相同目錄的時候,就會提示是否須要覆蓋,這時由於源目錄已經存在

2.12 mv命令

mv介紹

  • mv = move
  • mv 至關於把文件挪動地方,若是在同一個目錄就是更改文件名
  • 可使用這個命令實現,移動到一個目錄下,同時更改文件名
  • mv 一樣,默認使用命令的時候 ,帶有一個 -i 選項,就是也安全選項
    • 若想不須要提示,則可以使用絕對路徑
  • 假如,挪動的目錄是挪動到一個不存是目錄下是,就會變成,等同於更名字

mv例子

[root@hf-01 ~]# mv anaconda-ks.cfg anaconda-ks.cfg.1   這裏就是直接修改文件名
[root@hf-01 ~]# ls
anaconda-ks.cfg.1
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# ls
1.txt  aming1  aminglinux  amning  mysql.sock

[root@hf-01 tmp]# mv aming1/ aming/  
[root@hf-01 tmp]# ls
aming  aminglinux  amning  mysql.sock
[root@hf-01 tmp]# mv aming/ aming2/     若把目錄移動到當前一個不存在的目錄,則會修更名稱
[root@hf-01 tmp]# ls
aming2  aminglinux  amning  mysql.sock
[root@hf-01 tmp]# mv aming2/ aminglinux/     若是目標目錄存在了,就會把源目錄放到目標目錄下面去
[root@hf-01 tmp]# ls aminglinux/
2  aming2

總結

  • 目標文件:若是「目標文件」是文件名則在移動文件的同時,將其更名爲「目標文件」;
  • 若是「目標文件」是目錄名則將源文件移動到「目標文件」下。
  • 若是源是目錄,目標也是目錄,且目標目錄中已經存在一個與源相同的目錄,則移動失敗。
  • 若目標與源同名且都是文件時,則目標文件會被覆蓋

2.13 文檔查看cat/more/less/head/tail

cat/tac/more/wc用法

cat 這個查看文件內容的
    -A 顯示文件的全部文件(包括字符)
    -n 顯示行號
tac 倒序查看文件內容,與cat相反
more 也是用來查看文件內容,可是不會像cat同樣一下所有顯示出來,他的顯示方式爲一屏一屏的顯示,
    (可以使用空格鍵查看下一行或ctrl+b 能夠往前看,內容查看完之後會自動結束命令的運行)
wc -l 能夠查看文件的行數
[root@localhost ~]# wc -l anaconda-ks.cfg.1
51 anaconda-ks.cfg.1    顯示改文件的行數

>> 追加劇定向/more

  • 追加劇定向就是把文件中的內容增長到另外一個文件中去
[root@localhost ~]# wc -l anaconda-ks.cfg.1
51 anaconda-ks.cfg.1
[root@localhost ~]# cat /etc/passwd >> anaconda-ks.cfg.1    這就是把/etc/passwd中的文件內容增長到anaconda-ks.cfg.1文件中去
[root@localhost ~]# cat /etc/passwd >> anaconda-ks.cfg.1
[root@localhost ~]# wc -l anaconda-ks.cfg.1
93 anaconda-ks.cfg.1
[root@localhost ~]# cat /etc/passwd >> anaconda-ks.cfg.1
[root@localhost ~]# cat /etc/passwd >> anaconda-ks.cfg.1

more例子

  • more命令查看文本內容
    • 當內容較多時,能夠「空格鍵」,一頁一頁的往下翻看,直到內容結束,會自動退出。
    • ,若想向上翻看以前的看過的內容,使用「快捷鍵ctrl+b」去查看
[root@localhost ~]# more anaconda-ks.cfg.1   
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
此處省略....

less用法

  • less剛開歐式進去和more相似
    • 支持「空格鍵」一屏一屏的翻看
      • 快捷鍵ctrl+B向前查看
      • 快捷鍵ctrl+F向後查看
    • 支持方向鍵 「上下鍵」查看
    • 當查看結束,退出時,需按「q鍵」(quit)退出
    • 還能夠在文檔中查找,用 斜槓/加查找的內容,並==高亮顯示==出來,好比/==root==在文檔中查找並標記出來
      • 按 n鍵 往下查找下一個,並高亮顯示
      • 快捷鍵shift+n 往前查找
      • 總結:其實就是大寫N鍵,向前查看,小寫n鍵向後查看
    • 使問號?加查找內容,搜索?==chrony==是到文章最後
      • 按 n鍵 從後往前 查找
    • 按 shift+g鍵 定位到行尾
    • 按 g 定位到行首

總結

  • less命令的用法中包括了more命令全部功能,因此徹底可使用less

head和tail用法

head 查看文件的前 10行 (默認10行)
    -n 數字     (指定查看文件的多少行)
tail   查看文件的尾部,最後 10行
    -f 動態顯示文件
    -n 數字     顯示文件以數字爲單位的行數(頭幾行,爲幾行)

head和tail例子

[root@localhost ~]# head anaconda-ks.cfg.1
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
[root@localhost ~]# head -n 2 anaconda-ks.cfg.1
#version=DEVEL
# System authorization information
[root@localhost ~]# tail -n 2 anaconda-ks.cfg.1
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

tail -f用法

  • tail -f命令,動態顯示文件
  • 通常在查看日誌會用到
  • 按ctrl+c退出
相關文章
相關標籤/搜索