当前位置:   article > 正文

Linux系统(总结)

linux系统

目录

 一、Linux系统

         1.什么是Linux系统

         2.Linux基础命令

二、Alias别名系统

         1.什么是别名系统

         2.别名系统的设置

三、查看文件

         1.为什么要会查看文件

2.怎么查看文件

全量查看文件内容

分页查看文件内容

动态查看

过滤查找文件中想要的内容

grep -n 显示匹配内容的所在行号

grep -c 查找匹配内容一共有多少行的总数量

grep -v 反向选取

grep -e 条件满足其中一个,都可过滤内容显示出来(后面可跟多个或者单个内容条)


 一、Linux系统

1.什么是Linux系统

Linux是一套免费使用和自由传播的类Unix操作系统,是一个多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

2.Linux基础命令

在Linux中,命令分为内部命令和外部命令,命令拓展也分为很多不同功能的命令进行展开使用,结合使用,达到我们日常工作的需求

内部命令:由系统自带命令,可以直接进行载入使用执行

外部命令:不属于系统的命令,需要额外安装,或者由Shell载入执行的命令

基本命令:

pwd:查询当前所在目录的全路径

cd:切换目录 (cd    当前目录下的目录名称 或者 绝对路径)

cd . .  :返回上一层目录

cd - :返回上一次执行的目录

ls:查看当前目录下的文件和目录

ls - a:查看当前目录下的所以文件目录,包括隐藏文件

ls - i:显示当前目录下所以文件目录的Inode号

df - i:显示可用的Inode号数量

du -sh *:人性化的展示当前目录所有文件目录的自盘占用量(包括子目录子文件)

df - h:人性化展示所以系统文件的占用量

ln:创建硬链接(ln  源文件名  创建的硬链接文件名)硬链接和他的连接文件Inode号相同

ln - s:创建软连接(相当于Windows系统中的创建快捷方式)

mkdir:在当前目录下创建目录,后面可跟创建单个或者多个

mkdir -p:在当前目录下递归式创建目录(目录和子目录)

touch:在当前目录下创建文件

touch  {XX..XX} . 后缀:在当前目录下指定范围性的创建多个文件

cp:复制文件

cp - f:强制复制

cp - i:提醒性复制

cp - p:保留复制文件的所以权限进行复制

cp - r:复制目录(包括目录下的文件和子目录)

cp - a:复制目录下所有子目录,文件,并保留源文件的所有权限

rm - rf:强制删除指定文件目录

rm - rf *:强制删除当前目录下的所有文件

mv:移动文件或者目录      如果是不存在的目的目录则会变成重命名功能

二、Alias别名系统

1.什么是别名系统

别名系统就是相当于给一个复杂的命令创建一个快捷方式命令,相当于简化命令,但是重启虚拟机后除默认别名系统的命令,其他设置的会消失,设置的快捷命令也要符合合法字符,即是数字或者字母字符串,用户家目录下的Bashrc修改只对当前用户生效,在Source.bashrc中修改并保存

格式:alias    ‘源命令’    别名的命令名称

2.别名系统的设置

在这里别名设置的快捷命令,不可用已有的命令,不然会发生命令上的冲突

命令的执行优先级:绝对路径的命令 > 别名系统 > 内部命令 > Hash缓存命令 > 外部命令

直接输入alias可以查询目前已设置过的别名系统的命令

三、查看文件

1.为什么要会查看文件

遇到不熟悉的问题报错时,需要一步步去排错,要在日志和配置文件内容中进行查看排错,在Linux中大量的文件内容,占用巨大内存的文件许许多多,尤其是在日常生活工作学习当中,在那么多的文件中,怎样筛选出我们需要去修改,删除,移动,查看的文件,就要用到许许多多基础的筛选查看修改的命令。

2.怎么查看文件

全量查看文件内容

(过大的文件会造成虚机卡顿)

命令:cat             查看文件内容

           cat -n         对内容增添行号进行查看

           cat -b         对空白行不进行编辑行号查看

  1. [root@localhost /]# cat /var/log/boot.log
  2. [ OK ] Started Show Plymouth Boot Screen.
  3. [ OK ] Reached target Paths.
  4. [ OK ] Reached target Basic System.
  5. [ OK ] Found device /dev/mapper/centos-root.
  6. Starting File System Check on /dev/mapper/centos-root...
  7. [ OK ] Started File System Check on /dev/mapper/centos-root.
  8. [ OK ] Started dracut initqueue hook.
  9. Mounting /sysroot...
  10. [ OK ] Reached target Remote File Systems (Pre).
  11. [ OK ] Reached target Remote File Systems.
  12. [ OK ] Mounted /sysroot.
  13. [ OK ] Reached target Initrd Root File System.
  14. Starting Reload Configuration from the Real Root...
  15. [ OK ] Started Reload Configuration from the Real Root.
  16. [ OK ] Reached target Initrd File Systems.
  17. [ OK ] Reached target Initrd Default Target.
  18. Starting dracut pre-pivot and cleanup hook...
  19. [ OK ] Started dracut pre-pivot and cleanup hook.
  20. Starting Cleaning Up and Shutting Down Daemons...
  21. Starting Plymouth switch root service...
  22. [ OK ] Stopped target Timers.
  23. [ OK ] Stopped Cleaning Up and Shutting Down Daemons.
  24. [ OK ] Stopped dracut pre-pivot and cleanup hook.
  25. Stopping dracut pre-pivot and cleanup hook...
  26. [ OK ] Stopped target Remote File Systems.
  27. [ OK ] Stopped target Remote File Systems (Pre).
  28. [ OK ] Stopped dracut initqueue hook.
  29. Stopping dracut initqueue hook...
  30. [ OK ] Stopped target Initrd Default Target.
  31. [ OK ] Stopped target Basic System.
  32. [ OK ] Stopped target Slices.
  33. [ OK ] Stopped target Paths.
  34. [ OK ] Stopped target System Initialization.
  35. Stopping udev Kernel Device Manager...
  36. [ OK ] Stopped udev Coldplug all Devices.
  37. Stopping udev Coldplug all Devices...
  38. [ OK ] Stopped target Local File Systems.
  39. [ OK ] Stopped target Swap.
  40. [ OK ] Stopped Apply Kernel Variables.
  41. Stopping Apply Kernel Variables...
  42. [ OK ] Stopped target Sockets.
  43. [ OK ] Stopped udev Kernel Device Manager.
  44. [ OK ] Stopped Create Static Device Nodes in /dev.
  45. Stopping Create Static Device Nodes in /dev...
  46. [ OK ] Stopped Create list of required static device nodes for the current kernel.
  47. Stopping Create list of required static device nodes for the current kernel...
  48. [ OK ] Stopped dracut pre-udev hook.
  49. Stopping dracut pre-udev hook...
  50. [ OK ] Stopped dracut cmdline hook.
  51. Stopping dracut cmdline hook...
  52. [ OK ] Closed udev Control Socket.
  53. [ OK ] Closed udev Kernel Socket.
  54. Starting Cleanup udevd DB...
  55. [ OK ] Started Cleanup udevd DB.
  56. [ OK ] Reached target Switch Root.
  57. [ OK ] Started Plymouth switch root service.
  58. Starting Switch Root...
  59. Welcome to CentOS Linux 7 (Core)!
  60. [ OK ] Stopped Switch Root.
  61. [ OK ] Stopped Journal Service.
  62. Starting Journal Service...
  63. [ OK ] Created slice system-getty.slice.
  64. [ OK ] Stopped target Switch Root.
  65. [ OK ] Created slice system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice.
  66. Mounting Huge Pages File System...
  67. [ OK ] Listening on LVM2 poll daemon socket.
  68. Mounting POSIX Message Queue File System...
  69. [ OK ] Stopped target Initrd Root File System.
  70. Mounting Debug File System...
  71. [ OK ] Listening on udev Control Socket.
  72. [ OK ] Listening on Device-mapper event daemon FIFOs.
  73. [ OK ] Reached target Login Prompts.
  74. [ OK ] Listening on LVM2 metadata daemon socket.
  75. Starting Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling...
  76. Starting Create list of required static device nodes for the current kernel...
  77. [ OK ] Created slice User and Session Slice.
  78. [ OK ] Reached target Slices.
  79. [ OK ] Listening on udev Kernel Socket.
  80. [ OK ] Listening on /dev/initctl Compatibility Named Pipe.
  81. [ OK ] Stopped target Initrd File Systems.
  82. [ OK ] Stopped File System Check on Root Device.
  83. Stopping File System Check on Root Device...
  84. Starting Apply Kernel Variables...
  85. [ OK ] Listening on Delayed Shutdown Socket.
  86. [ OK ] Reached target User and Group Name Lookups.
  87. Starting Remount Root and Kernel File Systems...
  88. Mounting NFSD configuration filesystem...
  89. [ OK ] Set up automount Arbitrary Executable File Formats File System Automount Point.
  90. [ OK ] Mounted Debug File System.
  91. [ OK ] Mounted Huge Pages File System.
  92. [ OK ] Mounted POSIX Message Queue File System.
  93. [ OK ] Started Create list of required static device nodes for the current kernel.
  94. [ OK ] Started Apply Kernel Variables.
  95. [ OK ] Started Remount Root and Kernel File Systems.
  96. [ OK ] Started Journal Service.
  97. [ OK ] Started LVM2 metadata daemon.
  98. Starting LVM2 metadata daemon...
  99. Starting Load/Save Random Seed...
  100. Starting Flush Journal to Persistent Storage...
  101. Starting Configure read-only root support...
  102. Starting Rebuild Hardware Database...
  103. Starting Create Static Device Nodes in /dev...
  104. [ OK ] Started Load/Save Random Seed.
  105. [ OK ] Started Flush Journal to Persistent Storage.
  106. [ OK ] Started Create Static Device Nodes in /dev.
  107. Starting udev Kernel Device Manager...
  108. [ OK ] Started Configure read-only root support.
  109. [ OK ] Mounted NFSD configuration filesystem.
  110. [ OK ] Started udev Kernel Device Manager.
  111. [ OK ] Created slice system-systemd\x2drfkill.slice.
  112. Starting Load/Save RF Kill Switch Status of rfkill0...
  113. [ OK ] Started Load/Save RF Kill Switch Status of rfkill0.
  114. [ OK ] Started Rebuild Hardware Database.
  115. Starting udev Coldplug all Devices...
  116. [ OK ] Started udev Coldplug all Devices.
  117. Starting udev Wait for Complete Device Initialization...
  118. [ OK ] Found device VMware_Virtual_S 1.
  119. [ OK ] Created slice system-lvm2\x2dpvscan.slice.
  120. Starting LVM2 PV scan on device 8:2...
  121. [ OK ] Started Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling.
  122. [ OK ] Reached target Local File Systems (Pre).
  123. Mounting /boot...
  124. [ OK ] Found device /dev/mapper/centos-swap.
  125. Activating swap /dev/mapper/centos-swap...
  126. [ OK ] Started LVM2 PV scan on device 8:2.
  127. [ OK ] Activated swap /dev/mapper/centos-swap.
  128. [ OK ] Reached target Swap.
  129. [ OK ] Mounted /boot.
  130. [ OK ] Started udev Wait for Complete Device Initialization.
  131. Starting Activation of DM RAID sets...
  132. [ OK ] Started Activation of DM RAID sets.
  133. [ OK ] Reached target Local File Systems.
  134. Starting Migrate local SELinux policy changes from the old store structure to the new structure...
  135. Starting Rebuild Journal Catalog...
  136. Starting Import network configuration from initramfs...
  137. Starting Preprocess NFS configuration...
  138. Starting Tell Plymouth To Write Out Runtime Data...
  139. [ OK ] Reached target Encrypted Volumes.
  140. [ OK ] Started Rebuild Journal Catalog.
  141. Starting Update is Completed...
  142. [ OK ] Started Tell Plymouth To Write Out Runtime Data.
  143. [ OK ] Started Preprocess NFS configuration.
  144. [ OK ] Started Update is Completed.
  145. [ OK ] Started Migrate local SELinux policy changes from the old store structure to the new structure.
  146. [ OK ] Started Import network configuration from initramfs.
  147. Starting Create Volatile Files and Directories...
  148. [ OK ] Started Create Volatile Files and Directories.
  149. Mounting RPC Pipe File System...
  150. Starting Security Auditing Service...
  151. [ OK ] Mounted RPC Pipe File System.
  152. [ OK ] Started Security Auditing Service.
  153. Starting Update UTMP about System Boot/Shutdown...
  154. [ OK ] Started Update UTMP about System Boot/Shutdown.
  155. [ OK ] Reached target System Initialization.
  156. [ OK ] Listening on Avahi mDNS/DNS-SD Stack Activation Socket.
  157. [ OK ] Listening on CUPS Printing Service Sockets.
  158. [ OK ] Listening on RPCbind Server Activation Socket.
  159. [ OK ] Listening on Virtual machine lock manager socket.
  160. [ OK ] Listening on Open-iSCSI iscsid Socket.
  161. [ OK ] Listening on Open-iSCSI iscsiuio Socket.
  162. [ OK ] Reached target Timers.
  163. [ OK ] Listening on Virtual machine log manager socket.
  164. [ OK ] Listening on D-Bus System Message Bus Socket.
  165. [ OK ] Reached target Sockets.
  166. [ OK ] Reached target Paths.
  167. [ OK ] Reached target Basic System.
  168. [ OK ] Started VGAuth Service for open-vm-tools.
  169. Starting VGAuth Service for open-vm-tools...
  170. Starting Dump dmesg to /var/log/dmesg...
  171. [ OK ] Started ABRT Automated Bug Reporting Tool.
  172. Starting ABRT Automated Bug Reporting Tool...
  173. [ OK ] Started Self Monitoring and Reporting Technology (SMART) Daemon.
  174. Starting Self Monitoring and Reporting Technology (SMART) Daemon...
  175. Starting GSSAPI Proxy Daemon...
  176. [ OK ] Started libstoragemgmt plug-in server daemon.
  177. Starting libstoragemgmt plug-in server daemon...
  178. Starting OpenSSH Server Key Generation...
  179. Starting NTP client/server...
  180. [ OK ] Started D-Bus System Message Bus.
  181. Starting D-Bus System Message Bus...
  182. Starting Authorization Manager...
  183. Starting Modem Manager...
  184. Starting Install ABRT coredump hook...
  185. Starting Avahi mDNS/DNS-SD Stack...
  186. [ OK ] Started Manage Sound Card State (restore and store).
  187. Starting Manage Sound Card State (restore and store)...
  188. Starting Kernel Samepage Merging...
  189. Starting Resets System Activity Logs...
  190. Starting Bluetooth service...
  191. Starting Login Service...
  192. Starting RealtimeKit Scheduling Policy Service...
  193. [ OK ] Started irqbalance daemon.
  194. Starting irqbalance daemon...
  195. [ OK ] Started Hardware RNG Entropy Gatherer Daemon.
  196. Starting Hardware RNG Entropy Gatherer Daemon...
  197. Starting Initial Setup configuration program graphical interface...
  198. [ OK ] Started Service for virtual machines hosted on VMware.
  199. Starting Service for virtual machines hosted on VMware...
  200. Starting Accounts Service...
  201. Starting System Logging Service...
  202. [ OK ] Started ABRT kernel log watcher.
  203. Starting ABRT kernel log watcher...
  204. [ OK ] Started Dump dmesg to /var/log/dmesg.
  205. [ OK ] Started GSSAPI Proxy Daemon.
  206. [ OK ] Started Kernel Samepage Merging.
  207. [ OK ] Started Resets System Activity Logs.
  208. [ OK ] Started Initial Setup configuration program graphical interface.
  209. [ OK ] Started System Logging Service.
  210. [ OK ] Started NTP client/server.
  211. [ OK ] Started Avahi mDNS/DNS-SD Stack.
  212. [ OK ] Started RealtimeKit Scheduling Policy Service.
  213. [ OK ] Started Bluetooth service.
  214. [ OK ] Started Install ABRT coredump hook.
  215. [ OK ] Started Login Service.
  216. [ OK ] Started Modem Manager.
  217. Starting Hostname Service...
  218. [ OK ] Reached target Bluetooth.
  219. Starting Initial Setup configuration program...
  220. Starting Kernel Samepage Merging (KSM) Tuning Daemon...
  221. [ OK ] Reached target NFS client services.
  222. [ OK ] Started Authorization Manager.
  223. [ OK ] Started Accounts Service.
  224. Starting firewalld - dynamic firewall daemon...

查看目标文件的内容(不可以进行修改,只能读) 

分页查看文件内容

命令:more      /绝对路径

回车可以一行一行往下查看,空格则是查看下面整页内容

当看完文件内容后,会动退出文件,而且不可搜索指定文件内容

  1. [root@localhost /]# more /var/log/boot.log
  2. [ OK ] Started Show Plymouth Boot Screen.
  3. [ OK ] Reached target Paths.
  4. [ OK ] Reached target Basic System.
  5. [ OK ] Found device /dev/mapper/centos-root.
  6. Starting File System Check on /dev/mapper/centos-root...
  7. [ OK ] Started File System Check on /dev/mapper/centos-root.
  8. [ OK ] Started dracut initqueue hook.
  9. Mounting /sysroot...
  10. [ OK ] Reached target Remote File Systems (Pre).
  11. [ OK ] Reached target Remote File Systems.
  12. [ OK ] Mounted /sysroot.
  13. [ OK ] Reached target Initrd Root File System.
  14. Starting Reload Configuration from the Real Root...
  15. [ OK ] Started Reload Configuration from the Real Root.
  16. [ OK ] Reached target Initrd File Systems.
  17. [ OK ] Reached target Initrd Default Target.
  18. Starting dracut pre-pivot and cleanup hook...
  19. [ OK ] Started dracut pre-pivot and cleanup hook.
  20. Starting Cleaning Up and Shutting Down Daemons...
  21. Starting Plymouth switch root service...
  22. [ OK ] Stopped target Timers.
  23. [ OK ] Stopped Cleaning Up and Shutting Down Daemons.
  24. [ OK ] Stopped dracut pre-pivot and cleanup hook.
  25. Stopping dracut pre-pivot and cleanup hook...
  26. --More--(10%)

进行分页查看,下面More的10%就是已经显示的内容占总内容的多少(显示进度)

命令:less   /绝对路径

也可以进行分页查看,区别在于看完文件内容后不会自动退出,需要按q手动退出,浏览完后也可返回翻看,同时可以搜索指定内容。

  1. 总用量 16
  2. drwxr-xr-x. 14 root root 177 425 20:09 ./
  3. drwxr-xr-x. 21 root root 4096 425 20:09 ../
  4. drwxrwxr-x. 2 abrt abrt 6 810 2017 abrt-di/
  5. drwxrwx---. 3 root lp 17 425 20:09 cups/
  6. drwxr-xr-x. 2 root root 4096 425 20:05 fontconfig/
  7. drwxr-xr-t. 2 root gdm 6 87 2017 gdm/
  8. drwxr-xr-x. 3 root root 17 425 20:04 ibus/
  9. drwxr-xr-x. 2 root root 6 88 2017 krb5rcache/
  10. drwx------. 2 root root 23 425 20:05 ldconfig/
  11. drwx--x--x. 3 root root 18 425 20:04 libvirt/
  12. drwxr-xr-x. 35 root root 4096 426 10:33 man/
  13. drwxr-xr-x. 4 root root 60 425 20:11 PackageKit/
  14. drwxr-xr-x. 2 root root 6 1112 2016 realmd/
  15. drwxr-xr-x. 3 root root 20 425 20:11 yum/

可以进行内容搜索(:/搜索的内容),会显示出相对匹配的内容

动态查看

命令:tail -f   /绝对路径

从文件内容底部开始向上查看,不指定行数的情况下默认显示十行内容

实时显示可以随时查看错误,定位操作问题,每进行一步操作下面则会显示操作的步骤日志

拓展命令

命令:tail -n   /绝对路径    展示指定的固定行数内容(不实时显示)

输入   tail  -n  5  /var/log/messages  后面展示了五行内容 

  1. [root@localhost /]# tail -n 5 /var/log/messages
  2. May 4 14:10:01 localhost systemd: Starting Session 46 of user root.
  3. May 4 14:20:01 localhost systemd: Started Session 47 of user root.
  4. May 4 14:20:01 localhost systemd: Starting Session 47 of user root.
  5. May 4 14:30:01 localhost systemd: Started Session 48 of user root.
  6. May 4 14:30:01 localhost systemd: Starting Session 48 of user root.
  7. [root@localhost /]#

拓展命令

命令 :tail  -fn  数字  /绝对路径  动态查看指定固定数量的内容

  1. [root@localhost /]# tail -fn 5 /var/log/messages
  2. May 4 14:10:01 localhost systemd: Starting Session 46 of user root.
  3. May 4 14:20:01 localhost systemd: Started Session 47 of user root.
  4. May 4 14:20:01 localhost systemd: Starting Session 47 of user root.
  5. May 4 14:30:01 localhost systemd: Started Session 48 of user root.
  6. May 4 14:30:01 localhost systemd: Starting Session 48 of user root.
  7. [root@localhost /]#

命令:head 

从顶部往下查看文件内容,不指定数量的情况下也是默认十行

head 因为从顶部开始向下显示,所以不存在实时显示,因为新操作的日志都是在下面更新新的日志,顶部不会更新日志 

  1. [root@localhost /]# head /var/log/messages
  2. Apr 25 20:09:19 localhost journal: Runtime journal is using 8.0M (max allowed 196.7M, trying to leave 295.1M free of 1.9G available → current limit 196.7M).
  3. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuset
  4. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpu
  5. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuacct
  6. Apr 25 20:09:19 localhost kernel: Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
  7. Apr 25 20:09:19 localhost kernel: Command line: BOOT_IMAGE=/vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=zh_CN.UTF-8
  8. Apr 25 20:09:19 localhost kernel: e820: BIOS-provided physical RAM map:
  9. Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
  10. Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
  11. Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
  12. [root@localhost /]#

拓展命令:head -n   /绝对路径     

从顶部开始固定显示指定行数数量的文件内容

  1. [root@localhost /]# head -n 5 /var/log/messages
  2. Apr 25 20:09:19 localhost journal: Runtime journal is using 8.0M (max allowed 196.7M, trying to leave 295.1M free of 1.9G available → current limit 196.7M).
  3. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuset
  4. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpu
  5. Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuacct
  6. Apr 25 20:09:19 localhost kernel: Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
  7. [root@localhost /]#

这个指令和less的用法一样,操作顺序也一样,区别在于head顶部开始显示,并没有实时显示的功能,不能用来排错和定位操作错误。 

平时所用到的以上排查文件命令并不能更改修改文件内容

仅仅用于排查错误操作和错误定位

cat  、more  、tail  、less 主要用于生活工作中排查错误日志文件与配置文件

过滤查找文件中想要的内容

命令:grep

针对行进行筛选,针对行进行内容显示处理,用于查找文件内容,字符内容,并不是查找文件名

  1. [root@localhost /]# grep root /var/log/messages
  2. May 4 14:40:01 localhost systemd: Started Session 49 of user root.
  3. May 4 14:40:01 localhost systemd: Starting Session 49 of user root.
  4. [root@localhost /]#

筛选含有root内容行,并显示出来

拓展命令:

grep -n 显示匹配内容的所在行号

  1. [root@localhost /]# grep root /var/log/messages
  2. 2:May 4 14:40:01 localhost systemd: Started Session 49 of user root.
  3. 3:May 4 14:40:01 localhost systemd: Starting Session 49 of user root.
  4. [root@localhost /]#

每行前面标注出了在第几行

grep -c 查找匹配内容一共有多少行的总数量

  1. [root@localhost /]# grep -c root /var/log/messages
  2. 2
  3. [root@localhost /]#

grep -c并不显示内容,只显示所匹配内容所在多少行的总数量 

grep -v 反向选取

  1. [root@localhost /]# grep -v root /var/log/messages
  2. May 4 14:39:01 localhost rsyslogd: [origin software="rsyslogd" swVersion="8.24.0" x-pid="740" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
  3. [root@localhost /]#

反选指定筛选内容的其他内容,除指定内容之外

grep -e 条件满足其中一个,都可过滤内容显示出来(后面可跟多个或者单个内容条)

  1. [root@localhost /]# grep -e root -e user /var/log/messages
  2. May 4 14:40:01 localhost systemd: Started Session 49 of user root.
  3. May 4 14:40:01 localhost systemd: Starting Session 49 of user root.
  4. May 4 14:50:01 localhost systemd: Started Session 50 of user root.
  5. May 4 14:50:01 localhost systemd: Starting Session 50 of user root.
  6. [root@localhost /]#

后面可跟 - e 多个条件进行筛选,满足一个即可筛选出相匹配的内容

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号