Linux学习Day3:新手必须掌握的Linux命令(二)
今天学习的命令都是运维工作中经常要用到的,非常实用,必须要用心学习,争取把这些命令烂熟于心,具体内容如下:
一、系统状态监测命令
1、ifconfig命令
用于获取网卡配置与网络状态等信息。
[root@linuxprobe Desktop]# ifconfig
eno16777728: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.134.128 netmask 255.255.255.0 broadcast 192.168.134.255
inet6 fe80::20c:29ff:feff:6b1f prefixlen scopeid 0x20<link>
ether :0c::ff:6b:1f txqueuelen (Ethernet)
RX packets bytes (3.5 KiB)
RX errors dropped overruns frame
TX packets bytes (7.0 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (560.0 B)
RX errors dropped overruns frame
TX packets bytes (560.0 B)
TX errors dropped overruns carrier collisions
eno16777728 : 网卡名称
inet 192.168.134.128 : IP地址
ether 00:0c:29:ff:6b:1f : MAC地址
RX packets 32 bytes 3653 (3.5 KiB) : 接收数据包的个数以及累计流量,重启系统则流量清零
TX packets 58 bytes 7267 (7.0 KiB) :发送数据包的个数以及累计流量,重启系统则流量清零
2、uname命令
用于查看系统内核与系统版本等信息,一般会固定搭配-a参数
[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 3.10.-.el7.x86_64 # SMP Mon May :: EDT x86_64 x86_64 x86_64 GNU/Linux
3、uptime命令
用于查看系统的负载信息,系统在1分钟、5分钟、15分钟内的平均负载值(加粗信息)尽量不要长期超过1,在生产环境中不要超过5。
[root@linuxprobe ~]# uptime
:: up min, users, load average: 0.08, 0.04, 0.05
4、free命令
用于显示当前系统中内存的使用量信息,一般结合-h参数使用增加可读性。
[root@linuxprobe ~]# free -h
total used free shared buffers cached
Mem: .9G 910M .1G 9.6M 924K 295M
-/+ buffers/cache: 614M .3G
Swap: .0G 0B .0G
5、who命令
用于查看当前登录主机的用户终端信息,下面输出结果显示当前打开一个终端。
[root@linuxprobe ~]# who
root : -- : (:)
root pts/ -- : (:)
6、last命令
用于查看所有本机的登录记录,要注意的是,登录记录可以被人为篡改。
[root@linuxprobe ~]# last
root pts/ : Mon Feb : still logged in
root : : Mon Feb : still logged in
(unknown : : Mon Feb : - : (:)
reboot system boot 3.10.-.el7.x Mon Feb : - : (:)
root pts/ 192.168.134.1 Sun Feb : - : (:)
root pts/ 192.168.134.1 Sun Feb : - down (:)
7、history命令
用于显示历史执行过的命令,还可以使用“!数字”的方式来重复执行某一次的命令。
[root@linuxprobe ~]# history
cat /etc/redhat-release
reboot
ls
passwd root
su - linuxprobe
shutdown
shutdown -r
8、sosreport命令
用于收集系统配置与架构信息并输出诊断文档。
[root@linuxprobe ~]# sosreport sosreport (version 3.0) This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications. An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative. Any information provided to Red Hat will be treated in accordance with
the published support policies at: https://access.redhat.com/support/ The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party. No changes will be made to system configuration. Press ENTER to continue, or CTRL-C to quit. //此处按下回车键来确认收集信息
Please enter your first initial and last name [linuxprobe.com]: //此处按下回车键来确认主机编号
Please enter the case number that you are generating this report for: //此处按下回车键
Running plugins. Please wait ...
Running 1/69: abrt...
Running 6/69: auditd...
Running 7/69: block...
Running 69/69: yum...
Creating compressed archive...
Your sosreport has been generated and saved in:
/var/tmp/sosreport-linuxprobe.com-20200217114206.tar.xz //生成的诊断文档保存的位置
The checksum is: fd53340877a57c34803cccbcc9d15676
Please send this file to your support representative.
二、工作目录切换命令
1、pwd命令
用于显示用户当前所在的工作目录。
[root@linuxprobe ~]# pwd
/root
2、cd命令
用于切换工作路径。“cd -”命令返回上一次所处的目录,“cd ..”命令返回上级目录,“cd ~”命令切换至当前用户的家目录。
[root@linuxprobe ~]# cd /etc
[root@linuxprobe etc]#
3、ls命令
用于显示目录中的文件信息,-a参数可以显示隐藏文件,-l参数可以显示文件的属性、大小等详细信息。
[root@linuxprobe ~]# ls -al
total
dr-xr-x---. root root Feb : .
drwxr-xr-x. root root Feb : ..
-rw-------. root root Feb : anaconda-ks.cfg
-rw-------. root root Feb : .bash_history
-rw-r--r--. root root Dec .bash_logout
-rw-r--r--. root root Dec .bash_profile
-rw-r--r--. root root Dec .bashrc
drwx------. root root Feb : .cache
drwx------. root root Feb : .config
-rw-r--r--. root root Dec .cshrc
drwx------. root root Feb : .dbus
drwxr-xr-x. root root Feb : Desktop
drwxr-xr-x. root root Feb : Documents
drwxr-xr-x. root root Feb : Downloads
-rw-------. root root Feb : .esd_auth
-rw-------. root root Feb : .ICEauthority
-rw-r--r--. root root Feb : initial-setup-ks.cfg
drwxr-xr-x. root root Feb : .local
drwxr-xr-x. root root Feb : Music
drwxr-xr-x. root root Feb : Pictures
drwxr-xr-x. root root Feb : Public
-rw-r--r--. root root Dec .tcshrc
drwxr-xr-x. root root Feb : Templates
drwxr-xr-x. root root Feb : Videos
-rw-------. root root Feb : .viminfo
-rw-------. root root Feb : .Xauthority
三、文本文件编辑命令
1、cat命令
用于查看纯文本文件(内容较少的),-n参数可以显示行号。
[root@linuxprobe ~]# cat -n initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig --startxonboot # License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
2、more命令
用于查看纯文本文件(内容较多的),可以使用空格健或回车健向下翻页。
[root@linuxprobe ~]# more initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig --startxonboot # License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
3、head命令
用于查看纯文本文档的前N行,一般配合-n参数使用,下面命令表示查看文档前10行。
[root@linuxprobe ~]# head -n initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig --startxonboot # License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
4、tail命令
用于查看纯文本文档的后N行或持续刷新内容,前者的命令形式为“tail -n 20 文件名”,后者为“tail -f 文件名”。
[root@linuxprobe ~]# tail -f /var/log/messages
Feb :: linuxprobe systemd: Starting Hostname Service...
Feb :: linuxprobe dbus-daemon: dbus[]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb :: linuxprobe dbus[]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb :: linuxprobe systemd: Started Hostname Service.
Feb :: linuxprobe chronyd[]: Selected source 162.159.200.123
Feb :: linuxprobe kernel: TCP: lp registered
Feb :: linuxprobe systemd-logind: Removed session c1.
Feb :: linuxprobe systemd: Starting Session of user root.
Feb :: linuxprobe systemd: Started Session of user root.
Feb :: linuxprobe systemd-logind: New session of user root.
Feb :: linuxprobe chronyd[]: Selected source 202.112.29.82
5、tr命令
用于替换文本中的字符,例如把文本中的英文全部替换为大写。
[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512 # USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
# SYSTEM LANGUAGE
LANG EN_US.UTF-
6、wc命令
用于统计指定文本的行数、字数、字节数,-l参数显示文本行数。
[root@linuxprobe ~]# wc -l /etc/passwd
/etc/passwd
7、stat命令
用于查看文件的具体存储信息和时间等信息。
[root@linuxprobe ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: Blocks: IO Block: regular file
Device: fd00h/64768d Inode: Links:
Access: (/-rw-------) Uid: ( / root) Gid: ( / root)
Context: system_u:object_r:admin_home_t:s0
Access: -- ::53.095941142 +0800 //最后一次查看文件内容的时间
Modify: -- ::56.710988547 +0800 //最后一次修改文件内容的时间
Change: -- ::56.710988547 +0800 //最后一次变更文件权限或属性的时间
Birth: -
8、cut命令
用于按“列”提取文本字符,-d参数设置间隔符号,-f参数设置提取第几列字符。下面命令表示以“:”作为分隔符,提取第一列数据。
[root@linuxprobe ~]# cut -d : -f /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
operator
games
ftp
9、diff命令
用于比较多个文本文件的差异,--brief参数仅返回比较结果,-c参数详细比较多个文件之间的差异之处。
[root@linuxprobe ~]# diff -c anaconda-ks.cfg initial-setup-ks.cfg
*** anaconda-ks.cfg -- ::56.710988547 +
--- initial-setup-ks.cfg -- ::03.533009017 +
***************
*** , ****
#version=RHEL7
# System authorization information
auth --enableshadow --passalgo=sha512
-
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
- ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
-------------------------省略部分输出结果---------------------------------
四、文件目录管理命令
1、touch命令
用于创建空白文件或设置文件的时间,可以设置文件的查看时间(atime)、权限或属性的变更时间(ctime)以及文件的修改时间(mtime),分别对应stat命令里的三个时间。-d参数表示同时修改atime和mtime。
[root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. root root Feb : anaconda-ks.cfg
[root@linuxprobe ~]#
[root@linuxprobe ~]# touch -d "2019-2-18 15:20" anaconda-ks.cfg //修改anaconda-ks.cfg文件的atime和mtime
[root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1032 Feb 18 2019 anaconda-ks.cfg //修改成功
2、mkdir命令
用于创建空白的目录,结合-p参数来创建具有嵌套层关系的文件目录。
[root@linuxprobe ~]# mkdir -p a/b/c/d
[root@linuxprobe ~]#
[root@linuxprobe ~]# cd a/ //进入a目录
[root@linuxprobe a]# cd b/ //进入b目录
[root@linuxprobe b]#
3、cp命令
用于复制文件和目录,格式为“cp 参数 源文件 目标文件”,复制操作分为4种情况:
- 若目标文件是目录,则把源文件复制到该目录中;
- 若目标文件也是普通文件,则会询问是否覆盖目标文件;
- 若目标文件不存在,则执行正常的复制操作;
- 若源文件也是目录,则需要加-r参数。
[root@linuxprobe ~]# cp anaconda-ks.cfg anaconda-ks.cfg_bak //备份anaconda-ks.cfg文件
[root@linuxprobe ~]# ls
a anaconda-ks.cfg_bak Documents initial-setup-ks.cfg Pictures Templates anaconda-ks.cfg Desktop Downloads Music Public Videos
[root@linuxprobe ~]#
4、mv命令
用于剪切文件或将文件重命名,在同一目录下对一个文件进行剪切操作,就是对其进行重命名。
[root@linuxprobe ~]# mv anaconda-ks.cfg_bak anaconda-ks.cfg_cpoy //将anaconda-ks.cfg_bak文件重命名为anaconda-ks.cfg_copy
[root@linuxprobe ~]# ls
a anaconda-ks.cfg_cpoy Documents initial-setup-ks.cfg Pictures Templates
anaconda-ks.cfg Desktop Downloads Music Public Videos
[root@linuxprobe ~]#
5、rm命令
用于删除文件或目录,-f参数表示强制删除,不提示确认信息,删除目录需要-r参数。
[root@linuxprobe ~]# rm -f anaconda-ks.cfg_cpoy //强制删除anaconda-ks.cfg_copy文件
[root@linuxprobe ~]#
[root@linuxprobe ~]# ls
a anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@linuxprobe ~]#
6、dd命令
用来按照指定大小和个数的数据块来复制文件或转换文件,相关参数及作用如下所示:
参数 | 作用 |
if | 输入的文件名称,即input file |
of | 输出的文件名称,即output file |
bs | 设置每个“块”的大小 |
count | 设置复制“块”的个数 |
[root@linuxprobe ~]# dd if=/dev/zero of=test_file count= bs=20M //在/dev/zero设备文件中取出一个大小为20M的数据块,然后保存命名为test_file的文件
+ records in
+ records out
bytes ( MB) copied, 0.0999151 s, MB/s
[root@linuxprobe ~]# ls
a anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test_file Videos
[root@linuxprobe ~]#
7、file命令
用于查看文件的类型。
[root@linuxprobe ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text //文本文件
[root@linuxprobe ~]#
[root@linuxprobe ~]# file Downloads
Downloads: directory //目录
[root@linuxprobe ~]#
五、打包压缩与搜索命令
1、tar命令
用于对文件进行打包压缩和解压,相关参数及其作用如下:
参数 | 作用 |
-c | 创建压缩文件 |
-x | 解开压缩文件 |
-z | 用Gzip压缩或解压 |
-j | 用bzip2压缩或解压 |
-v | 显示压缩或解压过程 |
-f | 目标文件名 |
-P | 保留原始的权限和属性 |
-C | 指定解压到的目录 |
[root@linuxprobe ~]# tar -czvf test.tar.gz /root //把/root目录通过Gzip格式进行打包压缩,压缩包命名为test.tar.gz
tar: Removing leading `/' from member names
/root/
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
-----------------省略部分压缩过程信息-----------------
[root@linuxprobe ~]# ls
a Desktop Downloads Music Public test_file Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.tar.gz
接下来将压缩文件解压到 /root/test 目录中,注意:无论压缩还是解压,-f参数都一定要放在参数的最后面。
[root@linuxprobe ~]# mkdir test //创建test目录
[root@linuxprobe ~]#
[root@linuxprobe ~]# tar -xzvf test.tar.gz -C /root/test //解压
root/
root/.bash_logout
root/.bash_profile
root/.bashrc
root/.cshrc
root/.tcshrc
root/anaconda-ks.cfg
2、grep命令
用于在文本中执行关键词搜索,并显示匹配结果,格式为“grep 参数 关键词 文件名”,相关参数及其作用如下所示:
参数 | 作用 |
-b | 将可执行文件(binary)当作文本文件(text)来搜索 |
-c | 仅显示找到的行数 |
-i | 忽略大小写 |
-n | 显示行号 |
-v | 反向选择——仅列出没有“关键词”的行 |
[root@linuxprobe root]# grep root /etc/passwd //搜索/etc/passwd文件中,显示包含“root”关键词的行
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
3、find命令
用来按照指定的条件来查找文件,格式为“find [查找路径] 寻找条件 操作”。相关参数及其作用如下所示:
参数 | 作用 |
-name | 匹配名称 |
-user | 匹配所有者 |
-group | 匹配所有组 |
--type b/d/c/p/l/f |
匹配文件类型(后面字母参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件) |
-size | 匹配文件大小(+50KB表示查找超过50KB的文件,-50KB表示查找小于50KB的文件) |
-exec .... {}\; | 后面可以跟用于进一步处理搜索结果的命令 |
[root@linuxprobe root]# find /root -name test* //在/root目录下查找文件名以test开头的文件
/root/test_file
/root/test/root/test_file
[root@linuxprobe root]#
find命令进阶实验:在整个文件系统中找出所有归属于linuxprobe用户的文件并复制到/root/findresults目录。
[root@linuxprobe ~]# find / -user linuxprobe -exec cp -a {} /root/findresults/ \; //其中{}代表find命令搜索出的每一个文件,即假设find命令第一次搜索出了test.txt文件,则执行cp -a test.txt /root/findresults/
find: ‘/proc//task//fd/’: No such file or directory
find: ‘/proc//task//fdinfo/’: No such file or directory
find: ‘/proc//fd/’: No such file or directory
find: ‘/proc//fdinfo/’: No such file or directory
cp: cannot overwrite non-directory ‘/root/findresults/linuxprobe’ with directory ‘/home/linuxprobe’
[root@linuxprobe ~]#
[root@linuxprobe findresults]# ls -l //查看命令执行结果
total
srwxrwxrwx. linuxprobe linuxprobe Feb :
srwxrwxrwx. linuxprobe linuxprobe Feb :
srwxrwxrwx. linuxprobe linuxprobe Feb :
srwxrwxrwx. linuxprobe linuxprobe Feb :
srwxrwxrwx. linuxprobe linuxprobe Feb :
srwxrwxrwx. linuxprobe linuxprobe Feb :
-rw-r--r--. linuxprobe linuxprobe Feb : 6c80566e9d01473b8952f753857b506f-card-database.tdb
-rw-r--r--. linuxprobe linuxprobe Feb : 6c80566e9d01473b8952f753857b506f-default-sink
----------------------省略部分输出内容--------------------------
Linux学习Day3:新手必须掌握的Linux命令(二)的更多相关文章
- Linux 学习笔记之超详细基础linux命令(the end)
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 14
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 13
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 12
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 11
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 10
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 9----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 9
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 8
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 7----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 7
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 6
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 5----------------- ...
随机推荐
- 玩转Django2.0---Django笔记建站基础四(视图)
第四章 视图 4.1 探究视图 一.视图说明 视图(View)是Django的MTV架构模式的V部分,主要负责处理用户请求和生成相应的相应部分,然后在页面或其它类型文档中显示.也可以理解为视图是MVC ...
- UVA 最大面积最小三角形剖分
点击打开题目 题目大意: 以顺时针或逆时针给出一个简单多边形的n个点的坐标,用n-2条互不相交的,且与边不相交的对角线,分成n-2个三角形,要求其中最大三角形的面积最小 开始还汪星人咬乌龟,无从下口, ...
- GP工作室—Alpha版本发布1
目录 GP工作室-Alpha版本发布1 一.简介 1.1作业要求 1.2团队成员 二.软件下载安装说明 五.项目总结 @(Gold Point团队の项目计划) GP工作室-Alpha版本发布1 一.简 ...
- C#异常处理总结
Exception类分析 常见的异常类 异常捕获 异常处理原则和建议 SystemException类继承Exception,前者是System命名空间中所有其他异常类的基类,在捕获异常的时候,我首先 ...
- python输出结果后面带着一个“None”
执行代码的时候,输出的结果中含有一个“None” 只要将最后一行代码的print() 去掉即可 改成 my_range.get_range() 或者是将 函数中的print(range) 改成 r ...
- 基于playcanvas的3d模型展示
1.使用基于playcanvas的离线编辑器制作模型效果 2.使用基于playcanvas的开发包读取编辑好的3d模型进行在线3d展示 效果如下:
- servlet 深入了解
servlet 作用 在Java web b/s架构中,servlet扮演了重要的角色,作为一个中转处理的容器,他连接了客户端和服务器端的信息交互和处理.简单来说,客户端发送请求,传递到servle ...
- Spring相关jar说明
Spring整合使用说明 一.只是使用spring框架 dist\spring.jar lib\jakarta-commons\commons-logging.jar 如果使用到了切面编程(AOP), ...
- 06讲案例篇:系统的CPU使用率很高,但为啥却找不到高CPU的应用
小结 碰到常规问题无法解释的 CPU 使用率情况时,首先要想到有可能是短时应用导致的问题,比如有可能是下面这两种情况. 第一,应用里直接调用了其他二进制程序,这些程序通常运行时间比较短,通过 top ...
- c/c++判断文件是否存在
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <fstream> #include <cstd ...