目錄
- 檔案管理命令
- mkdir
- 查看內置還是外置命令
- type
- enable
- touch
- tree
- cp
- mv
- rm
- cat
- 系統檔案查看命令-more
- 系統檔案查看命令-less
- 系統檔案查看命令-head
- 系統檔案查看命令-tail
- 檔案上傳下載命令
- 檔案查找命令
- 字串處理的命令
- 字符排序命令-sort
- 字符去重命令-uniq
- 字符截取命令-cut
- 字符替換命令-tr
- 統計命令-wc
- free查看記憶體命令
檔案管理命令
mkdir
- -m:在創建目錄的時候設定權限
- -p:遞回創建目錄
- -v:顯示創建目錄的程序
mkdir zls{a..Z}
mkdir zls{A..z}
查看內置還是外置命令
type
[root@XX ~]#type wc
wc is /usr/bin/wc #小老弟這是內置命令
[root@XX ~]#type echo
echo is a shell builtin # 這是外置命令
enable
enable cmd 啟動內部命令
enable -n cmd 禁用內部命令
enable -n 不接內部命令則為查看所有禁用的內部命令
[root@XX ~]#enable -n echo
[root@XX ~]#enable -n enable -n echo
[root@XX ~]#enable echo
[root@XX ~]#enable -n
touch
#1. mkdir zls
#2. touch zls
## 不會報錯,雖然zls目錄已經存在,但是linux中一切皆檔案,修改'3種時間'
[root@zls ~]# touch /zlsxxx/abc
touch: cannot touch ‘/zlsxxx/abc’: No such file or directory
## touch創建檔案的時候,必須要在已經存在的目錄中
## 如果目錄權限不夠,touch也會報錯
## 當我使用root用戶,在已存在的目錄中touch檔案,永遠都不會報錯,
tree
-d:只顯示目錄,不顯示檔案
-L:顯示目錄的層級
cp
3個語法:copy
#cp -T 源檔案 目標檔案(必須起個名字)
#cp -t 目標路徑(也可以寫名字) 源檔案
[root@zls ~]# cp /etc/hosts /opt/
cp: overwrite ‘/opt/hosts’?
是否要覆寫
注意:cp命令,無法直接拷貝目錄,會修改檔案的屬性
cp 源檔案 目標路徑(也可以改名)
-a:相當于 -r -p ,既做到了遞回拷貝,又保持了檔案的屬性
-i:如果拷貝程序中,出現重復檔案名,則詢問是否要覆寫
-r:遞回拷貝檔案 -R --recursive
-p:在拷貝檔案的程序中,保持檔案原有的屬性
-v:顯示拷貝的程序
-t:將源檔案和目標,反過來
-f:強制拷貝
mv
語法:move
Usage: mv [OPTION]... SOURCE... DIRECTORY
mv [選項] 源檔案 目標目錄(改名)
[root@zls ~]# mv zls zls1
1.zls1是否存在,如果存在,并且是個目錄,則將zls檔案移動到zls1目錄中
2.zls1是否存在,如果不存在,則將zls檔案改名為zls1
3.zls1是否存在,如果存在,并且是個檔案,則會詢問是否要覆寫
注意:
1.mv命令,可以直接移動目錄,不需要遞回,mv不會修改檔案的屬性
2.mv 后面可以寫 n個檔案,但是最后一個必須是目錄
3.檔案不能覆寫目錄,但是可以放入目錄里面,目錄不能覆寫檔案,并且也不能放到檔案里面
-i:在移動的程序中,如果存在相同的檔案名,則詢問是否要覆寫
-f:強制移動
-t:將源檔案和目標,反過來
rm
語法:remove
Usage: rm [OPTION]... FILE...
rm [選項] 檔案名
-d:洗掉目錄(空目錄)
-r:遞回洗掉 -R, --recursive
-f:force 強制洗掉
-i:在洗掉檔案之前,會詢問是否真的要洗掉(一個一個檔案問)
-I:也詢問,但是只問一次
-v:顯示洗掉的程序
注意:
1.在Linux中,沒有回收站,所以rm會永久洗掉檔案
2.洗掉檔案的時候,盡量使用 rm -f
3.如果檔案或者目錄不存在,也不會報錯
rm -f file
rm -fr /dir
rm -f zls*
rm -f zls1*
------
rm -fr /tmp/* #洗掉/tmp目錄下面的所有內容,但是/tmp目錄本身還在
rm -fr /tmp/ #洗掉/tmp目錄下面的所有內容,包括/tmp目錄本身也會被洗掉
------ 區別很大
cat
語法:cat
Usage: cat [OPTION]... [FILE]...
cat [選項] 檔案名
-A:相當于-v -E -T
-n:查看檔案并顯示出檔案的行數(空行也編號)
-T:以^I,標注檔案中的TAB鍵
-t:相當于 -v -T
-E:以$符,標注一行的結尾
-e:相當于 -E -v
-b:空行不給編號
[root@zls ~]# cat >> menu <<eof
> 1.蘋果
> 2.梨
> 3.香蕉
> eof
tac 把檔案反過來看
系統檔案查看命令-more
語法:more [選項] 檔案
回車:按行往下看
空格:按頁往下看
n:向下查找
/:/xxx 搜索檔案中含有xxx的內容行
q:退出
系統檔案查看命令-less
語法:less [選項] 檔案
回車:按行往下看
空格:按頁往下看
/:搜索內容,并高亮顯示
n:向下查找關鍵字
N:向上查找關鍵字
gg:回到第一行
G:跳轉到最后一行
冒號:指定翻頁行數
#查看檔案內容
[root@aaa ~]# less /etc/services
系統檔案查看命令-head
語法:head [選項] 檔案
作用:查看一個檔案的前N行,默認是前十行,
#顯示/etc/services檔案的前十行內容
[root@localhost ~]# head /etc/services
-n:指定顯示檔案多少行內容(n可以省略)
-10
-11
不指定行數,默認情況下,只顯示10行,
`head /etc/service` = `head -n 10 /etc/service` = `head -10 /etc/service`
#查看檔案頭三行
[root@aaa ~]# head -3 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#查看檔案前10行
[root@aaa ~]# head -3 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
[root@aaa ~]# head /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports
系統檔案查看命令-tail
語法:tail [選項] 檔案
作用:查看一個檔案的后N行,默認是后十行
#顯示/etc/services檔案的后十行內容
[root@localhost ~]# tail /etc/services
-n:指定顯示檔案后數多少航內容(n可以省略)
#follow,追蹤檔案末尾
[root@localhost ~]# tail -f /var/log/messages
-f:追蹤檔案的末尾,實時查看日志的新內容
-c:顯示后數N個字符,(空格也算字符)
-F: -f --retry
tailf = tail -f
不指定行數,默認情況先,只顯示10行
`tail /etc/services` = `tail -n 10 /etc/services` = `tail -10 /etc/services`
#查看檔案后10行
[root@aaa ~]# tail /etc/services
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
#查看檔案后3行
[root@aaa ~]# tail -3 /etc/services
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
#實時查看檔案資訊
[root@aaa ~]# tail -f /var/log/messages
Mar 23 14:01:01 aaa systemd: Created slice User Slice of root.
Mar 23 14:01:01 aaa systemd: Started Session 5 of user root.
Mar 23 14:01:01 aaa systemd: Removed slice User Slice of root.
Mar 23 14:40:29 aaa systemd: Created slice User Slice of root.
Mar 23 14:40:29 aaa systemd: Started Session 6 of user root.
Mar 23 14:40:29 aaa systemd-logind: New session 6 of user root.
#顯示后數5個字符
[root@aaa ~]# tail -c 5 /etc/services
oker
#實時查看檔案內容等于tail -f
[root@aaa ~]# tailf /etc/services
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
檔案上傳下載命令
[root@localhost ~]# yum install -y lrzsz
rz:上傳
[root@localhost tmp]# rz
sz:下載 (把虛擬機里面的檔案,下載到我們的物理機上windows)
[root@localhost tmp]# sz a.txt
# wget
-O : 指定我要下載的位置,檔案名
檔案查找命令
# 查找檔案或目錄locate
yum install -y mlocate
updatedb
[root@localhost opt]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
# 查找命令的命令
[root@localhost opt]# which netstat
$():執行命令的結果
``:執行命令的結果
# 查找命令
[root@localhost ~]# type -a ls
ls 是 `ls --color=auto' 的別名
ls 是 /usr/bin/ls
# 查找命令
[root@localhost ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
例:
$()呼叫內容
[root@oldboy mlocate]# ping $(hostname -I)
PING 10.0.0.200 (10.0.0.200) 56(84) bytes of data.
64 bytes from 10.0.0.200: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 10.0.0.200: icmp_seq=2 ttl=64 time=0.058 ms
查看命令位置
[root@oldboy mlocate]# type -a netstat
netstat is /bin/netstat
netstat is /usr/bin/netstat
[root@oldboy mlocate]# which netstat
/bin/netstat
[root@oldboy mlocate]# whereis netstat
netstat: /usr/bin/netstat /usr/share/man/man8/netstat.8.gz
字串處理的命令
字符排序命令-sort
用法:sort [選項]... [檔案]...
[root@localhost ~]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11
[root@localhost ~]# sort sort.txt
a:4
b:3
c:2
d:1
e:5
f:11
注意:什么都不加,默認按照首字母排序
-t:指定分隔符(默認空格為分隔符)
-k:指定按照哪一列排序
-n:按照阿拉伯數字大小排序(默認是按照數字的首個字符排序)
-r:倒敘
[root@localhost ~]# sort -t ':' -k 2 -n sort.txt
d:1
c:2
b:3
a:4
e:5
f:11
[root@localhost ~]# sort -nrk 2 -t ':' sort.txt
字符去重命令-uniq
用法:uniq [選項]... [檔案]
注意:去重,必須在排好序之后才能去重
-c:count 統計,計數,重復的值有多少
[root@localhost ~]# sort uniq.txt
123
123
456
456
789
abc
abc
abc
abc
[root@localhost ~]# sort uniq.txt|uniq
123
456
789
abc
[root@localhost ~]# sort uniq.txt|uniq -c
2 123
2 456
1 789
4 abc
例:
新建一個文本檔案,將檔案中的內容進行排序,然后去重,注意:必須先排序才支持去重
[root@oldboy ~]# sort aaa.txt|uniq -c
1
246 123
123 123123
123 123456
123 345
123 567
字符截取命令-cut
用法:cut [選項]... [檔案]...
-d:指定分隔符
-f:指定區域
-c:按照字符截取
[root@localhost ~]# cut -d '.' -f 1-4 ip
[root@localhost ~]# cut -d ' ' -f 2,6 info.txt |cut -d ',' -f 2
18 133411023
例:
以/etc/passwd為例,以冒號為分隔符,取出第三列的內容
[root@oldboy ~]# cut -d ':' -f 3 /etc/passwd
0
1
2
81
999
74
89
1000
字符替換命令-tr
#問題發現最好在替換的時候選擇字符長度一致字串,否則建議使用sed命令
[root@localhost ~]# cat info.txt
I'm zls,18 years old QQ 133411023
[root@localhost ~]# cat info.txt |tr 'QQ' 'qq'
I'm zls,18 years old qq 133411023
統計命令-wc
[root@localhost ~]# wc /etc/services
11176 61033 670293 /etc/services
[root@localhost ~]# wc -l /etc/services
11176 /etc/services
[root@localhost ~]# wc -w /etc/services
61033 /etc/services
[root@localhost ~]# wc -c /etc/services
670293 /etc/services
注意:在wc命令,什么選項都不加的情況下,統計出檔案的行數,單詞數,和字符數
-l:按照行數統計
-w:按照單詞數統計
-c:按照字符數統計
free查看記憶體命令
[root@db01 day04]# free
total used free shared buff/cache available
Mem: 2028088 102084 1612636 9792 313368 1746144
Swap: 1048572 0 1048572
total:總記憶體大小
total=used+free+buff/cache
used:記憶體已經使用了的大小
free:記憶體空閑大小
shared:記憶體共享空間
行程之間是相互隔離的,當需要將行程之間的資源進行隔離,可以使用管道,將一個行程的輸出結果,交個另外一個行程的輸入,因此兩個
行程之間就實作了共享,理所當然,也就使用了記憶體中的共享空間的資源,
buff/cache
Cache:快取區
Cache并不是快取檔案的,而是快取塊的(塊是I/O讀寫最小的單元);Cache一般會用在I/O請求上,如果多個行程要訪問某個檔案,
可以把此檔案讀入Cache中,這樣下一個行程獲取CPU控制權并訪問此檔案直接從Cache讀取,提高系統性能,
用通俗的話來說就是將硬碟中的資料放入到記憶體中,方便下次的讀取,加快讀取速度
buff:緩沖區
存盤慢的資料先把資料存放到buffer,達到一定程度存盤塊的設備再讀取buffer的資料,在此期間存盤快的設備CPU可搜索以干其他的事情,
通俗話來說就是將資料先存盤至記憶體,不直接交由至硬碟,等待達到一定的資料量,再一次性保存至硬碟中
# 注意:
當記憶體可用空間較多時,系統會自動呼叫可用記憶體交由其他程式作為buff或者cache使用,加快讀寫速度,
當記憶體空間不足,但buff或者cache占用較多空間,可以使用命令將記憶體中的快取資料進行釋放,
例如:
sync 將記憶體中的資料首先保存至磁盤中
echo 3 > /proc/sys/vm/drop_caches 將記憶體中的資源進行釋放
available:真實程式可以用的記憶體大小
這里才是最真實的反饋
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/123995.html
標籤:Linux
