210 環(huán)境變量PATH
環(huán)境變量作用
所有的命令其實(shí)使用時應(yīng)該使用它的絕對路徑屎慢,例如
[root@mylinux ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
這里的ls命令應(yīng)該是使用 /usr/bin/ls,但是我們能直接使用命令忽洛,就是因?yàn)樘砑恿谁h(huán)境變量
我們看到下面打印出來的環(huán)境變量
[root@mylinux ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin (這里就是我們存放的環(huán)境變量)
暫時改變環(huán)境變量
只有當(dāng)我們的命令在環(huán)境變量里面就能讓系統(tǒng)找到該命令腻惠,下面我們演示一下
[root@mylinux ~]# ls
anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# cp /usr/bin/ls /root/ls2 (復(fù)制ls并且改名為ls2到當(dāng)然root家目錄下面來)
[root@mylinux ~]# ls
anaconda-ks.cfg frp_0.11.0_linux_amd64 ls2 (已經(jīng)有l(wèi)s2了)
[root@mylinux ~]# ls2
-bash: ls2: 未找到命令 (我們直接使用發(fā)現(xiàn)ls2并不能生效)
[root@mylinux ~]# /root/ls2 (必須使用絕對路徑才能生效)
anaconda-ks.cfg frp_0.11.0_linux_amd64 ls2
[root@mylinux ~]# PATH=$PATH:/root/ (我們把ls2所在絕對路徑目錄加入到環(huán)境變量中)
[root@mylinux ~]# echo $PATH (打印環(huán)境變量)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/ (發(fā)現(xiàn)最后出現(xiàn)了:/root/)
[root@mylinux ~]# ls2 (再來直接使用ls2就生效了)
anaconda-ks.cfg frp_0.11.0_linux_amd64 ls2
永久改變環(huán)境變量
上述的方法并不能實(shí)現(xiàn)對環(huán)境變量的永久改變,我們看下圖我重新連接了一個客戶端之后ls2就已經(jīng)不生效了
環(huán)境變量沒有永久改變
[root@mylinux ~]# vi /etc/profile (更改每次終端連接都會加載的文件)
并在文件最后加上PATH=$PATH:/root/并保存就可以使之永久生效
最后一行加上PATH=$PATH:/root/
重新開啟終端發(fā)現(xiàn)已經(jīng)生效
重新開啟終端發(fā)現(xiàn)已經(jīng)生效
需要刪除的話可以直接刪除文件里面的PATH=$PATH:/root/
211 cp命令
拷貝一個文件到某處并改名
[root@mylinux ~]# cp /root/2/1.txt /root/3/2.txt (拷貝1.txt到3文件夾下面并且改名為2.txt)
[root@mylinux ~]# ls /root/3/
2.txt
拷貝一個文件到某處并覆蓋里面的某一個文件
[root@mylinux ~]# cp /root/2/1.txt /root/3/2.txt
cp:是否覆蓋"/root/3/2.txt"欲虚? y
[root@mylinux ~]# ls /root/3
2.txt
拷貝目錄到某處
[root@mylinux ~]# cp -r /root/2/ /root/3/ (建議所有目錄結(jié)尾的路徑都加上/)
[root@mylinux ~]# tree /root/3/
/root/3
├── 2
│ └── 1.txt
└── 2.txt
1 directory, 2 files
也可以
[root@mylinux ~]# tree !$ (!$表示上一條命令里面的最后一個參數(shù))
tree /root/3
/root/3
├── 2
│ └── 1.txt
└── 2.txt
1 directory, 2 files
[root@mylinux ~]# which cp
alias cp='cp -i' (我們發(fā)現(xiàn)cp是cp -i的別名集灌,這里面的-i表示詢問是一個安全參數(shù),即上面我們看到的是否覆蓋就是因?yàn)檫@個參數(shù))
/usr/bin/cp
[root@mylinux ~]# which rm
alias rm='rm -i' (rm 也有 -i 安全參數(shù))
/usr/bin/rm
[root@mylinux ~]# ls
2 3 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# cp -r ./2/ ./3/(拷貝目錄如果目標(biāo)目錄下有該目錄了就會詢問是否覆蓋)
cp:是否覆蓋"./3/2/1.txt"复哆? n
[root@mylinux ~]# cp -r ./3/ ./2/ (如果沒有就不會詢問)
[root@mylinux ~]# ls
2 3 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# ls ./2/
1.txt 3
212 mv命令
修改文件名字
[root@mylinux ~]# ls
2 3 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# mv ./2/ ./4/ (修改目錄名稱)
[root@mylinux ~]# ls
3 4 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# touch 1.txt
[root@mylinux ~]# mv ./1.txt ./2.txt (修改文件名稱)
[root@mylinux ~]# ls
2.txt 3 4 anaconda-ks.cfg frp_0.11.0_linux_amd64
移動文件(或目錄)并且更改名字或者只移動文件(文件或目錄)
[root@mylinux ~]# ls
2.txt 3 4 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# mv 2.txt ./3/ (只更移動文件)
[root@mylinux ~]# ls ./3/
2.txt
[root@mylinux ~]# ls
3 4 anaconda-ks.cfg frp_0.11.0_linux_amd64
[root@mylinux ~]# mv ./3/2.txt ./5.txt
[root@mylinux ~]# ls
3 4 5.txt anaconda-ks.cfg frp_0.11.0_linux_amd64
移動時如果文件或者文件夾重名會詢問是否覆蓋欣喧,目標(biāo)目錄如果不存在相當(dāng)于改名字
213 文檔查看cat_more_less_head_tail命令
cat命令
cat順序查看文件
tac倒序查看文件
cat和tac
[root@mylinux ~]# cat -A 5.txt ($行位符號)
1$
2$
$
[root@mylinux ~]# cat -n 5.txt (顯示行號)
1 1
2 2
3
[root@mylinux ~]#
more命令
查看文件,按屏幕分頁梯找,并顯示當(dāng)前所看百分比唆阿。(按空格鍵向下翻頁,Ctrl + B 可以回翻)
分屏查看文件
[root@mylinux ~]# wc -l anaconda-ks.cfg (查看文件內(nèi)容行數(shù))
51 anaconda-ks.cfg
>>符號
通過 >> 符號可以使前面的文件內(nèi)容追加到后面的文件里面去(這里是從最后寫入進(jìn)去)
[root@mylinux ~]# cat 1.txt
a b c
[root@mylinux ~]# cat 5.txt
1 2 3 4
[root@mylinux ~]# cat 1.txt >> 5.txt
[root@mylinux ~]# cat 5.txt
1 2 3 4
a b c
less命令
[root@mylinux ~]# less anaconda-ks.cfg (和more差不多锈锤,但是支持方向鍵一行一行的向下翻看和向上回看驯鳖,也支持空格翻頁,Ctrl + B 回翻久免,到最后需要按q鍵退出)
在lees中還有搜索功能浅辙,可以輸入/然后后面跟上字符串搜索字符串,搜索出來的字符串高亮顯示阎姥,這是可以按n查看下一個记舆,Shift + n 可以查看上一個。
輸入/然后后面跟上字符串搜索字符串呼巴,搜索出來的字符串高亮顯示泽腮,這是可以按n查看上一個,Shift + n 可以查看下一個衣赶。
Shift + g定位到末尾诊赊,g定位到文首
head命令
查看文件內(nèi)容的頭十行
[root@mylinux ~]# head -n 2 anaconda-ks.cfg (查看頭兩行)
[root@mylinux ~]# head anaconda-ks.cfg
#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
tail命令
查看文件內(nèi)容的末尾十行
[root@mylinux ~]# tail -f anaconda-ks.cfg (查看動態(tài)文件)
[root@mylinux ~]# tail anaconda-ks.cfg
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end