管道操作 |
作用: 將前面命令的輸出,傳遞給后面命令,作為后面命令的參數
head -3 /etc/passwd | tail -1 ?取第三行
head -8 /etc/passwd | tail -3 | cat -n ?取6 7 8行
ifconfig | head -2 | tail -1 ?只查看IP地址
ifconfig | grep 192 ?過濾192的ip地址
wc是一個統計工具,可以統計文件中的行數,單詞數,字符數,字節數,以及最長行的長度,分析日志文件
wc 選項 ?文件?
echo hello world > a.txt ?把hello world寫入a.txt中
echo hello >> a.txt ? ? ?把hello追加到a.txt中
選項:
-w 單詞數 ?wc -w a.txt ? ?3個單詞
-l 文件的行數 wc -l a.txt ?2行
? ? ? ? ? ? ?cat /etc/passwd | wc -l 45行
? ? ? ? ? ? ?ls ?/opt | wc -l 2行
? ? ? ? ? ? ?ls | wc -l 查看當前的文件或者目錄有多少個
-m 字符數 wc -m a.txt ?18個字符 ?單詞15個 空格算1個 換行\n算1個,一共有2個
在a.txt中輸入一個中文字符,-c 字節數 wc -c a.txt ?一個漢字是三個字節
? ?UTF-8 編碼 通常使用 3 個字節來表示
? ?GBK 通常使用 2 個字節來表示
L 最長行的長度 ? wc -L a.txt ?11個(hello world)不涉及換行
Linux中大多數的配置文件都是以#開頭,這個叫注釋
顯示配置文件有效信息(去除注釋 以#開頭, 去除空行 ^$ )
grep -v ^# /etc/login.defs | grep -v ^$ ?| cat -n > a.txt
把/root/.bashrc配置文件中的有效信息保存到gongli.txt中
grep -v ^# /root/.bashrc | grep -v ^$ > gongli.txt
練習:
1)創建目錄 /study/nsd01
? mkdir -p ?/study/nsd01
2)在 /study/nsd01 創建文件abc.txt,利用echo 寫入內容 abc.tedu.cn
? echo ?abc.tedu.cn > abc.txt
3)將/study/nsd01/abc.txt 文件復制到/opt目錄下,同時改名為test.txt
? cp /study/nsd01/abc.txt ?/opt/test.txt
4)使用vim修改文件/etc/hostname,刪除原來內容,寫入www.sina.com
? echo ?www.sina.com > /etc/hostname
5)將/etc/passwd,/etc/hostname,/etc/hosts同時拷貝到/study/nsd01?
? cp /etc/passwd /etc/hostname /etc/hosts /study/nsd01?
6)將文件/study/nsd01/hostname 重命名為host.txt
? mv /study/nsd01/hostname ?/study/nsd01/host.txt
7)把目錄/boot內容中以vm開頭的數據復制到/root/vm目錄下(自己創建vm目錄)
? mkdir /root/vm
? cp /boot/vm* /root/vm
8)將/home 目錄復制到/root/vm目錄下
? cp -r ?/home ?/root/vm
9)創建/root/boothome與/root/usrsbin目錄
? mkdir ?/root/boothome ?/root/usrsbin
10)打包/boot和/home這兩個文件夾,壓縮包名字為boothome.tar.gz?
? ?tar -czf ?boothome.tar.gz ?/boot ?/home
11)打包/usr/sbin目錄,壓縮包名字為usrsbin.tar.bz2?
? ?tar -cjf usrsbin.tar.bz2 /usr/sbin
12)解壓boothome.tar.gz到/study/nsd01
? ?tar -xf ?boothome.tar.gz ?-C /study/nsd01
find 精確查找
find ?目錄 ?條件
條件
-type ?類型 (f 文件 ?d目錄 l快捷方式)
?? ?find ?/boot ?-type d
?? ??
?? ?touch ?/opt/a.txt
?? ?touch ?/opt/b.txt
?? ?mkdir ?/opt/nsd
?? ?find ?/opt ?-type f
-name 名字
find /etc -name "passwd"
find /etc -name "*tab"
find /etc -name "*tab" | cat -n
find /etc -name "*.conf"
find /etc -name "*.conf" | wc -l
find /root -name ".*" ? 查找隱藏數據
兩個條件一起使用:
mkdir /mnt/cbd01
mkdir /mnt/cbd02
touch /mnt/cbd03.txt
find /mnt -name "cbd*"
find /mnt -name "cbd*" -type d ? ? 兩個必須都滿足
find /mnt -name "cbd*" -type f ? ? 兩個必須都滿足
find /mnt -name "cbd*" -o -type f ?兩個滿足其中一個
-size ?大小 ?+ -
ls -lh /boot
find ?/boot -size +1M ? ? 大于1M的數據
find ?/boot -size +1M -size -10M ? ?1M到10M之間的數據
-user 用戶名,按照數據的所有者
find /home -user nsd ?普通用戶的名字
-mtime ?修改時間(所有的時間都是過去時間)
+90 ?90天之前修改過的數據
-10 ?最近10天之內修改過的數據
/var 存放經常變化的數據,日志文件
find /var -mtime +90 ?三個月之前的數據
-newermt ?在此時間之后
! -newermt 在此時間之前 , 不寫年月日則表示今天
find ?/var -newermt '2025-5-8 15:28:50'
find ?/var -newermt '2025-5-8 10:30:50' ! -newermt '12:30:50'
find高級使用
處理find找到的數據,每查找一個就傳遞一次
find [范圍] ?[條件] ?-exec ?處理命令 {} \;
-exec 額外操作的開始
{} ? ?前面find查找的結果
\; ? ?額外操作的結束
find ?/boot -size +10M -exec cp {} /opt \;
find ?/boot -size +10M -exec ls -lh {} \;
兩個條件聯合使用
mkdir /root/mytab
find ?/etc -name "*tab" -type f -exec cp {} /root/mytab \;
案例:
利用find查找,數據的所有者為student,并且必須是文件,把他們拷貝到/root/findfiles目錄中
useradd ?student ? ? 添加student用戶
mkdir ?/root/findfiles
find / -user student -type f -exec cp {} /root/findfiles \;
/proc: 內存的數據,不占用硬盤空間?
紅帽RHCSA題目:
1.查找屬于 jacques 用戶所屬文件,并拷貝到/root/findfiles 目錄
useradd ?jacques ? ? 添加student用戶
mkdir ?/root/findfiles
find / -user jacques -type f -exec cp {} /root/findfiles \;
2.查找文件 /usr/share/xml/iso-codes/iso_639_3.xml 中包含字符串 ng 的所有行。將所有這些行的副本按原始順序放在文件 /root/list 中。/root/list 不得包含空行且所有行必須是 /usr/share/xml/iso-codes/iso_639_3.xml 中原始行的確切副本
grep ng ?/usr/share/xml/iso-codes/iso_639_3.xml > /root/list
3.創建一個名為/root/backup.tar.bz2的tar存檔,其應包含/usr/local的tar存檔,其應包含/usr/local的內容。該tar存檔使用bzip2進行壓縮。
tar ?-cjf ?/root/backup.tar.bz2 ?/usr/local
vim 文本編輯器
cp ?/etc/passwd user
vim user
命令模式
? ?yy 復制一行 ?p粘貼
? ?10yy復制十行 ?
? ?dd ?刪除1行
? ?10dd ?刪除10行
? ?G 跳轉到末尾
? ?gg 跳轉到首行
? ?/a 查找字符串a
? ? ? n ?跳到下一個結果
? ? ? N ?跳到上一個結果
? ?u 撤銷
? ?ctrl +r 取消上一次撤銷
? ?ZZ ?保存修改并退出
插入模式
? ?自己隨便寫東西
末行模式
? ?:set nu 顯示行號
? ?:set nonu 關閉行號
? ?:set ai 啟用縮進
? ?:set noai 關閉自動縮進
?