Linux云计算训练营笔记day05(Rocky Linux中的命令:管道操作 |、wc、find、vim)

发布于:2025-05-10 ⋅ 阅读:(15) ⋅ 点赞:(0)

管道操作 |
作用: 将前面命令的输出,传递给后面命令,作为后面命令的参数

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 关闭自动缩进

 


网站公告

今日签到

点亮在社区的每一天
去签到