Linux基础命令笔记

199 阅读10分钟

Linux基础命令笔记(centos7)

1.关机指令

sync #将数据由内存同步到硬盘中
shutdown #关机指令
shutdown -h 10 #十分钟后关机
shutdown -now #立马关机
shutdown -20:25 #在今天20:25关机
shutdown -r now #立马重启
shutdown -r 10 #十分钟后重启
reboot #立刻重启
halt #关闭系统

2.Linux目录

/bin:存放最常用的命令

/boot:存放启动Linux时使用的核心文件,包括连接文件以及镜像文件

/dev:Device(设备)缩写,存放Linux外部设备,在Linux中访问设备的方式和访问文件的方式相同的

/etc:存放所有的系统管理所需配置文件和子目录

/home:用户的主目录

/lib:存放系统最基本的动态连接共享库,作用类似Windows里的DLL文件

/lost+found:一半为空,当系统非法关机后存放一些文件

/media:Linux会自动识别一些设备,当识别后会把识别的设备挂载在改目录下

/mnt:让用户临时挂载别的文件系统的,可以将光驱挂载在改目录

/opt:一个虚拟目录,是系统内存的映射,可以访问这个目录来获取系统信息

/root:系统管理员用户主目录

/sbin:s(super user),存放系统管理员使用的系统管理程序

/srv:存放一些服务启动之后需要提取的数据

/tmp:存放临时文件

/usr:用户的很多程序和文件都放在这个目录下,类似Windows下 program files目录

/usr/bin:超级用户使用的比较高级的管理程序和系统守护程序

/usr/src:内核源码默认存放目录

/var:存放在不断扩充的东西,我们习惯将那些经常被修改的目录放在这个目录下,包括各种日志文件

/run:一个临时文件系统,储存系统启动以来的信息,当系统重启时这个目录被删除或清除

3.常用基本命令

3.1 ls:列出目录

#参数
#-a:全部文件,连同隐藏文件(开头为.的文件)一起列出来
#-l:长数据串列出,包含文件的属性和权限等数据

[root@VM-12-13-centos home]# ls
lighthouse  myhope

#列出该目录下所有文件
[root@VM-12-13-centos myhope]# ls -al 

3.2 cd:切换目录

[root@VM-12-13-centos home]# cd myhope
[root@VM-12-13-centos myhope]# 

#回到上一级目录
[root@VM-12-13-centos myhope]# cd ../
[root@VM-12-13-centos home]#

3.3 pwd:显示目前的目录

#加参数p显示当前目录确实的路径,而非使用(连接)link路径

[root@VM-12-13-centos myhope]# pwd -p
/home/myhope

3.4 mkdir:创建一个新目录

#参数:
#-m:配置文件权限
#-p:直接递归创建多级目录

[root@VM-12-13-centos myhope]# mkdir my
[root@VM-12-13-centos myhope]# ls
my  resource  runProject

#创建多级目录
[root@VM-12-13-centos myhope]# mkdir -p test/test1
[root@VM-12-13-centos myhope]# cd test
[root@VM-12-13-centos test]# ls
test1

#创建权限为rwx(4+2+1)--x(1)--x(1)的目录
[root@VM-12-13-centos test]# mkdir -m 711 test2
[root@VM-12-13-centos test]# ls
test1  test2

3.5 rmdir:删除一个空的目录

#参数:
#-p:连同上一级的目录(空的)一起删除

[root@VM-12-13-centos myhope]# rmdir my
[root@VM-12-13-centos myhope]# ls
resource  runProject

#删除多级目录
[root@VM-12-13-centos myhope]# rmdir -p test/test1
[root@VM-12-13-centos myhope]# ls
my  resource  runProject

3.6 cp:复制文件或目录

#参数:
#-a:相当于-pdr
#-p:连同文件的属性一起复制过去,而非使用默认的属性
#-d:若来源文档为连接文档的属性(link file),则复制连接结构属性而非文件本身
#-r:递归持续复制
#-f:强制(force),若目标文件以及存在且无法开启则移除后再尝试一次
#-i:若目标文件以及存在,覆盖式会先询问
#-l:进行硬式连接(hard link)的连接档创建,而非复制文件本身
#-s:复制成为符号连接档(捷径文件)
#-u:若目标文档比复制文档旧才升级目标文档

#将/test/test1下的test2复制到/test下
[root@VM-12-13-centos test1]# ls
test2
[root@VM-12-13-centos test1]# cp test2 ../
[root@VM-12-13-centos test1]# cd ../
[root@VM-12-13-centos test]# ls
test1  test2

#再次将/test/test1下的test2复制到/test下并询问是否覆盖
[root@VM-12-13-centos test1]# cp -i test2 ../
cp: overwrite ‘../test2’? y
[root@VM-12-13-centos test1]# cd ../
[root@VM-12-13-centos test]# ls
test1  test2

3.7 rm:移除文件或者目录

#参数:
#-f:force的意思,忽略不存在的文件不会出现警告信息
#-i:在删除前询问是否动作
#-r:递归删除,最常用的目录删除

#询问删除/test/test1下的test2文件
[root@VM-12-13-centos test1]# rm -i test2
rm: remove regular empty file ‘test2’? y
[root@VM-12-13-centos test1]# ls
[root@VM-12-13-centos test1]# 

#-f参数删除不存在的文件
[root@VM-12-13-centos test1]# ls
[root@VM-12-13-centos test1]# rm -f test2
[root@VM-12-13-centos test1]# 

#递归删除/test目录(包含下面子目录)
[root@VM-12-13-centos myhope]# rm -r test
rm: descend into directory ‘test’? y
rm: descend into directory ‘test/test1’? y
rm: remove regular empty file ‘test/test1/test2’? y
rm: remove directory ‘test/test1’? y
rm: remove directory ‘test’? y
[root@VM-12-13-centos myhope]# ls
my  resource  runProject

3.8 mv:移动文件或目录

#参数:
#-f:force强制,如果目标文件以及存在不会询问而直接覆盖
#-i:如果目标文件存在询问是否覆盖
#-u:如果目标文件存在且移动文件比目标文件新才升级

#-i参数询问移动/test/test1/test2/test3文件到test1目录下
[root@VM-12-13-centos test2]# mv -i  test3 ../
[root@VM-12-13-centos test2]# touch test3
[root@VM-12-13-centos test2]# mv -i  test3 ../
mv: overwrite ‘../test3’? y
[root@VM-12-13-centos test2]# ls

#不询问直接移动/test/test1/test2/test3文件到test1(已有test3文件)目录下
[root@VM-12-13-centos test2]# touch test3
[root@VM-12-13-centos test2]# mv -f test3 ../

#修改文件或目录名字
[root@VM-12-13-centos test1]# ls
test2  test3
[root@VM-12-13-centos test1]# mv test3 test4
[root@VM-12-13-centos test1]# ls
test2  test4

4.文件属性

Linux系统是一个典型的多用户系统,不同用户处于不同地位拥有不同权限。为了保护系统安全性,Linux对不同用户访问同一文件(夹)的权限做了不同规定。

用ll或者ls -l命令来显示一个文件的属性以及文件所属的用户和用户组

[root@VM-12-13-centos test1]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Nov  2 23:26 test2
-rw-r--r-- 1 root root    0 Nov  2 23:26 test4

#在第一个字符代表这个文件的类型
#d:目录
#-:文件
#l:链接文档(link file)
#b:装置文件里面的可供储存的接口设备(可随机存取装置)
#c:装置文件里面串行端口设备(如键盘、鼠标等一次性读取装置)
#
#接下来字符中,三个一组,均为r(读:权限数字4)w(写:权限数字2)x(可执行:权限数字1),三组从左往右依次为属主权限、属组权限、其他用户权限。

4.1 chgrp:更改文件属组

#参数:
#-R:递归更改文件属组

4.2 chown:更改文件属主也可以同时更改文件属组

chown -R 属主名 文件名
chown -R 属主名:属组名 文件名

4.3 chmod:更改文件9个属性

[root@VM-12-13-centos test]# ls
test1
[root@VM-12-13-centos test]# chmod 711 test1
[root@VM-12-13-centos test]# ll
total 4
drwx--x--x 3 root root 4096 Nov  2 23:27 test1

5.文件内容查看

5.1 cat:由第一行开始显示文件内容

#参数:
#-A:相当于-vET,可列出一些特殊字符而不是空白
#-b:列出行号,针对非空白行做出行号显示,空白行不标行号
#-E:将结尾断行字节$显示出来
#-n:列出行号(包括空白行)
#-T:将tab键以^l显示出来
#-v:;列出看不到的特殊字符

#列出行号包括空白
[root@VM-12-13-centos failure]# cat -b failure.log

     1	  .   ____          _            __ _ _
     2	 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
     3	( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     4	 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
     5	  '  |____| .__|_| |_|_| |_\__, | / / / /
     6	 =========|_|==============|___/=/_/_/_/
     7	 :: Spring Boot ::        (v2.3.7.RELEASE)

#列出行号(非空白)
[root@VM-12-13-centos failure]# cat -n failure.log
     1	
     2	  .   ____          _            __ _ _
     3	 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
     4	( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     5	 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
     6	  '  |____| .__|_| |_|_| |_\__, | / / / /
     7	 =========|_|==============|___/=/_/_/_/
     8	 :: Spring Boot ::        (v2.3.7.RELEASE)

5.2 tac:文件内容从最后一行开始显示

5.3 nl:显示行号

#参数:

#-b:指定行号的方式,-b a:列出行号(包括空白类似cat-n),-b t:列出行号(不包括空白,默认)


#-n:列出行号表示方法,主要三种:-n ln:行号在荧幕最左显示,-n rn:行号在自己栏位最右方显示且不加0,- n rz:行号在自己栏位最右显示且加0

5.4 more:一页一页翻动

#空格键:向下翻一页
#回车键:向下翻一行
#/字符串:在显示内容中向下搜索字符串这个关键字
#f:立刻显示出档名以及目前显示的行数
#q:退出more,不在显示文件内容
#b:往回翻页只对文件有用

[root@VM-12-13-centos failure]# more failure.log

5.5 less:一页一页翻动

#空格键:向下翻一页
#pageDown:向下翻一页
#pageUp:向上翻一页
#/字串:向下搜索字串
#?字串:向上搜素字串
#n:重复前一个搜索
#N:反复前一个搜索
#q:退出less

[root@VM-12-13-centos failure]# less failure.log

5.6 head -n:取出文件前几行

[root@VM-12-13-centos failure]# head -n 2 failure.log
#默认显示前10行

5.7 tail -n:取出文件后几行

[root@VM-12-13-centos failure]# tail -n 2 failure.log
#默认显示后10行

6.Linux链接概念

linux链接分为硬链接和符号链接(软链接)

ln命令产生硬链接

软链接类似Windows里面的快捷方式

[root@VM-12-13-centos test]# touch test2
[root@VM-12-13-centos test]# ls
test1  test2
[root@VM-12-13-centos test]# ln test2 test3 #创建test2的硬链接文件test3
[root@VM-12-13-centos test]# ln -s test2 test4 #创建test2的软连接文件test4
[root@VM-12-13-centos test]# ls
test1  test2  test3  test4
[root@VM-12-13-centos test]# ls -li
total 4
659970 drwx--x--x 3 root root 4096 Nov  2 23:27 test1
659984 -rw-r--r-- 2 root root    0 Nov  3 13:14 test2
659984 -rw-r--r-- 2 root root    0 Nov  3 13:14 test3
659986 lrwxrwxrwx 1 root root    5 Nov  3 13:15 test4 -> test2

#删除符号链接test4对test2,test3无影响
#删除硬链接test3对test2,test4无影响
#删除原文件test2对硬链接test3无影响,导致符号链接test4失效
#同时删除原文件test2和硬链接test3整个文件会被真正删除

7.三种安装方式以及防火墙的打开

7.1rpm安装

[root@VM-12-13-centos test]# rpm -qa|grep jdk #检查存在的JDK
jdk-17-17.0.4.1-ga.x86_64
[root@VM-12-13-centos test]# rpm -e --nodeps jdk-17-17.0.4.1-ga.x86_64 #强制删除
[root@VM-12-13-centos test]# rpm -ivh jdk-17-17.0.4.1-ga.x86_64.rpm #rpm安装rpm包
[root@VM-12-13-centos test]# source 文件名 #让某个文件生效

7.2 tar安装

[root@VM-12-13-centos test]# tar -zxvf tar.gz文件 #解压文件

7.3 yum安装

yum -y remove 要移除文件 #卸载文件
yum -y install 要安装的文件 #安装文件
yum makecache fast #更新软件包索引

7.4 防火墙

#启动
service firewalld start
systemctl start firewalld
#重启
service firewalld restart
systemctl restart firewalld
#关闭
service firewalld stop
service stop firewalld

#查看防火墙规则
firewall-cmd --list-all #查看全部信息
firewall-cmd --list-ports #查看端口信息

#开启端口
firewall-cmd --zone(作用域)=public --add-port=80/tcp --permanent(永久生效,无此参数重启失效)