赞
踩
目录
grep -e 条件满足其中一个,都可过滤内容显示出来(后面可跟多个或者单个内容条)
Linux是一套免费使用和自由传播的类Unix操作系统,是一个多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。
在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:移动文件或者目录 如果是不存在的目的目录则会变成重命名功能
别名系统就是相当于给一个复杂的命令创建一个快捷方式命令,相当于简化命令,但是重启虚拟机后除默认别名系统的命令,其他设置的会消失,设置的快捷命令也要符合合法字符,即是数字或者字母字符串,用户家目录下的Bashrc修改只对当前用户生效,在Source.bashrc中修改并保存
格式:alias ‘源命令’ 别名的命令名称
在这里别名设置的快捷命令,不可用已有的命令,不然会发生命令上的冲突
命令的执行优先级:绝对路径的命令 > 别名系统 > 内部命令 > Hash缓存命令 > 外部命令
直接输入alias可以查询目前已设置过的别名系统的命令
遇到不熟悉的问题报错时,需要一步步去排错,要在日志和配置文件内容中进行查看排错,在Linux中大量的文件内容,占用巨大内存的文件许许多多,尤其是在日常生活工作学习当中,在那么多的文件中,怎样筛选出我们需要去修改,删除,移动,查看的文件,就要用到许许多多基础的筛选查看修改的命令。
(过大的文件会造成虚机卡顿)
命令:cat 查看文件内容
cat -n 对内容增添行号进行查看
cat -b 对空白行不进行编辑行号查看
- [root@localhost /]# cat /var/log/boot.log
- [ OK ] Started Show Plymouth Boot Screen.
- [ OK ] Reached target Paths.
- [ OK ] Reached target Basic System.
- [ OK ] Found device /dev/mapper/centos-root.
- Starting File System Check on /dev/mapper/centos-root...
- [ OK ] Started File System Check on /dev/mapper/centos-root.
- [ OK ] Started dracut initqueue hook.
- Mounting /sysroot...
- [ OK ] Reached target Remote File Systems (Pre).
- [ OK ] Reached target Remote File Systems.
- [ OK ] Mounted /sysroot.
- [ OK ] Reached target Initrd Root File System.
- Starting Reload Configuration from the Real Root...
- [ OK ] Started Reload Configuration from the Real Root.
- [ OK ] Reached target Initrd File Systems.
- [ OK ] Reached target Initrd Default Target.
- Starting dracut pre-pivot and cleanup hook...
- [ OK ] Started dracut pre-pivot and cleanup hook.
- Starting Cleaning Up and Shutting Down Daemons...
- Starting Plymouth switch root service...
- [ OK ] Stopped target Timers.
- [ OK ] Stopped Cleaning Up and Shutting Down Daemons.
- [ OK ] Stopped dracut pre-pivot and cleanup hook.
- Stopping dracut pre-pivot and cleanup hook...
- [ OK ] Stopped target Remote File Systems.
- [ OK ] Stopped target Remote File Systems (Pre).
- [ OK ] Stopped dracut initqueue hook.
- Stopping dracut initqueue hook...
- [ OK ] Stopped target Initrd Default Target.
- [ OK ] Stopped target Basic System.
- [ OK ] Stopped target Slices.
- [ OK ] Stopped target Paths.
- [ OK ] Stopped target System Initialization.
- Stopping udev Kernel Device Manager...
- [ OK ] Stopped udev Coldplug all Devices.
- Stopping udev Coldplug all Devices...
- [ OK ] Stopped target Local File Systems.
- [ OK ] Stopped target Swap.
- [ OK ] Stopped Apply Kernel Variables.
- Stopping Apply Kernel Variables...
- [ OK ] Stopped target Sockets.
- [ OK ] Stopped udev Kernel Device Manager.
- [ OK ] Stopped Create Static Device Nodes in /dev.
- Stopping Create Static Device Nodes in /dev...
- [ OK ] Stopped Create list of required static device nodes for the current kernel.
- Stopping Create list of required static device nodes for the current kernel...
- [ OK ] Stopped dracut pre-udev hook.
- Stopping dracut pre-udev hook...
- [ OK ] Stopped dracut cmdline hook.
- Stopping dracut cmdline hook...
- [ OK ] Closed udev Control Socket.
- [ OK ] Closed udev Kernel Socket.
- Starting Cleanup udevd DB...
- [ OK ] Started Cleanup udevd DB.
- [ OK ] Reached target Switch Root.
- [ OK ] Started Plymouth switch root service.
- Starting Switch Root...
-
- Welcome to CentOS Linux 7 (Core)!
-
- [ OK ] Stopped Switch Root.
- [ OK ] Stopped Journal Service.
- Starting Journal Service...
- [ OK ] Created slice system-getty.slice.
- [ OK ] Stopped target Switch Root.
- [ OK ] Created slice system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice.
- Mounting Huge Pages File System...
- [ OK ] Listening on LVM2 poll daemon socket.
- Mounting POSIX Message Queue File System...
- [ OK ] Stopped target Initrd Root File System.
- Mounting Debug File System...
- [ OK ] Listening on udev Control Socket.
- [ OK ] Listening on Device-mapper event daemon FIFOs.
- [ OK ] Reached target Login Prompts.
- [ OK ] Listening on LVM2 metadata daemon socket.
- Starting Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling...
- Starting Create list of required static device nodes for the current kernel...
- [ OK ] Created slice User and Session Slice.
- [ OK ] Reached target Slices.
- [ OK ] Listening on udev Kernel Socket.
- [ OK ] Listening on /dev/initctl Compatibility Named Pipe.
- [ OK ] Stopped target Initrd File Systems.
- [ OK ] Stopped File System Check on Root Device.
- Stopping File System Check on Root Device...
- Starting Apply Kernel Variables...
- [ OK ] Listening on Delayed Shutdown Socket.
- [ OK ] Reached target User and Group Name Lookups.
- Starting Remount Root and Kernel File Systems...
- Mounting NFSD configuration filesystem...
- [ OK ] Set up automount Arbitrary Executable File Formats File System Automount Point.
- [ OK ] Mounted Debug File System.
- [ OK ] Mounted Huge Pages File System.
- [ OK ] Mounted POSIX Message Queue File System.
- [ OK ] Started Create list of required static device nodes for the current kernel.
- [ OK ] Started Apply Kernel Variables.
- [ OK ] Started Remount Root and Kernel File Systems.
- [ OK ] Started Journal Service.
- [ OK ] Started LVM2 metadata daemon.
- Starting LVM2 metadata daemon...
- Starting Load/Save Random Seed...
- Starting Flush Journal to Persistent Storage...
- Starting Configure read-only root support...
- Starting Rebuild Hardware Database...
- Starting Create Static Device Nodes in /dev...
- [ OK ] Started Load/Save Random Seed.
- [ OK ] Started Flush Journal to Persistent Storage.
- [ OK ] Started Create Static Device Nodes in /dev.
- Starting udev Kernel Device Manager...
- [ OK ] Started Configure read-only root support.
- [ OK ] Mounted NFSD configuration filesystem.
- [ OK ] Started udev Kernel Device Manager.
- [ OK ] Created slice system-systemd\x2drfkill.slice.
- Starting Load/Save RF Kill Switch Status of rfkill0...
- [ OK ] Started Load/Save RF Kill Switch Status of rfkill0.
- [ OK ] Started Rebuild Hardware Database.
- Starting udev Coldplug all Devices...
- [ OK ] Started udev Coldplug all Devices.
- Starting udev Wait for Complete Device Initialization...
- [ OK ] Found device VMware_Virtual_S 1.
- [ OK ] Created slice system-lvm2\x2dpvscan.slice.
- Starting LVM2 PV scan on device 8:2...
- [ OK ] Started Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling.
- [ OK ] Reached target Local File Systems (Pre).
- Mounting /boot...
- [ OK ] Found device /dev/mapper/centos-swap.
- Activating swap /dev/mapper/centos-swap...
- [ OK ] Started LVM2 PV scan on device 8:2.
- [ OK ] Activated swap /dev/mapper/centos-swap.
- [ OK ] Reached target Swap.
- [ OK ] Mounted /boot.
- [ OK ] Started udev Wait for Complete Device Initialization.
- Starting Activation of DM RAID sets...
- [ OK ] Started Activation of DM RAID sets.
- [ OK ] Reached target Local File Systems.
- Starting Migrate local SELinux policy changes from the old store structure to the new structure...
- Starting Rebuild Journal Catalog...
- Starting Import network configuration from initramfs...
- Starting Preprocess NFS configuration...
- Starting Tell Plymouth To Write Out Runtime Data...
- [ OK ] Reached target Encrypted Volumes.
- [ OK ] Started Rebuild Journal Catalog.
- Starting Update is Completed...
- [ OK ] Started Tell Plymouth To Write Out Runtime Data.
- [ OK ] Started Preprocess NFS configuration.
- [ OK ] Started Update is Completed.
- [ OK ] Started Migrate local SELinux policy changes from the old store structure to the new structure.
- [ OK ] Started Import network configuration from initramfs.
- Starting Create Volatile Files and Directories...
- [ OK ] Started Create Volatile Files and Directories.
- Mounting RPC Pipe File System...
- Starting Security Auditing Service...
- [ OK ] Mounted RPC Pipe File System.
- [ OK ] Started Security Auditing Service.
- Starting Update UTMP about System Boot/Shutdown...
- [ OK ] Started Update UTMP about System Boot/Shutdown.
- [ OK ] Reached target System Initialization.
- [ OK ] Listening on Avahi mDNS/DNS-SD Stack Activation Socket.
- [ OK ] Listening on CUPS Printing Service Sockets.
- [ OK ] Listening on RPCbind Server Activation Socket.
- [ OK ] Listening on Virtual machine lock manager socket.
- [ OK ] Listening on Open-iSCSI iscsid Socket.
- [ OK ] Listening on Open-iSCSI iscsiuio Socket.
- [ OK ] Reached target Timers.
- [ OK ] Listening on Virtual machine log manager socket.
- [ OK ] Listening on D-Bus System Message Bus Socket.
- [ OK ] Reached target Sockets.
- [ OK ] Reached target Paths.
- [ OK ] Reached target Basic System.
- [ OK ] Started VGAuth Service for open-vm-tools.
- Starting VGAuth Service for open-vm-tools...
- Starting Dump dmesg to /var/log/dmesg...
- [ OK ] Started ABRT Automated Bug Reporting Tool.
- Starting ABRT Automated Bug Reporting Tool...
- [ OK ] Started Self Monitoring and Reporting Technology (SMART) Daemon.
- Starting Self Monitoring and Reporting Technology (SMART) Daemon...
- Starting GSSAPI Proxy Daemon...
- [ OK ] Started libstoragemgmt plug-in server daemon.
- Starting libstoragemgmt plug-in server daemon...
- Starting OpenSSH Server Key Generation...
- Starting NTP client/server...
- [ OK ] Started D-Bus System Message Bus.
- Starting D-Bus System Message Bus...
- Starting Authorization Manager...
- Starting Modem Manager...
- Starting Install ABRT coredump hook...
- Starting Avahi mDNS/DNS-SD Stack...
- [ OK ] Started Manage Sound Card State (restore and store).
- Starting Manage Sound Card State (restore and store)...
- Starting Kernel Samepage Merging...
- Starting Resets System Activity Logs...
- Starting Bluetooth service...
- Starting Login Service...
- Starting RealtimeKit Scheduling Policy Service...
- [ OK ] Started irqbalance daemon.
- Starting irqbalance daemon...
- [ OK ] Started Hardware RNG Entropy Gatherer Daemon.
- Starting Hardware RNG Entropy Gatherer Daemon...
- Starting Initial Setup configuration program graphical interface...
- [ OK ] Started Service for virtual machines hosted on VMware.
- Starting Service for virtual machines hosted on VMware...
- Starting Accounts Service...
- Starting System Logging Service...
- [ OK ] Started ABRT kernel log watcher.
- Starting ABRT kernel log watcher...
- [ OK ] Started Dump dmesg to /var/log/dmesg.
- [ OK ] Started GSSAPI Proxy Daemon.
- [ OK ] Started Kernel Samepage Merging.
- [ OK ] Started Resets System Activity Logs.
- [ OK ] Started Initial Setup configuration program graphical interface.
- [ OK ] Started System Logging Service.
- [ OK ] Started NTP client/server.
- [ OK ] Started Avahi mDNS/DNS-SD Stack.
- [ OK ] Started RealtimeKit Scheduling Policy Service.
- [ OK ] Started Bluetooth service.
- [ OK ] Started Install ABRT coredump hook.
- [ OK ] Started Login Service.
- [ OK ] Started Modem Manager.
- Starting Hostname Service...
- [ OK ] Reached target Bluetooth.
- Starting Initial Setup configuration program...
- Starting Kernel Samepage Merging (KSM) Tuning Daemon...
- [ OK ] Reached target NFS client services.
- [ OK ] Started Authorization Manager.
- [ OK ] Started Accounts Service.
- Starting firewalld - dynamic firewall daemon...
查看目标文件的内容(不可以进行修改,只能读)
命令:more /绝对路径
回车可以一行一行往下查看,空格则是查看下面整页内容
当看完文件内容后,会动退出文件,而且不可搜索指定文件内容
- [root@localhost /]# more /var/log/boot.log
- [ OK ] Started Show Plymouth Boot Screen.
- [ OK ] Reached target Paths.
- [ OK ] Reached target Basic System.
- [ OK ] Found device /dev/mapper/centos-root.
- Starting File System Check on /dev/mapper/centos-root...
- [ OK ] Started File System Check on /dev/mapper/centos-root.
- [ OK ] Started dracut initqueue hook.
- Mounting /sysroot...
- [ OK ] Reached target Remote File Systems (Pre).
- [ OK ] Reached target Remote File Systems.
- [ OK ] Mounted /sysroot.
- [ OK ] Reached target Initrd Root File System.
- Starting Reload Configuration from the Real Root...
- [ OK ] Started Reload Configuration from the Real Root.
- [ OK ] Reached target Initrd File Systems.
- [ OK ] Reached target Initrd Default Target.
- Starting dracut pre-pivot and cleanup hook...
- [ OK ] Started dracut pre-pivot and cleanup hook.
- Starting Cleaning Up and Shutting Down Daemons...
- Starting Plymouth switch root service...
- [ OK ] Stopped target Timers.
- [ OK ] Stopped Cleaning Up and Shutting Down Daemons.
- [ OK ] Stopped dracut pre-pivot and cleanup hook.
- Stopping dracut pre-pivot and cleanup hook...
- --More--(10%)
进行分页查看,下面More的10%就是已经显示的内容占总内容的多少(显示进度)
命令:less /绝对路径
也可以进行分页查看,区别在于看完文件内容后不会自动退出,需要按q手动退出,浏览完后也可返回翻看,同时可以搜索指定内容。
- 总用量 16
- drwxr-xr-x. 14 root root 177 4月 25 20:09 ./
- drwxr-xr-x. 21 root root 4096 4月 25 20:09 ../
- drwxrwxr-x. 2 abrt abrt 6 8月 10 2017 abrt-di/
- drwxrwx---. 3 root lp 17 4月 25 20:09 cups/
- drwxr-xr-x. 2 root root 4096 4月 25 20:05 fontconfig/
- drwxr-xr-t. 2 root gdm 6 8月 7 2017 gdm/
- drwxr-xr-x. 3 root root 17 4月 25 20:04 ibus/
- drwxr-xr-x. 2 root root 6 8月 8 2017 krb5rcache/
- drwx------. 2 root root 23 4月 25 20:05 ldconfig/
- drwx--x--x. 3 root root 18 4月 25 20:04 libvirt/
- drwxr-xr-x. 35 root root 4096 4月 26 10:33 man/
- drwxr-xr-x. 4 root root 60 4月 25 20:11 PackageKit/
- drwxr-xr-x. 2 root root 6 11月 12 2016 realmd/
- drwxr-xr-x. 3 root root 20 4月 25 20:11 yum/
- :
可以进行内容搜索(:/搜索的内容),会显示出相对匹配的内容
命令:tail -f /绝对路径
从文件内容底部开始向上查看,不指定行数的情况下默认显示十行内容
实时显示可以随时查看错误,定位操作问题,每进行一步操作下面则会显示操作的步骤日志
拓展命令
命令:tail -n /绝对路径 展示指定的固定行数内容(不实时显示)
输入 tail -n 5 /var/log/messages 后面展示了五行内容
- [root@localhost /]# tail -n 5 /var/log/messages
- May 4 14:10:01 localhost systemd: Starting Session 46 of user root.
- May 4 14:20:01 localhost systemd: Started Session 47 of user root.
- May 4 14:20:01 localhost systemd: Starting Session 47 of user root.
- May 4 14:30:01 localhost systemd: Started Session 48 of user root.
- May 4 14:30:01 localhost systemd: Starting Session 48 of user root.
- [root@localhost /]#
拓展命令
命令 :tail -fn 数字 /绝对路径 动态查看指定固定数量的内容
- [root@localhost /]# tail -fn 5 /var/log/messages
- May 4 14:10:01 localhost systemd: Starting Session 46 of user root.
- May 4 14:20:01 localhost systemd: Started Session 47 of user root.
- May 4 14:20:01 localhost systemd: Starting Session 47 of user root.
- May 4 14:30:01 localhost systemd: Started Session 48 of user root.
- May 4 14:30:01 localhost systemd: Starting Session 48 of user root.
- [root@localhost /]#
命令:head
从顶部往下查看文件内容,不指定数量的情况下也是默认十行
head 因为从顶部开始向下显示,所以不存在实时显示,因为新操作的日志都是在下面更新新的日志,顶部不会更新日志
- [root@localhost /]# head /var/log/messages
- 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).
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuset
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpu
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuacct
- 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
- 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
- Apr 25 20:09:19 localhost kernel: e820: BIOS-provided physical RAM map:
- Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
- Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
- Apr 25 20:09:19 localhost kernel: BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
- [root@localhost /]#
拓展命令:head -n /绝对路径
从顶部开始固定显示指定行数数量的文件内容
- [root@localhost /]# head -n 5 /var/log/messages
- 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).
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuset
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpu
- Apr 25 20:09:19 localhost kernel: Initializing cgroup subsys cpuacct
- 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
- [root@localhost /]#
这个指令和less的用法一样,操作顺序也一样,区别在于head顶部开始显示,并没有实时显示的功能,不能用来排错和定位操作错误。
平时所用到的以上排查文件命令并不能更改修改文件内容
仅仅用于排查错误操作和错误定位
cat 、more 、tail 、less 主要用于生活工作中排查错误日志文件与配置文件
命令:grep
针对行进行筛选,针对行进行内容显示处理,用于查找文件内容,字符内容,并不是查找文件名
- [root@localhost /]# grep root /var/log/messages
- May 4 14:40:01 localhost systemd: Started Session 49 of user root.
- May 4 14:40:01 localhost systemd: Starting Session 49 of user root.
- [root@localhost /]#
筛选含有root内容行,并显示出来
拓展命令:
- [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.
- [root@localhost /]#
每行前面标注出了在第几行
- [root@localhost /]# grep -c root /var/log/messages
- 2
- [root@localhost /]#
grep -c并不显示内容,只显示所匹配内容所在多少行的总数量
- [root@localhost /]# grep -v root /var/log/messages
- 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
- [root@localhost /]#
反选指定筛选内容的其他内容,除指定内容之外
- [root@localhost /]# grep -e root -e user /var/log/messages
- May 4 14:40:01 localhost systemd: Started Session 49 of user root.
- May 4 14:40:01 localhost systemd: Starting Session 49 of user root.
- May 4 14:50:01 localhost systemd: Started Session 50 of user root.
- May 4 14:50:01 localhost systemd: Starting Session 50 of user root.
- [root@localhost /]#
后面可跟 - e 多个条件进行筛选,满足一个即可筛选出相匹配的内容
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。