linux-基础学习-03 linux必知必会的命令
获取命令帮助
date --help //date查看系统时间,加了--help以看后面可以跟什么参数等
date -h
man date
echo
echo命令用于在终端输出字符串或变量提取后的值,格式为“echo [字符串 | $变量]”。
[root@localhost ~]# echo helloworld
helloworld
[root@localhost ~]# echo "helloworld"
helloworld
[root@localhost ~]# i="helloworld"
[root@localhost ~]# echo $i
helloworld
date
date查看时间以及设置时间相关命令,-s参数表示set设置
[root@localhost ~]# date
Wed Jan 16 07:48:36 EST 2019
[root@localhost ~]# date -s "19961001 1:01:00"
Tue Oct 1 01:01:00 EDT 1996
ntpdate
将系统的当前时间同步回网络时间
[root@localhost ~]# ntpdate -u ntp.api.bz
16 Jan 07:54:58 ntpdate[1494]: step time server 47.96.136.13 offset 703496889.865151 sec
[root@localhost ~]# date
Wed Jan 16 07:55:09 EST 2019
wget (Linux上的下载命令)
yum -y install wget
wget 链接地址
ps(静态任务管理器)
用于查看系统中的进程状态,命令格式为:
ps [参数]
常用ps命令组合:
ps -aux
-a:显示所有进程
-u:用户以及其他详细信息
-x:显示没有控制终端的进程
备注: 状态栏中,R表示正在运行的进程,S表示中断的进程,D表示不可终端的进程,Z表示僵尸进程,T表示停止的进程
TOP命令(动态任务管理器)
TOP命令用于动态地监视进程活动与系统负载等信息,其格式为top。能够动态地查看系统运维状态,完全将它看作Linux中的“强化版的Windows任务管理器”。
第1行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为1分钟、5分钟、15分钟内的平均值,数值越小意味着负载越低)。
第2行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数。
第3行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等。
第4行:物理内存总量、内存使用量、内存空闲量、作为内核缓存的内存量。
第5行:虚拟内存总量、虚拟内存使用量、虚拟内存空闲量、已被提前加载的内存量。
kill命令:
结束后台进程为1的进程
[root@localhost ~]# kill %[后台进程号]
结束进程pid为1234的进程
[root@localhost ~]# kill 1234
结束进程pid为1234的进程树
[root@localhost ~]# kill -9 1234
pkill
格式:pkill [进程名或者进程名的一部分连续字符串]
[root@localhost ~]# pkill ss
系统状态检测命令
查看和IP地址
ifconfig
临时修改IP地址
ifconfig ens33 192.168.9.101 netmask 255.255.255.0
free
查看内存相关信息
[root@localhost ~]# free
[root@localhost ~]# free -h
who
查看当前登入主机的用户终端信息:
[root@localhost ~]# who [参数]
last:
用于查看所有系统的登录记录:
[root@localhost ~]# last [参数]
history
用于显示历史执行过的命令,最多一千条:
[root@localhost ~]# history [-c]
sosreport
用于收集系统配置及架构信息并输出诊断文档:
[root@localhost ~]# yum install sos -y
[root@localhost ~]# sosreport
cd
用于切换目录
pwd
用于显示当前所在目录
#切换到根目录*
[root@localhost etc]# cd /
[root@localhost /]# pwd
#切换到/etc/yum.repos.d目录*
[root@localhost /]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# pwd
/etc/yum.repos.d
#切换到当前目录的上一级目录*
[root@localhost yum.repos.d]# cd .. *#“.”代表当前目录,两个“.”代表上一级目录*
[root@localhost etc]# pwd
/etc
#切换到家目录*
[root@localhost etc]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd ~
[root@localhost ~]# pwd
/root
*#回到切换目录之前所在的目录*
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /var/tmp/
[root@localhost tmp]# cd -
/root
[root@localhost ~]# pwd
/root
ls命令
ls命令用于显示目录中的文件信息(Linux中一切皆文件),格式为“ls [选项] [文件] ”。在CentOS系列系统中,ls -l 默认别名有ll
查看目录下文件的详细信息
[root@localhost ~]# ls -l
查看目录下的隐藏文件
[root@localhost ~]# ls -a
只查看目录本身的信息,不显示目录中的文件
[root@localhost ~]# cd /
[root@localhost /]# ls -ld /root/
tail命令
tail命令用于查看纯文本文档的后N行或持续刷新内容,格式为“tail [选项] [文件]”。
动态追踪文本更新
tail -f
或者
tailf
[root@localhost ~]# tailf /var/log/messages
cat
文本输出命令,通常是用于观看某个文件的内容的
[root@localhost ~]# cat anaconda-ks.cfg
tr
tr命令用于替换文本文件中的字符,格式为“tr [原始字符] [目标字符]”。
[root@localhost ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
stat
查看文件或者文件系统的状态
touch
touch命令用于创建空白文件或设置文件的时间,格式为“touch [选项] [文件]”
创建文件filea,查看该文件的状态
[root@localhost ~]# touch filea
[root@localhost ~]# stat filea
修改filea的最近访问和最近改动的时间
[root@localhost ~]# touch -d "19990101 01:01:01" filea
[root@localhost ~]# stat filea
wc命令
wc命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本”
统计系统中一共有多少个用户
[root@localhost ~]# wc -l /etc/passwd
统计/etc/yum.repos.d/CentOS-Base.repo文件一共有多少字数
[root@localhost ~]# wc -c /etc/yum.repos.d/CentOS-Base.repo
统计/etc/yum.repos.d/CentOS-Base.repo文件一共有多少个单词
[root@localhost ~]# wc -w /etc/yum.repos.d/CentOS-Base.repo
mkdir
用于创建目录
相对路径:
相对于当前目录的路径
绝对路径:
以/开头的路径(相对于根目录的路径)
在/root目录下新建目录aaa
[root@localhost ~]# mkdir aaa
在/root目录下新建目录bbb(当你还在另一个目录时,利用…/命令切换式创建)
[root@localhost ~]# cd /var/tmp
[root@localhost tmp]# mkdir ../../root/bbb
在/root目录下新建目录ccc(当你还在另一个目录时,利用绝对路径创建)
[root@localhost tmp]# mkdir /root/ccc
递归创建目录
[root@localhost tmp]# cd /root
[root@localhost ~]# mkdir -p ddd/eee
cp
用于复制文件或目录
格式为“cp [选项] 源文件 目标文件”
复制操作具体分为3种情况:
如果目标文件是目录,则会把源文件复制到该目录中;
如果目标文件也是普通文件,则会询问是否要覆盖它;
如果目标文件不存在,则执行正常的复制操作。
将家目录(/root目录)下的hello.c文件复制到/var/tmp目录下
[root@localhost ~]# touch hello.c
[root@localhost ~]# cp hello.c /var/tmp
cp命令的参数及其作用
参数 | 作用 |
---|---|
-p | 保留原始文件的属性aa* |
-d | 若对象为“链接文件”,则保留该“链接文件”的属性 |
-r | 递归持续复制(用于目录)* |
-i | 若目标文件存在则询问是否覆盖 |
-a | 相当于-pdr(p、d、r为上述参数)*** |
mv
用于剪切文件或将文件重命名
格式为“mv [选项] 源文件 [目标路径|目标文件名]”
将hello.c重命名为 helloworld.c
[root@localhost ~]# mv hello.c helloworld.c
将 helloworld.c剪切到/var/tmp目录下
[root@localhost ~]# mv helloworld.c /var/tmp
rm
用于删除文件或目录
格式为“rm [选项] 文件”
删除/var/tmp的hello.c文件(需要确认,敲y加回车)
[root@localhost ~]# cd /var/tmp
[root@localhost tmp]# rm hello.c
rm: remove regular empty file ‘hello.c’? y
[root@localhost tmp]#
不用确认(敲y加回车)直接删除helloworld.c
[root@localhost tmp]# rm -f helloworld.c
删除家目录(/root目录)下的aaa目录(需要确认,敲y加回车)
[root@localhost tmp]# cd -
[root@localhost ~]# rm -r aaa
rm: remove directory ‘aaa’? y
[root@localhost ~]#
删除家目录(/root目录)下的bbb目录(不需要确认,不用敲y加回车)
[root@localhost ~]# rm -rf bbb
dd
用于按照指定大小和个数的数据块来复制文件或转换文件
格式为“dd [参数]”
生成一个大小为128M的空文件
dd if=/dev/zero of=128M_file count=1M bs=128
可以参考以下教程:
http://www.runoob.com/linux/linux-comm-dd.html
将光盘中的文件制作成为光盘镜像(.ISO)
1.插入光盘到Linux系统中
2.将光盘/dev/cdrom挂载到指定的目录,检查光盘是否能读取
[root@localhost ~]# mkdir /mnt/cdrom
[root@localhost ~]# ls /mnt/cdrom
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# ls /mnt/cdrom
3.卸载已经挂载的目录,将光盘中的内容制作成虚拟光盘(ISO镜像文件)
[root@localhost ~]# umount /mnt/cdrom
[root@localhost ~]# ls /mnt/cdrom
[root@localhost ~]# cd ~
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# dd if=/dev/cdrom of=/root/CentOS.iso
[root@localhost ~]# ls
anaconda-ks.cfg CentOS.iso
[root@localhost ~]#
如下图,看到了CentOS.iso文件,这就是制作出来的光盘镜像
意外情况:
如果你到挂在目录下卸载绝对会卸载失败,因为你在他的目录下占用着,卸载肯定失败!
[root@localhost ~]# cd /mnt/cdrom/
[root@localhost cdrom]# umount /mnt/cdrom/
umount: /mnt/cdrom: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
File
用于查看文件的类型
格式为“file 文件名”
[root@localhost ~]#
[root@localhost ~]# file CentOS.iso
CentOS.iso: # ISO 9660 CD-ROM filesystem data 'CentOS 7 x86_64' (bootable)
[root@localhost ~]# file /dev/cdrom
/dev/cdrom: symbolic link to `sr0'
[root@localhost ~]# file /dev/sr0
/dev/sr0: block special
[root@localhost ~]#
tar
用于打包压缩与搜索
格式为“tar [选项] [文件]”
将/root目录打包为root.tar
[root@localhost ~]# cd ..
[root@localhost /]# tar -cvf root.tar /root/
[root@localhost /]# ls
将/root目录打包并且使用gzip压缩算法打包到文件/root.tar.gz中
[root@localhost /]# tar -czvf root.tar.gz /root
[root@localhost /]# ls
加压缩算法和不加压缩算法对同一个目录打包的文件大小进行对比
[root@localhost /]# ll -h root.tar
[root@localhost /]# ll -h root.tar.gz
可以看出加了压缩算法后,压出来的包明显更小更节省空间!
.tar文件的解压命令
[root@localhost /]# rm -rf /root/*
[root@localhost /]# tar -xvf root.tar
.tar.gz文件的解压命令
[root@localhost /]# rm -rf /root/*
[root@localhost /]# ls /root
[root@localhost /]# tar -xzvf root.tar.gz
[root@localhost /]# ls /root
aaa.txt anaconda-ks.cfg text
grep
用于在文本中执行关键词搜索,并显示匹配的结果
格式为“grep [选项] [文件]”
匹配所有不能登录系统的用户
[root@localhost ~]# grep nologin /etc/passwd
匹配出所有没有被注释的行,且不是空行的行
[root@localhost ~]# grep -v "^#" /etc/selinux/config | grep -v "^$"
find
用于按照指定条件来查找文件
格式为“find [查找路径] 寻找条件 操作”
搜索/dev目录下所有硬盘和分区设备
[root@localhost ~]# find /dev/ -name sd*
搜索磁盘中所有.conf格式的文件
[root@localhost ~]# find / -name *.conf
本篇到此结束,这篇文章真长QAQ