Linux操作系统
- 💹lecture4已经完成
- 💹lecture2 还有最后一点没有写完
- 💹lecture3
- 💹lecture4
- 💹lecture5
- 💹lecture6
- 💹lecture7
- 💹lecture8已经完成
- 💹lecture9 只剩下until loop 和 while loop没有
- 💹lecture10已经完成,
- 💹lecture11已经完成,
❗❗❗程序的部分站的比例比较高,程序这一块还是留到考完oracle在复习了
❗❗❗40分以上的题来自 编程shell
-
lecture10和lecture11已经复习,也听了老师的视屏重点,
-
lecture2和lecture3看了宣哥ppt复习了,但是没有看老师的视频回放复习重点
-
🤷♂️这个是不知道不熟悉的知识点
-
lecture5 只有正则表达式没有做
-
lectrun6 已经做了,但是那个任务还没有完全理解
-
😀第九和第十单元已经复习了三次了
⚠⚠⚠
-
Ubuntu的root用户密码为12345678
-
安装的时候 :安装就按照老师给的lecture1的ppt步骤来做就行了
-
账号chentianxi, 超级用户:ru - root 密码:123456 💹💹💹 注意:数字输入numlock锁住了
-
密码123456
-
ctrl shif + 三个键可以把Teminal终端调大
-
ctrl j加上 - 减号缩小
-
可以看文件夹里的linux命令大全
-
su root 超级用户登录
-
su 也可以
💹csdn连接
(41条消息) ❤️肝下25万字的《决战Linux到精通》笔记,你的Linux水平将从入门到入魔❤️【建议收藏】_小小明-代码实体的博客-CSDN博客_决战linux
- /:/是根目录.Unix 和 Linux 中,没有盘符. 只有一个硬盘,一个根.
- /bin :系统的常用命令目录. 包括控制台命令, 系统可执行文件, 系统的核心二进制文件等.
- /etc: 发布目录, 相当于 windows 系统中的 windows 目录, 保存系统中的所有核心内容. 要求控制权限高, 建议不要随便读写.
- /usr :用户目录, 相当于 windows 系统中的 program files 目录. 常用于安装系统所有用户共用的软件,资源的.
- /root :root 根用户的用户目录 . 相当于 windows 系统中的C:/users/administrator 目录.
- /home :保存其他用户主目录的目录. 如: Linux 系统中有 oldlu 用户. 那么一定有/home/oldlu 目录存在. 代表用户的主目录.
- /var :系统运行过程的数据目录.
它的基本的操作命令有:
- 1.cd: change directory - 切换目录. 特殊目录符号。其中当前目录为 - ‘.’ , 父目录为 - ‘…’ 它的使用方式为:使用根目录作为定位标准, 绝对寻址. cd:/xxx/yyy/。相对寻址为:cd xxx/yyy/zzz 。直接进入用户主目录 cd cd ~
- 2.ls: list - 列表目录中的内容.默认显示当前目录下的文件列表 .其中ls -a [目录] 表示当前目录下的所有的文件和目录。
- 3.ll:简化命令list list - 以列表的信息,显示指定目录中的内容. 列表代表的是文件的详情.
- 4.clear:清空屏
- 5.touch :创建空白文件在 Linux 系统中,文件不需要强制后缀名. 如: 文本文件可以定义为, a | a.txt | a.text
- 6.cat :查看文件的全部内容. 一次性显示文件中所有内容.
- 7.more 分屏显示文件内容, 显示后,使用空格显示下一屏, 回车显示下一行,q 退出分屏显示. ctrl+c,退出命令.
- 8.head 显示文件的前多少行, 默认显示前 10 行. head -number filename 查看文件中的前多少行.
- 9.tail :显示文件末尾多少行.默认显示末尾 10 行. tail -number filename
- \10. mkdirmake directory - 创建目录. 用法为mkdir directoryName ,一次性创建多级目录
- mkdir -p parentDirectoryName/childDirectoryName
- \11. cp :copy - 复制命令. 使用方式 copy source target copy 源信息 目录:copy 源信息 目录信息
- 12.rm remove - 删除 :使用方式 rm source rm 要删除的资源
- 13.mv :move - 移动或重命名. 相当于剪切和重命名.
- 14:vi | vim 编辑文件, vim 是增强命令. 不代表所有的 Linux 都支持. vim 增强在有高亮显示.
- \15. ifconfig 查看网络编辑器. 查看网卡信息. eth0 - 命名为 eth0 的网卡信息
- 16.service 服务控制命令. 常用服务: iptables - 防火墙, vsftpd ftp 文件服务器, mysql 防火墙建议关闭. 否则除 80,22 端口外,其他所有端口无法访问.
- 17.ps 进程信息查看命令.
- 18.grep 过滤|筛选, 筛选符合关键字的数据.
LinuxLecture2
shell命令
选项卡选项卡选项卡
- Ctrl+A 将光标移到开头
- Ctrl+C 结束运行程序
- Ctrl+D 结束当前会话
- Ctrl+E 将光标移到末尾
- Ctrl+H 退格键
- Ctrl+L 清除
- Ctrl+R 搜索命令历史记录
- Ctrl+Z 挂起程序
- ctrl + shift + c 复制 (insert+ctrl)
- ctrl + shift + v粘贴 (insert+shift)
- Tab Auto complete
- Tab Tab Show possible auto completes
- Up/Down Command history (查看最近的记录)
- Left/Right Move cursor ()
ls 显示目录
- 蓝色–>目录
- 绿色–>可执行文件
- 红色–>压缩文件
- 浅蓝色–>链接文件
- 灰色–>其他文件
常用的有ls、ls -l、ls-a
- ls 列出当前目录
- ls ~ 列表主目录
- ls … 列出父目录 🤷♂️
- ls / 列表根目录
- ls 普通显示
- ls -a 列出所有文件,包括隐藏的文件 🤷♂️
- ls -l 列出长列表中的文件(详细信息)🤷♂️
- ls -al 列出长列表中的所有文件
- ll ls -l 同ll,显示出ls结果的详细信息🤷♂️
cd
- cd . 进入当前文件夹
- cd … 返回上级文件夹
- cd~和cd都是回到当前用户的家目录
- cd – 返回上一个访问的目录 🤷♂️
- cd / 进入根目录
pwd 显示当前路径
**cat a.txt ** 浏览看内容
由第一行开始显示文件内容 💹💹💹 touch file1.txt.
[chentianxi@localhost test]$ touch tst.txt [chentianxi@localhost test]$ ls tst.txt [chentianxi@localhost test]$ vim tst.txt #我在里面写了afaefaffe 按esc 在shift+: wq保存退出 [chentianxi@localhost test]$ cat tst.txt afaefaffe
- tac 从最后一行开始显示
man 读取命令的手册页
文件类型
- - 常规文件
- d 目录
- l 链接
- c 特殊文件
- s 插座
- p 命名管道
- b 块设备
chentainxi@localhost test]$ ls -l
total 16
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 30 05:39 aaa.txt 💹#-rw-rw-r-- 开头便是- 表示常规文件
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
-rw-rw-r--. 1 chentainxi chentainxi 15 Dec 1 03:49 abc.txt
drwxrwxr-x. 2 chentainxi chentainxi 6 Nov 30 05:40 bb 💹#开头d 表示目录
-rwxrwxr-x. 1 chentainxi chentainxi 42 Nov 30 05:58 my_shell.sh
-rw-rw-r--. 1 chentainxi chentainxi 104 Nov 30 06:06 test.sh
文件系统
In a Linux system, everything is a file.
If something is not a file, it is a process.
跟windows差不多,可以层层嵌套
文件名
不超过256个字符
区分大小写Chen和chen是不一样的
可以使用大小写字母,数字, ( . ), _下划线
以下符号是不允许的G”/美元;& !* "«?
根目录 /
- ls /
- cd /
- 对于用户家目录
/home/user_name - 对于管理员的家目录
/root
绝对路径和相对路劲
-
cd … 上一层
-
cd ./ 当前路径
-
ls … 显示上一层目录
-
ls ./当前目录
三个常用命令
pwd print working directory (当前路径)
cd change working directory (去到指定目录)
ls list content of directory (看下当前目录下有哪些文件和目录)
touch (创建空文件 )
create empty files
eg:
touch bb.txt #创建bb.txt文件
test
[chentainxi@localhost test]$ touch {1,2,3} 🤷♂️ #这个1,2,3中间是有逗号的,
[chentainxi@localhost test]$ ls
1 2 3 my_shell.sh
[chentainxi@localhost ~]$ touch {aa,bb}{1,2,3} 笛卡尔乘积
[chentainxi@localhost ~]$ ls
aa1 aa3 bb2 Desktop Downloads Pictures Templates Videos
aa2 bb1 bb3 Documents Music Public test
[chentainxi@localhost ~]$
mkdir(创建文件)
-
-m :指定被创建目录的权限,而不是根据默认权限 (umask) 设定
-
-p 确保目录名称存在,不存在的就建一个。🤷♂️
-
-m:在创建目录的同时设定权限
-
-p:可新建多级文件夹,当路径中的某个目录已经存在时,跳过该目录继续创建下一级目录
-
-v:显示创建目录的过程信息
-
-Z:将每个创建的目录的SELinux 安全环境设置为CTX
在多个文件夹下创建相同的子文件夹:
-p 🤷♂️
创建多级目录
[chentainxi@localhost test1]$# mkdir -p aa/bb/cc
[chentainxi@localhost test1]$ ls
aa
[chentainxi@localhost test1]$ cd aa
[chentainxi@localhost aa]$ ls
bb
mkdir -v 🤷♂️
make directories
cp (拷贝)
1. 将一个文件复制到目标文件(夹)中
2、同时将多个文件复制到目标文件(夹)中
3、将一个文件夹复制到目标文件中
- -a 该选项通常在复制目录时使用,它保留链接、文件属性,并递归地复制目录,简单而言,保持文件原有属性。
- -f 直接覆盖,不提示🤷♂️
- -i 交互式复制,在覆盖目标文件之前将给出提示要求用户确认🤷♂️
- -r 若给出的源文件是目录文件,则cp将递归复制该目录下的所有子目录和文件,目标文件必须为一个目录名。
- -v 显示拷贝进度
- -p 保留源文件或目录的所有属性🤷♂️
- -l 创建硬链接(hard link),而非复制文件本身
- -s 复制成为符号链接 (symbolic link),相当于批量创建快捷方式
- -u 若 destination 比 source 旧才升级 destination !
cp vim_configure/ code/ -ivr #把文件夹 vim_configure 拷贝到 code 目录里。
#使用指令 cp 将当前目录 test/ 下的所有文件复制到新目录 newtest 下,输入如下命令:
$ cp –r test/ newtest
#下面四个命令结果相同,都是递归拷贝 packageA 文件及其任意层的结构到 packageB 中:
cp -r /home/packageA /home/packageB
🤷♂️复制文件夹一定要加“**-r”**参数,否则会出现“cp: omitting directory”错误。
将test1 拷贝到 test下面
[chentainxi@localhost ~]$ ls
Desktop Downloads Pictures Templates test1
Documents Music Public test Videos
[chentainxi@localhost test1]$ ls
11 22 aa a.txt c ccc.txt d
[chentainxi@localhost test1]$ cd ..
[chentainxi@localhost ~]$ cp -r ./test1 ./test/
[chentainxi@localhost ~]$ cd test
[chentainxi@localhost test]$ ls
test1
[chentainxi@localhost test]$ cd test1
[chentainxi@localhost test1]$ ls
11 22 aa a.txt c ccc.txt d
[chentainxi@localhost test1]$
mv (移动文件)
💹使用方法跟cp一模一样的
-f | 禁止交互式操作,如有覆盖也不会给出提示🤷♂️ |
---|---|
-i | 确认交互方式操作,如果mv操作将导致对已存在的目标文件的覆盖,系统会询问是否重写,要求用户回答以避免误覆盖文件🤷♂️ |
-v | 显示移动进度 🤷♂️ |
rm (删除文件)
option
-i | 以进行交互式方式执行 |
---|---|
-f | 强制删除,忽略不存在的文件,无需提示 |
-r | 递归地删除目录下的内容,⭕删除文件夹时必须加此参数 |
rmdir (删除文件目录)
remove directories
⚠一定区分和rm, rm是删除 , mv是移动
①移动文件🤷♂️
#a b 都是 directory
[chentainxi@localhost test]$ cd a
[chentainxi@localhost a]$ touch 1.txt #在a里面创建
[chentainxi@localhost a]$ ls
1.txt
[chentainxi@localhost a]$ cd ..
[chentainxi@localhost test]$ mv a/1.txt b/ #移动到b下
[chentainxi@localhost test]$ ls b
1.txt
[chentainxi@localhost test]$ ls a
②移动目录directory🤷♂️
[chentainxi@localhost test]$ ls
a b
[chentainxi@localhost test]$ mv b/ a/
[chentainxi@localhost test]$ ls a #在当前面目录把b目录移动到 a里面
b
[chentainxi@localhost test]$ cd a
[chentainxi@localhost a]$ cd b
[chentainxi@localhost b]$ ls
1.txt 2.txt 3.txt
[chentainxi@localhost b]$ pwd
#/home/chentainxi/test/a/b 💹本来是a和b平行,但是移动 mv b/ a/ 把b目录移到a里面了
LinuxLecture3
vi Edit or New & Edit file 编辑问文件或者新建文件
vim Same as vi, more features 跟vi差不多,有更多的特性 (现在主流用这个💹)
view Starts vim in read-only mode 只读文件
cat Read text file content 读取文本文件内容
vim全套笔记(chapter3)
- 命令模式(command mode)🤷♂️
- 文本输入模式
- 末行模式
什么是 vim?
Vim是从 vi 发展出来的一个文本编辑器。代码补完、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。vim 的官方网站 (http://www.vim.org)
三种模式
基本上vi可以分为三种状态:
-
命令模式(command mode)
-
插入模式(Insert mode)💹💹💹
-
底行模式(last line mode)
①按:冒号即可进入last line mode (尾行模式)
-
:冒号进入尾行模式
-
:w 保存
:q Quit
:! Force
:w! 强制保存
:q! 退出而不保存更改 🤷♂️强制退出
:wq 保存并退出
:wq! 强制保存并退出
:x Same as :wq
ZZ Same as :wq:n Positions the cursor at line n.
😒 Positions the cursor on the last line.:set nu Turn on line numbering.
:set nonu Turn off line numbering.
②从command mode进入Insert mode (插入模式)
-
按i在当前位置编辑
-
按a在当前位置的下一个字符编辑
-
按o插入新行,从行首开始编辑
-
按R(Replace mode):R会一直取代光标所在的文字,直到按下 ESC为止;(常用)
③按ESC键退回command mode (命令模式)
用户刚刚启动 vi/vim,便进入了命令模式。 任何时候,不管用户处于何种模式,只要按一下ESC键,即可使Vi进入命令模式;此状态下敲击键盘动作会被Vim识别为命令,输入: 可切换到底线命令模式,以在最底一行输入命令。若想要编辑文本:启动Vim,进入了命令模式,按下i,切换到输入模式。
k Up one line
j 下一行
h 上一行
l Right one character
w Right one word
b Left one word
^ Go to the beginning of the line
$ Go to the end of the line
命令模式下的操作
⚠以下替换、删除、撤销、搜索、复原都是在命令模式之下(按esc)
进入替换模式(Replace mode)
- r : 只会取代光标所在的那一个字符一次
- R: 会一直取代光标所在的文字,直到按下ESC为止
删除操作 (delete)
- 删除操作 删除后会添加到剪切板,相当于剪切
- x, X x为向后删除一个字符 (相当于 [del] 按键), X为向前删除一个字符(相当于 [backspace] )
- nx n 为数字,连续向后删除 n 个字符。例如10x表示连续删除 10 个字符。
- **dd 删除光标所在的一整行(常用)**🤷♂️
- ndd n 为数字。删除光标所在的向下 n 行,例如 20dd 则是删除 20 行
- d1G 删除光标所在行到首行的所有数据
- dG 删除光标所在行到最后一行的所有数据
- d$ 删除光标所在位置到该行的最后一个字符
- d0 删除光标所在位置到该行的最前面一个字符
撤销&复原&重复
u | 撤销操作,相对于普通编辑器里面的ctrl+z 🤷♂️ |
---|---|
Ctrl+r | 恢复操作,相对于普通编辑器里面的ctrl+y |
. | 就是小数点!可重复前一个动作 |
搜索
/word | 向光标之下寻找一个名称为 word 的字符串。 |
---|---|
?word | 向光标之上寻找一个字符串名称为 word 的字符串。 |
n | 代表重复前一个搜寻的动作,根据前面输入的/word还是?word向下或向上搜索下一个匹配的字符串。 |
N | 表示反向搜索,与n的搜索方向相反。 |
替换
- :n1,n2s/word1/word2/g 在第 n1 与 n2 行之间寻找word1并替换为word2!比如『:100,200s/vbird/VBIRD/g』表示在100到200行之间将vbird替换为VBIRD
- :1,$s/word1/word2/g 或 :%s/word1/word2/g $表示最后一行,%s表示所有行。
- :1,$s/word1/word2/gc 或 :%s/word1/word2/gc gc中的c表示取代前显示提示字符给用户确认 (confirm) !
- >
为什么我们要这样做呢?因为有时候把一个命令的运行结果存储到一个文件很有用处。把 ls 命令的运行结果输送到文件
ls-output.txt 中去,由文件代替屏幕。
[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt
wc - 统计字数
可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。
wc [-l、w、m] [filename]
- -l : 统计行数
- -w :统计英文单词 word🤷♂️
- -m :统计字符数
eg:
python@xxx:~$ wc -l /etc/passwd
49 /etc/passwd
python@xxx:~$ wc -w /etc/passwd
81 /etc/passwd
python@xxx:~$ wc -m /etc/passwd
2696 /etc/passwd
例如同时统计testfile、testfile_1、testfile_2,可使用如下命令:
$ wc testfile testfile_1 testfile_2 #统计三个文件的信息
3 92 598 testfile #第一个文件行数为3、单词数92、字节数598
9 18 78 testfile_1 #第二个文件的行数为9、单词数18、字节数78
3 6 32 testfile_2 #第三个文件的行数为3、单词数6、字节数32
15 116 708 总用量 #三个文件总共的行数为15、单词数116、字节数708
[lei@localhost ~]$ cat intro
Hello,
this is Lei
your Linux lecture
[lei@localhost ~]$ wc intro
3 7 39 intro
[lei@localhost ~]$ wc -w intro
7 intro
[lei@localhost ~]$ wc -l intro
3 intro
[lei@localhost ~]$ wc -c intro
39 intro
取首尾n行:head、tail
**①head -n **
语法:
head [-n number] 文件
head -n 文件名
[chentainxi@localhost test]$ vim a.txt
[chentainxi@localhost test]$# head -3 a.txt 💹显示前三行
faefsfss
sssssssssss
sssssssssssss
②tail -n
语法:
tail [-n number] 文件
eg:
[root@www ~]# tail /etc/man.config
# 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
[root@www ~]# tail -n 20 /etc/man.config
选项与参数:
- -n :后面接数字,代表显示几行的意思
- -f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测
Linux管道命令
重定向 >和>>
①>:是定向输出到文件中,如果文件存在,则会将内容清空。
②>>:1.将输出内容追加到目标文件中,2.如果文件存在,则将新的内容追加到目标文件的末尾,原有内容不受影响。
①>直接覆盖原有文件内容
[chentainxi@localhost ~]$ ls
Desktop Downloads Pictures Templates test1 Videos
Documents Music Public test test3
[chentainxi@localhost ~]$ #ls > a.txt 💹把ls 的结果赋给 a.txt
[chentainxi@localhost ~]$ ls
a.txt Documents Music Public test test3
Desktop Downloads Pictures Templates test1 Videos
[chentainxi@localhost ~]$ #cat a.txt
a.txt
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
test
test1
test3
Videos
②>>结果追加在文件内容尾部
Linux的管道命令是💹💹💹 ’|’,通过它可以对数据进行连续处理,其示意图如下:
Pipe管道线 |
<cmd1> | <cmd2> ⭕🤷♂️后面都是不用接 a.txt这些的,因为cmd1已经有来源
< cmd1 > | < cmd2 > 将的结果(输出)作为的输入
eg案例
#案例一
[chentainxi@localhost ~]$ ls
a.txt Documents Music Public test test3
Desktop Downloads Pictures Templates test1 Videos
[chentainxi@localhost ~]$# ls |wc -w #ls上面的结果是12个单词wc -w是计算多少个单词
12
#案例二
[chentainxi@localhost ~]$ cat a.txt | head -2
a.txt
Desktop
[chentainxi@localhost ~]$ cat a.txt
a.txt
Desktop
Documents
a) With >
ls / > aa.txt
pwd > aa.txt
cat aa.txt
b) With >>
ls / >> aa.txt
pwd >> aa.txt
cat aa.txt
案例
- ls /
- ls / > RootContent
- cat RootContent
ifconfig命令
移除virbro并禁用服务
- ifconfig virbr0 down
- systemctl disable(enable) libvirtd.service
- ifconfig again , virbr0 no longer there
要yum安装才能用tree
tree命令
linuxLectrue4
⭕宣哥ppt19页
Linux 用户和用户组管理 | 菜鸟教程 (runoob.com)
用户和用户组管理
-
用户管理包括用户与组账号的管理。
-
在Unix/Linux系统中,不论是由本机或是远程登录系统,每个系统都必须拥有一个账号,并且对于不同的系统资源拥有不同的使用权限。
-
Unix/Linux系统中的root账号通常用于系统的维护和管理,它对Unix/Linux操作系统的所有部分具有不受限制的访问权限。
-
在Unix/Linux安装的过程中,系统会自动创建许多用户账号,而这些默认的用户就称为**“标准用户”**。
-
在大多数版本的Unix/Linux中,都不推荐直接使用root账号登录系统。
查看当前用户:whoami
cat /etc/passwd❗❗❗
退出登录账户: exit
[chentainxi@localhost ~]$ exit
查看登录用户:who
who命令用于💹查看当前所有登录系统的用户信息。
常用选项:
-m或am I | 只显示运行who命令的用户名、登录终端和登录时间 |
---|---|
-q或–count | 只显示用户的登录账号和登录用户的数量 |
-u | 在登录时间后显示该用户最后一次操作到当前的时间间隔 |
-u或–heading | 显示列标题 |
[chentainxi@localhost ~]$ who
chentainxi :0 2022-10-28 00:44 (:0)
chentainxi pts/0 2022-10-28 00:44 (:0)
[chentainxi@localhost ~]$ who -m
chentainxi pts/0 2022-10-28 00:44 (:0)
[chentainxi@localhost ~]$ who -q
#chentainxi chentainxi
# users=2
[chentainxi@localhost ~]$ who -u
#chentainxi :0 2022-10-28 00:44 ? 1862 (:0)
#chentainxi pts/0 2022-10-28 00:44 . 2618 (:0)
*/etc/group 文件的格式:4 个字段,由“**😗*”分隔*
添加、删除组账号:groupadd、groupdel
- groupadd 新建组账号
- groupdel 组账号
- cat /etc/group 查看用户组 💹💹💹❗❗❗
python@ubuntu:~/test$ sudo groupadd abc #添加
python@ubuntu:~/test$ sudo groupdel abc #删除
python@ubuntu:~/test$ sudo groupdel abc
groupdel:“abc”组不存在
用户组管理:groupmod
修改用户组的属性使用groupmod命令。其语法如下:
groupmod 选项 用户组
常用的选项有:
-g GID 为用户组指定新的组标识号。
-o 与-g选项同时使用,用户组的新GID可以与系统已有用户组的GID相同。
-n新用户组 将用户组的名字改为新名字
将组group2的组标识号修改为102:
groupmod -g 102 group2
将组group2的标识号改为10000,组名修改为group3:
groupmod –g 10000 -n group3 group2
主目录 (Home Directory)
- 当登录时,用户被放在他们的主目录中。
- 如果该目录不存在,则将它们放在其中
添加用户账号:useradd
useradd
常用的选项包括-c, -d, -m, -g, -G, -s, -u以及-o等
,这些选项的意义与useradd
命令中的选项一样,可以为用户指定新的资源值
**Options ** (老师的)
- -c Comments for the account.
- -m Create the home directory.
- -s <shell_path>The path to the user’s shell.
- -g Specify the default group.
- -d /home/dir Specify the home directory.
[chentainxi@localhost ~]$ useradd –c "Grant Stewart" –m –s /bin/bash grant 💹💹💹这里用到三个option
csdn的
参数 | 含义 |
---|---|
-m | 自动建立目录,未指定-d参数时会在**/home/{当前用户}**目录下建立主目录 |
-g | 指定组名称 |
-d | 指定用户登录系统时的主目录 |
example
useradd -d /home/a a -g test -m | 创建一个用户名字叫a,主目录在/home/a,如果主目录不存在,就自动创建主目录,同时用户属于test组 |
---|---|
cat /etc/passwd | 查看系统当前用户名 |
useradd -d /home/abc abc -m | 创建abc用户,如果/home/abc目录不存在,就自动创建这个目录,同时用户属于abc组 |
[chentainxi@localhost ~]$ su
Password:
'abrt-cli status' timed out
[root@localhost chentainxi]# useradd -d /home/abc abc -m
[root@localhost chentainxi]# ls
Desktop Documents Downloads Music Pictures Public Templates test Videos
[root@localhost chentainxi]# pwd
/home/chentainxi
[root@localhost chentainxi]# cd ..
[root@localhost home]# ls
#abc chentainxi
[root@localhost home]#
设置用户密码:passwd
- -l 锁定口令,即禁用账号。
- -u 口令解锁。
- -d 使账号无口令。
- -f 强迫用户下次登录时修改口令。
$ passwd
Old password:******
New password:*******
Re-enter new password:*******
老师的在root下
- passwd
- set password for
- passwd grant
- Enter new UNIX password:
Retype new UNIX password:
passwd: password updated
successfully
修改用户:usermod
改用户所在组:usermod -g 用户组 用户名
[root@localhost chentainxi]# usermod -g test abc 💹💹💹把用户abc改他的用户组改为 test
改abc用户的家目录位置:usermod -d 家目录 用户名
[root@localhost chentainxi]# usermod -d /home/abc1 abc 💹💹💹把用户abc改他的 家目录 为 /home/abc1
老师的ppt
-c “COMMENT” Comments account.
-g GROUP Specify the default group.
-s <shell_path>The path to the user’s shell.
-d /home/dir Specify the home directory.
用户删除:userdel
-
userdel delete only the user
-
userdel -r remove the home directory as well
[root@localhost home]# userdel -r abc 删除abc用户
(47条消息) Linux命令之who、who am i、whoami 的区别_MultiSkill的博客-CSDN博客
文件权限
⭕ls -l 查看文件权限
[chentainxi@localhost test]$ ls -l
total 0
drwxrwxr-x. 2 chentainxi chentainxi 6 Oct 28 03:12 aa
-
r “只读”许可是
-
W “写”的许可
-
X “执行”的许可对于目录,
x 授予列出目录内容的权限
-
读权限(r) :对于文件,具有读取文件内容的权限;对于目录,具有浏览目录的权限。
-
写权限(w) :对于文件,具有修改文件内容的权限;对于目录,具有删除、移动目录内文件的权限。
-
可执行权限(x): 对于文件,具有执行文件的权限;对于目录,该用户具有进入目录的权限
linuxLecture5
1.连接到Internet网络设置
ifconfig
- ens# 网络配置信息
- virbr0 虚拟网卡,由于libvirtd服务应该被删除
- lo 本地网络
移除virbrO并关闭服务
ifconfig virbr0/1 down
systemctl disable libvirtd.service #在开机时禁用服务
查看或配置网卡信息:ifconfig
ifconfig显示所有网卡的信息:
python@ubuntu:~$ ifconfig
ens33 Link encap:以太网 硬件地址 00:0c:29:59:65:f2
inet 地址:192.168.40.11 广播:192.168.40.255 掩码:255.255.255.0
inet6 地址: fe80::432f:6c4a:f47d:5f6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:422794 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:208666 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:603741383 (603.7 MB) 发送字节:12819550 (12.8 MB)
lo Link encap:本地环回
inet 地址:127.0.0.1 掩码:255.0.0.0
inet6 地址: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 跃点数:1
接收数据包:2248 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:2248 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1
接收字节:497588 (497.5 KB) 发送字节:497588 (497.5 KB)
修改ip:
sudo ifconfig ens33 192.168.40.10
python@ubuntu:~$ sudo ifconfig ens33 192.168.40.10
python@ubuntu:~$ ifconfig ens33
ens33 Link encap:以太网 硬件地址 00:0c:29:59:65:f2
inet 地址:192.168.40.10 广播:192.168.40.255 掩码:255.255.255.0
inet6 地址: fe80::432f:6c4a:f47d:5f6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:422818 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:208692 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:603745130 (603.7 MB) 发送字节:12822615 (12.8 MB)
Remove virbr0 and disable service(移除virbrO并禁用服务)
- ifconfig virbr0 down
- systemctl disable libvirtd.service
- ifconfig again, virbr0 no longer there
- Job done
修改ifcfg-ens上网
Modify network configuration(更改网络配置)
/etc/sysconfig/network-scripts/ifcfg-ens#
the configuration file
[chentainxi@localhost ~]$ ping baidu.com
ping: baidu.com: Name or service not known
[chentainxi@localhost ~]$ cd /etc/sysconfig/network-scripts
[chentainxi@localhost network-scripts]$ ls
#ifcfg-ens33💹 ifdown-ppp ifup-ib ifup-Team
ifcfg-lo ifdown-routes ifup-ippp ifup-TeamPort
ifdown ifdown-sit ifup-ipv6 ifup-tunnel
ifdown-bnep ifdown-Team ifup-isdn ifup-wireless
ifdown-eth ifdown-TeamPort ifup-plip init.ipv6-global
ifdown-ib ifdown-tunnel ifup-plusb network-functions
ifdown-ippp ifup ifup-post network-functions-ipv6
ifdown-ipv6 ifup-aliases ifup-ppp
ifdown-isdn ifup-bnep ifup-routes
ifdown-post ifup-eth ifup-sit
2.root用户进入ifcfg-ens33
[chentainxi@localhost network-scripts]$ su
Password:
[root@localhost network-scripts]# vim ifcfg-ens33
3.vim修改onboot=yes
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=11ad7174-a585-49b5-8997-2e27dcf5b3e8
DEVICE=ens33
ONBOOT=yes #改为yes后按住一下esc键这里改为yes之后就可以上网了
~
~
~
~
:wq #先按住shift + : 然后再输入wq 强制退出
也可以配置静态的
BOOTPROTO=static # fixed IP
IPADDR=192.168.83.3 # IP address ip地址
NETMASK=255.255.255.0 # mask 网关
再查看ipconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.253.129 netmask 255.255.255.0 broadcast 192.168.253.255
inet6 fe80::be20:fae6:7af2:e4e6 prefixlen 64 scopeid 0x20<link>
🤷♂️ ether 00:0c:29:6b:e2:63 txqueuelen 1000 (Ethernet)
RX packets 3617 bytes 2818640 (2.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1393 bytes 267761 (261.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#主要信息
inet 192.168.253.129 # host IP address 宿主ip
netmask 255.255.255.0 # mask
broadcast 192.168.253.255 # broadcast address 广播
inet6 fe80::be20:fae6:7af2:e4e6 # IPv6 address
ether 00:0c:29:6b:e2:63 # Mac address mac地址
yum命令(安装)
[root@localhost lei]# 💹 yum install tree
Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/tree-1.6.0-10.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for tree-1.6.0-10.el7.x86_64.rpm is not installed
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : “CentOS-7 Key (CentOS 7 Official Signing Key) security@centos.org”
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1Installed:
tree.x86_64 0:1.6.0-10.el7Complete!
tree(yum安装后使用)
[root@localhost lei]# ls
aaaa date_log
[root@localhost lei]# tree 💹💹💹 使用tree后结构很清晰
.
├── aaaa
│ ├── bb
│ └── cc
└── date_log
3 directories, 1 file
过滤器
sort - 排序(行)
-f :忽略大小写的差异,例如 A 与 a 视为编码相同;
-M :以月份的名字来排序,例如 JAN, DEC 等等的排序方法;
-n :使用『纯数字』进行排序(默认是以文字型态来排序的);
-r :反向排序;
-u :就是 uniq ,相同的数据中,仅出现一行代表;
-k :以哪个区间 (field) 来进行排序
eg
# cat /etc/passwd | sort
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi-autoipd:x:100:156:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
......
eg
[lei@localhost ~]$ cat sort_example
123 bec Fgi
89 aie des
009 ia;di FIAJ
[lei@localhost ~]$ sort sort_example
009 ia;di FIAJ
123 bec Fgi
89 aie des
[lei@localhost ~]$ sort -n sort_example
009 ia;di FIAJ
89 aie des
123 bec Fgi
[lei@localhost ~]$ sort -k2 sort_example
89 aie des
123 bec Fgi
009 ia;di FIAJ
[lei@localhost ~]$ sort -k2r sort_example
009 ia;di FIAJ
123 bec Fgi
89 aie des
[lei@localhost ~]$ cat sort_uni
abc
ced
ABC
123
abc
CED
[lei@localhost ~]$ sort sort_uni
123
abc
abc
ABC
ced
CED
uniq - 去重(行)
选项与参数:
-
-i :忽略大小写字符的不同;
-
-c :进行计数
-
-u :只显示唯一的行
-
-d, --repeated 只打印重复出现的行
-D 打印所有重复行
[root@localhost test]# cat a.txt
Happy Birthday
Happy Birthday
Merry Christmas
Merry Christmas
Merry Christmas
Happy Valentine's Day
Happy Valentine's Day
Happy Birthday
[root@localhost test]# uniq a.txt
Happy Birthday
Merry Christmas
Happy Valentine's Day
Happy Birthday
[root@localhost test]# uniq -c a.txt
2 Happy Birthday
3 Merry Christmas
2 Happy Valentine's Day
1 Happy Birthday
[root@localhost test]# uniq -d a.txt
Happy Birthday
Merry Christmas
Happy Valentine's Day
#uniq 的功能是对于连续重复行只显示一次,但是最后一行并不是连续的,所以没有做处理,还是打印出来了。怎么不打印它呢?可以与 sort 命令配合使用:
[root@localhost test]# sort a.txt | uniq
Happy Birthday
Happy Valentine's Day
Merry Christmas
comm(比较)
💹去看宣哥的笔记把
tr - 替换指定的字符⭕
不指定参数时,即表示替换指定的字符为另一个字符,支持指定的字符集合。
- -d, --delete:删除指定的字符
- -s, --squeeze-repeats:缩减连续重复的字符成指定的单个字符
eg
将文件testfile中的小写字母全部转换成大写字母:
cat testfile |tr a-z A-Z
或
cat testfile |tr [:lower:] [:upper:]
缩减连续重复的字符成指定的单个字符:
python@xxx:~/test$ cat t
dddddddsssssdd
eeeeeeeeee
aaaaaaaaaaaaaa
vvvvvvvvvvvvvv
python@xxx:~/test$ cat t|tr -s 'se'
dddddddsdd
e
aaaaaaaaaaaaaa
vvvvvvvvvvvvvv
python@xxx:~/test$ cat t|tr -s 'sdeav'
dsd
e
a
v
删除指定的字符:
python@xxx:~/test$ cat t|tr -d 'dv' #删除指定字符dv
sssss
eeeeeeeeee
aaaaaaaaaaaaaa
grep 文本搜索(行)
💹Linux系统中grep命令是一种强大的文本搜索工具,grep允许对文本文件进行模式查找。如果找到匹配模式, grep打印包含模式的所有行。
- -i 不区分大小写的匹配方式
- -v 匹配不包含字符串的行 🤷♂️verse相反的---->不匹配
- -A1 B1 C1
-A显示符合匹配样式的行,并显示该行之后的内容
-B 显示符合匹配样式的行,并显示该行之前的内容
-C 显示符合匹配样式的行,并显示该列之前和之后的内容
test
grep [-选项] '搜索内容串' 文件名 #相当于ctrl +F
💹最好引号或双引号括起来。例如:grep 'a' 1.txt
[root@localhost test]# cat a.txt
HAppy Birthday
Happy Birthday
Merry Christmas
Merry Christmas
Merry Christmas
lower merry
lower merry
mErRy
[root@localhost test]# grep "Merry" a.txt
Merry Christmas
Merry Christmas
Merry Christmas
[root@localhost test]# grep -i "Merry" a.txt 💹-i忽略大小写匹配
Merry Christmas
Merry Christmas
Merry Christmas
lower merry
lower merry
mErRy
[root@localhost test]# grep -v "Merry" a.txt -v💹是匹配不上的显示出来
HAppy Birthday
Happy Birthday
lower merry
lower merry
mErRy
⭕⭕⭕#看宣哥ppt29页
另外那些-A1 after显示之后的一行 -B2显示之前的两行 before -C3 显示前后不匹配的三行
正则表达式在grep中的应用💹
⭕宣哥31页
grep [-opt] "string(正则表达式)" a.txt
字符范围 [ a-z]
在字符集中,指定一个字符范围
-
“[a-z]” matches any lowercase letter 匹配小写a-z
-
“[a-zA-Z0-9]” (匹配任何小写或大写字母或数字)
Anchors⭕未
末尾那些知识必考
❗❗❗character range
linuxLecture6
宣哥的ppt36页
1.process进程
- ProcessProcess means a program is in execution,Different from thread
- Process是指程序正在执行中,而thread是指进程的一部分。进程不是轻量级的,而线程是轻量级的。
- 进程需要更多的时间来终止,线程需要更少的时间来终止。
- Process需要更多的时间创建,而Thread需要更少的时间创建。
- 进程通常是独立的,而线程则共享内存。
- 进程不共享数据,和线程之间共享数据。
ps
● -e 显示所有进程 | |
---|---|
● -f 完整格式显示 | |
● -u 详细显示💹 | |
● -u username 显示username的进程 | |
● -p pid 显示 PID 的信息 | |
ps -ef 以完整格式显示所有进程 | |
ps -eH 以树的形式显示进程 | |
pstree 以树的形式显示出来 |
ps -f 查看有父子关系的进程
ps的内容
- UID/USER 此进程所属的 UID/用户
- PID 进程标识
- PPID 父进程标识
- %CPU 处理器占用百分比
- %MEM 内存占用百分比
- STAT 进程状态
pstree 以树状图显示
pstree Display processes in a tree format
top命令
top 交互式流程查看器,显示最活跃(消耗的资源)流程(类似于windows的任务管理器)💹💹💹
- -d # 刷新间隔 # 秒
- P 按CPU占比排序 (默认)
- M 按内存占比排序
- P/M 是热键,不是选项
top -d 1 隔一秒刷新
后台和前台进程常规命令在前台执行任何时候都只有一个前台进程命令与在后台启动命令。
- ctrl - c关闭前台进程。
- ctrl - z挂起前台进程(放到后台)
❗❗❗同一时间只能有一个foreground , background可以有无数个
常规命令在前台执行
任何时候都只有一个前台进程
jobs
查看当前有多少在后台运行的命令,只能是当前终端运行的后台命令,关闭之后只能用top或者ps找了。
jobs -l
可显示所有任务的PID,jobs的状态可以是running, stopped, Terminated。但是如果任务被终止了(kill),shell 从当前的shell环境已知的列表中删除任务的进程标识。
fg #
将数字 # 作业置于前台
终止进程:kill
kill命令指定进程号的进程,需要配合 ps 使用。
使用格式:
kill [-signal] pid
#常用到的命令
kill PID
kill -9 PID
此时发送的是SIGKILL信号。正如前文所述,SIGTERM信号不一定能够“杀死”目标进程,在这种情况下,我们就会释放大杀器,SKGKILL信号。SIGKILL信号不会被进程所“截获”,它只能由主机系统内核处理,由其负责提供可靠的控制进程执行的方法,SIGKILL会杀死进程。
信号值从0到15,其中9为绝对终止,可以处理一般信号无法终止的进程。
❗❗❗-9和-15有什么区别
kill -9 是直接kill杀死
kill- 15 是先close file ,然后再kill
at
💹在指定的时间执行一次命令
systemctl enable atd #要启用该服务(如果未激活)
每行一个命令按Shift+Ctrl+d 退出任务设置
at time
去开始设置任务(输入命令)
❗在 at> 环境中,每行一个命令
Shift+Ctrl+d
退出任务设置
at 选项
-l 用于列出所有任务
-d # 删除任务 #
-c # 查看任务 #
[root@localhost test]# touch at.txt
[root@localhost test]# at 00:35
at> ls>at.txt 💹#这里就是一条命令,将ls 中a at.txt 赋值到at.txt里面
at> <EOT>
job 2 at Tue Dec 6 00:35:00 2022
[root@localhost test]# at -l
2 Tue Dec 6 00:35:00 2022 a root
[root@localhost test]# cat at.txt 💹这是时候已经有了
a
at.txt
[root@localhost test]# ls
a at.txt
❗❗❗at和cron有什么区别:
at只能做一次,cron可以做很多次
Cron⭕
crontab命令的基本格式如下:
crontab [选项]
crontab [选项] file
- cron -基于时间的作业调度服务,用于重复作业。
- crontab -一个创建、读取、更新和删除作业计划的程序。
- 使用cron来调度和自动化任务。
systemctl enable crond (先开启自动执行服务)
-
crontab file(Install a new crontab from file.)
-
crontab -l (List your cron jobs.)
-
crontab -e (Edit your cron jobs.)
-
crontab -r (Remove all of your cron jobs.)
Crontab Format
❗❗❗就是考下面这幅图
❗❗❗cron每个星期、每个星期执行一次reboot,写个cron
背一下星期一到星期天
test
可以用crontab -e命令来编辑/var/spool/cron下对应用户的cron文件,也可以直接编辑/etc/crontab。
总结:系统级别的定时任务crontab文件是/etc/crontab,用户自定义的定时任务其crontab文件保存在 /var/spool/cron/ 目录下,名字和用户名一致。
用户任务调度:用户自定义的定期要执行的工作,用户可以使用 crontab 工具来定制自己的计划任务,所有用户自定义的crontab 文件都被保存在 /var/spool/cron目录中。其文件名与用户名一致,例如我用test用户设置了一个定时任务,那么其对应的crontab文件就是 /var/spool/cron/test。
#每天4:22以root身份执行/etc/cron.daily目录中的所有可执行文件,run-parts参数表示,执行后面目录中的所有可执行文件。添加后保存的路径是/var/spool/cron,可以直接用vi查看和修改。
Package (Software) Management
1.rpm
-
RPM (RPM Package Manager)是Red Hat Linux及其衍生产品如CentOS和Fedora所使用的一个功能强大的包管理系统。
-
RPM也指RPM命令和。RPM文件格式。
-
RPM包由文件和元数据的存档组成,包括依赖关系和安装位置等信息。
2.yum
- YUM (Yellowdog Updater Modified)是一个开源软件基于RPM (RedHat package Manager)的Linux系统的包管理工具。
- 它允许用户和系统管理员在系统上轻松地安装、更新、删除或搜索软件包。
- YUM使用了大量第三方党库通过解决包的依赖关系问题来自动安装包。。
❗❗❗rpm和yum区别
rpm要手工解决依赖,yum自动解决
❗❗❗解释dependencies,就是安装这个也要安装那个
yum repo
❗❗❗/etc/yum.repos.d/???.repo 这个是-yum的一个配置文件,告诉你去哪找这些package
Specifies where to find packages when use yum
❗❗❗[lei@localhost ~]$ cd /etc/yum.repos.d/ ⭕后面记得加/斜杆 对比 /etc/passwd 是没有的,他是一个文件,repos.d有些干是个文件夹
-
yum install package_name
install the package with all dependencies
-
yum install -y package_name
install automatically, no question asked
-
yum remove package_name
remove a package completely with their all dependencies
-
yum updatepackage_name
update it to the latest stable version
-
yum list name
exact name match
-
yum search name
search for packages that are name related
1.列出所有可更新的软件清单
命令:#yum check-update
2.安装所有更新软件
命令:#yum update
3.仅安装指定的软件
命令:#yum install <package_name>
4.仅更新指定的软件
命令:#yum update <package_name>
5.列出所有可安裝的软件清单
命令:#yum list
Interaction Commands (from L2) 💹💹💹✔✔✔
You may learn the following command by your self, through reading and practicing
cp (a)
mv (b)
rm ©
rmdir (d)
❗❗❗这个地方不太好出题
❗❗❗ssh命令是安全的不,—>是安全的
❗❗❗通过什么用户名,通过哪个ip 登录 (ssh user_name @ip)
❗❗❗
The authenticity of host '192.168.253.129 (192.168.253.129)' can't be established.
ECDSA key fingerprint is SHA256:K+PksdW+caAWI8odi7kOmrHLECy5/3nHLibu+UV7tIg.
ECDSA key fingerprint is MD5:4a:0f:78:60:4f:af:fc:31:23:d0:0a:66:ab:53:7a:be.
Are you sure you want to continue connecting (yes/no)? #yes 💹💹💹第一次登录的认证
ssh免密登陆
Remote login
SSH或Secure Shell是一种远程管理协议,允许用户通过Internet控制和修改远程服务器。该服务是作为未加密的Telnet的安全替代品而创建的,它使用加密技术来确保所有与远程服务器的通信都在加密的环境中进行的方式。它提供了一种对远程用户进行身份验证的机制,将输入从客户机传输到主机,并将输出转发回客户机。
- ssh sever_ip 作为相同名称的用户连接(在客户端使用)
- ssh user_name@sever_ip 以用户名连接 (比如想用root用户登录)
第一次连接
The authenticity of host '192.168.253.129 (192.168.253.129)' can't be established.
ECDSA key fingerprint is SHA256:K+PksdW+caAWI8odi7kOmrHLECy5/3nHLibu+UV7tIg.
ECDSA key fingerprint is MD5:4a:0f:78:60:4f:af:fc:31:23:d0:0a:66:ab:53:7a:be.
Are you sure you want to continue connecting (yes/no)? 💹yes
Remote login via SSH
SSH或Secure Shell是一种远程管理协议,允许用户通过Internet控制和修改远程服务器。该服务是作为未加密的Telnet的安全替代品而创建的,它使用加密技术来确保所有与远程服务器的通信都在加密的环境中进行的方式。它提供了一种对远程用户进行身份验证的机制,将输入从客户机传输到主机,并将输出转发回客户机。
拷贝文件:cp
(78条消息) Linux cp命令详解_辣条好辣的博客-CSDN博客_linux cp命令详解
cp命令的功能是将给出的文件或目录复制到另一个文件或目录中,相当于DOS下的copy命令。
选项
cp vim_config/
移动更名文件:mv
mv命令用来移动 文件或目录,也可以给文件或目录****重命名。
常用选项说明:
选项 | 含义 |
---|---|
-f | 禁止交互式操作,如有覆盖也不会给出提示 |
-i | 确认交互方式操作,如果mv操作将导致对已存在的目标文件的覆盖,系统会询问是否重写,要求用户回答以避免误覆盖文件 |
-v | 显示移动进度 |
改名
mv item1 item2 #把文件或目录"item1”移动或重命名为"item2”,或者:
touch创建空文件/更改文件时间
9创建多个文件
touch a.txt b.txt
touch /home/{a.txt,b.txt}
删除文件:rm
rm -i cat.txt #-i会提示是否删除
rm -f dog.txt #强行删除
rm -v dog2.txt #有一个删除成功都信息提示
mkdir bbb
touch bbb/text.txt #创建一个文件
rm bbb #这样是删除不了一个文件夹,但是可以删除文件txt那种
rm -r bbb #这样才能删除文件夹 ,递归地删除目录下的内容,删除文件夹时必须加此参数
rmdir删除空文件
mkdir test test1 test2
rmdir test1 #这样是不能删除,因为里面为空
rmdir -pv test1/test11/test111 #递归删除test1里面的所有文件,
- 命令删除⏩⏩⏩空的目录
将工作目录下,名为 AAA 的子目录删除 :
rmdir AAA
在工作目录下的 BBB 目录中,删除名为 Test 的子目录。若 Test 删除后,BBB 目录成为空目录,则 BBB 亦予删除。
rmdir -p BBB/Test
(51条消息) scp命令:linux上从远程服务器拷贝文件/目录到本地服务器_大虾头J的博客-CSDN博客_scp从远程复制到本地
远程文件复制:scp
1.命令格式:
scp [参数] [原路径] [目标路径]
先远程登录
ssh sever_ip # connect as the same name user (used at client)
ssh user_name@sever_ip #connect as user_name , 比如ssh root@192.168.19.100
scp 命令用于 Linux 之间复制文件和目录,scp是 secure copy 的缩写是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。
scp 是加密的,rcp 是不加密的,scp 是 rcp 的加强版。
使用scp命令要确保使用的用户具有可读取远程服务器相应文件的权限,否则scp命令是无法起作用的。
从本地复制到远程命令格式:
复制文件
scp local_file remote_username@remote_ip:remote_folder
或者
scp local_file remote_username@remote_ip:remote_file
或者
scp local_file remote_ip:remote_folder
或者
scp local_file remote_ip:remote_file
复制文件夹
scp -r local_folder remote_username@remote_ip:remote_folder
或者
scp -r local_folder remote_ip:remote_folder
实例:
scp /home/space/music/1.mp3 root@xiaoxiaoming.xyz:/home/root/others/music
scp /home/space/music/1.mp3 root@xiaoxiaoming.xyz:/home/root/others/music/001.mp3
scp /home/space/music/1.mp3 xiaoxiaoming.xyz:/home/root/others/music
scp /home/space/music/1.mp3 xiaoxiaoming.xyz:/home/root/others/music/001.mp3
scp -r /home/space/music/ root@xiaoxiaoming.xyz:/home/root/others/
scp -r /home/space/music/ xiaoxiaoming.xyz:/home/root/others/
从远程复制到本地:
scp root@xiaoxiaoming.xyz:/home/root/others/music /home/space/music/1.mp3
scp -r xiaoxiaoming.xyz:/home/root/others/ /home/space/music/
-P 参数来设置命令的端口号:
#scp 命令使用端口号 4588
scp -P 4588 remote@xiaoxiaoming.xyz:/usr/local/sin.sh /home/administrator
一、示例
1、从本地复制到远程
在本地服务器上将/root/lk目录下所有的文件传输到服务器43.224.34.73的/home/lk/cpfile目录下,命令为:
scp -r /root/lk root@43.224.34.73:/home/lk/cpfile
2、从远程复制到本地
在本地服务器上操作,将服务器43.224.34.73上/home/lk/目录下所有的文件全部复制到本地的/root目录下,命令为:
scp -r root@43.224.34.73:/home/lk /root
lecture7
echo (相当于print)
Linux echo详解:输出命令, 常见用法
python@ubuntu:~$ echo -e "hello\t\t world!" 解析转义字符
hello world!
python@ubuntu:~$ echo -E "hello\t\t world!" 不解析转义字符
hello\t\t world!
python@ubuntu:~$ echo $a 输出环境变量
b
Shell编程 .sh
这里说的Shell 脚本(shell script),是在Linux 环境下运行的脚本程序
Shell 编程跟 JavaScript、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。
#Create a shell script (text file)
vim my_shell.sh
#Put the following into my_shell.sh
#!/bin/bash
echo "hello world"
#Execute the script with sh command
sh my_shell.sh
#!/bin/bash
#she-bang, specifies bash as the interpreter
echo "hello world"
#statement or command, tell the interpreter what to do
sh my_shell.sh
#shell executing command, to run the shell script
执行方式一
sh my_shell.sh 别忘了后缀 .sh
执行方式二:(先chmod +x a.sh)
./my_shell.sh 别忘了后缀 .sh
test:
[chentainxi@localhost test]$ ls -l
total 8
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 30 05:39 aaa.txt
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
drwxrwxr-x. 2 chentainxi chentainxi 6 Nov 30 05:40 bb
-rw-rw-r--. 1 chentainxi chentainxi 29 Nov 30 05:47 my_shell.sh💹#这里权限只有rw读和写,没有执行x
[chentainxi@localhost test]$ ./my_shell.sh #./my_shel.sh 这种方式需要权限
bash: ./my_shell.sh: Permission denied
[chentainxi@localhost test]$# chmod +x my_shell.sh
[chentainxi@localhost test]$ ls -l
total 8
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 30 05:39 aaa.txt
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
drwxrwxr-x. 2 chentainxi chentainxi 6 Nov 30 05:40 bb
-rwxrwxr-x. 1 chentainxi chentainxi 29 Nov 30 05:47 my_shell.sh #添加-rwxrwxr-x权限了,可以执行
[chentainxi@localhost test]$ ./my_shell.sh
life is fucking movie
[chentainxi@localhost test]$
1、作为可执行程序
chmod +x test.sh #使脚本具有执行权限
.test.sh #执行脚本 ,之后会在下面打印hello world
⏩⏩⏩💹💹💹一定要写成 ./test.sh,而不是 test.sh,
Shell 注释 (comment)
1.单行注释 Single line comments
#!/bin/bash
# my comment 1
# my comment 2
# my comment N
2.多行注释(Multi-Line comments) ❗❗❗不考试内容
#!/bin/bash
echo "Say Something"
<<COMMENT1
your comment 1
comment 2
blah
COMMENT1
echo "Do something else"
Shell变量(Variables)
1.定义变量
your_name="taobao.com"
2.使用变量
💹💹💹在变量名前面加美元符号即可,如下两种使用方式
- $VariableName
- ${VariableName}
#Use $VariableName or ${VariableName}
your_name="qinjx"
echo $your_name #在前面加$
echo ${your_name} #或者在前面加#{ }💹💹💹最好用这个
#如果不使用# 或者 #{ } linux会认为是一个命令,不是变量
区别$VariableName or ${VariableName}
[lei@localhost ~]$ cat my_shell.sh
#!/bin/bash
MY_SHELL="bash"
echo "I like the $MY_SHELL shell."
echo "I like the ${MY_SHELL} shell."
echo "I am $MY_SHELLing on my keyboard." 💹#这样写是错误的,后面有ing字符跟这个要用#{ } 使用这个变量
echo "I am ${MY_SHELL}ing on my keyboard."
[lei@localhost ~]$ sh my_shell.sh
I like the bash shell.
I like the bash shell.
I am on my keyboard. 💹#此处输出就不能识别变量#MY_SHELL,
I am bashing on my keyboard.
环境变量
#Try echo the following
echo $HOSTNAME
echo $USER
echo $UID
echo $USERNAME
echo $SHELL
echo $HOME
echo $PWD
echo $PATH
🟢🟢🟢绿色表示可执行文件、 🔵🔵🔵 蓝色表示文件目录directory
[chentainxi@localhost test]$ a=$USER #$USER 是 chentianxi
[chentainxi@localhost test]$ echo $a
chentainxi
[chentainxi@localhost test]$ echo "$ashi" #要用${a}
[chentainxi@localhost test]$ echo "${a}shi" #拼接上了
chentainxishi
[chentainxi@localhost test]$
test
[..]$ echo "the current directory is $(pwd)"
the current directory is /home/lei
[..]$ echo "the current directory is \"$(pwd)\""
the current directory is "/home/lei"
[..]$ echo "the current time is `date`"
the current time is Tue Nov 2 13:37:20 EDT 2021
PATH
❗❗❗写一个路径,加到PATH里面, -----》 PATH=$PATH: 路径
echo $PATH
#向$PATH添加新路径
PATH=$PATH:NewPath
eg:
PATH=$PATH:/home/user/
❌PATH=NewPath 错误添加
$PATH找到可执行文件的方法
[chentianxi@localhost ~]$ echo $PATH #找到所有bin 二进制可执行文件
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/chentianxi/.local/bin:/home/chentianxi/bin
试一下这个⭕
宣哥ppt51页
Try the following
echo $PATH
ls /usr/bin
ls /usr/bin | grep "^ls„
ls /usr/bin | grep "^mk"
使用变量 $
在变量名前面加美元符号即可,如:
your_name="qinjx"
echo $your_name
echo ${your_name}
加花括号可以帮助解释器识别变量的边界,比如:
for skill in Ada Coffe Action Java; do
echo "I am good at ${skill}Script"
done
只读变量 readonly
使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。
下面的例子尝试更改只读变量,结果报错:
python@ubuntu:~/shell$ myUrl="http://www.google.com"
python@ubuntu:~/shell$ readonly myUrl
python@ubuntu:~/shell$ myUrl="http://www.baidu.com"
-bash: myUrl: 只读变量
删除变量unset
使用 unset 命令可以删除变量,但不能删除只读变量:
#!/bin/sh
myUrl="http://www.baidu.com"
unset myUrl
echo $myUrl
❗❗❗这个是最重要的内容,绝对考到
python@ubuntu:~$ pwd
python@ubuntu:~$ mycurrentpath = $(pwd)
python@ubuntu:~$ echo $mycurrentpath
$(command) or command
$(command) 提取command的结果 :
⭕区别使用 V a r i a b l e N a m e o r ∗ ∗ VariableName or ** VariableNameor∗∗{VariableName}** 中括号是 v a r 是提取变量,而小括号 ∗ ∗ {var}是提取变量,而小括号** var是提取变量,而小括号∗∗(** command**)**是提取command的结果
test
例如:
CurDir=$(pwd)
echo $CurDir
date
CurDate=date
echo $CurDate
[root@localhost test]# echo "current $(date)" ⭕一定要小写呀
current Tue Dec 6 01:49:36 CST 2022
[root@localhost test]# echo "current \"$(date)\"" 💹\" \" 是s双引号
current "Tue Dec 6 01:49:48 CST 2022"
[root@localhost test]# echo "current \"`pwd`\""
current "/home/chentainxi/test"
[root@localhost test]#
单引号和双引号
- 双引号是语法
- 单引号值字符串
“” 允许 V a r 、 Var、 Var、{Var}、$(cmd) 和 ‘cmd’ 在里面运行
’ ’ 排除任何元字符,按原样获取内部的任何内容
echo -e
使用echo -e,可以使 用 “\n换行符” 和 “\t制表符、 \ " 双引号符 ”
字符切片
[lei@localhost ~]$ Str='abcdefgh'
[lei@localhost ~]$ echo ${#Str}
8
[lei@localhost ~]$ echo ${Str:1:3}
bcd
LinuxLecture8
Shell scripting (programming)
这个章节主要讲shell命令
****((算数操作))
💹💹💹注意:echo $(( )) 要两个符号
[chentainxi@localhost ~]$ x=5
[chentainxi@localhost ~]$ echo $(( x++ ))
5
[chentainxi@localhost ~]$ echo $x
6
read(键盘读取)
- -p 后面跟提示信息,即在输入前打印提示信息。
- 注意: read输入的时候变量放在外面不用加$, echo 输出的时候放在“"Number1 - n 1 " ”里面使用变量加 n1"” 里面使用变量加 n1"”里面使用变量加
[chentainxi@localhost ~]$ read num
4
[chentainxi@localhost ~]$ echo $num #注意使用变量呀加$符号
4
可以使用先打印前面Enter your name在输入给变量name
[chentainxi@localhost ~]$ read -p "Enter your name : " name
#Enter your name : hhhhhhhhhhh
[chentainxi@localhost ~]$ echo $name
#hhhhhhhhhhh
[chentainxi@localhost ~]$ echo "hi , $name.let us be friends" #使用$变量打印
hi , hhhhhhhhhhh.let us be friends
-
-t 后面跟秒数,定义输入字符的等待时间。
-
Hiding input option -s (隐藏你的输入)
read -s -p "Enter Password : " my_password
-
-p 后面跟提示信息,即在输入前打印提示信息。
if / elif / else
💹💹💹这里的if 是大括号 【 】 下面还有一个then 然后还有结尾还有个fi
if [ condition-is-true ] 大括号
then 💹有个then
commands
elif [ condition-is-true ] elif
then
commands
else
commands
fi fi结束
Example
#!/bin/bash
MY_SHELL="csh"
if [ "$MY_SHELL" = "bash" ]
then
echo "You seem to lie=ke the bash shell."
elif [ "$MY_SHELL" = "csh" ]
then
echo "You seem to like the csh shell."
else
echo "You don't seem to like the bash or csh shells."
fi
逻辑not /or / and
NOT
if [ ! Statement ]
OR
[ Statement1 -o Statement2 ]
[[Statement1 || Statement2 ]] #第二种
AND
[ Statement1 -a Statement2 ]
[[Statement1 && Statement2 ]] 💹💹💹#注意是两个大括号【【 】】
算术运算符
下表列出了常用的算术运算符,假定变量 ⭕a 为 10,变量 b 为 20:
⭕⭕⭕💹💹💹这里expr是返回数值1/2/3,下面关系运算符是返回false/ture
Example
💹💹💹 注意这个expr表达式和两个符号
#!/bin/bash
a=10
b=20
[chentainxi@localhost ~]$ val=`expr $a + $b` ⭕⭕⭕💹💹💹#这里一定有 `expr ` 符号和两个变量表达式
[chentainxi@localhost ~]$ echo $val
30
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a 等于 b"
fi
if [ $a != $b ]
then
echo "a 不等于 b"
fi
关系运算符
关系运算符💹💹💹只支持数字,不支持字符串,除非字符串的值是数字。
下表列出了常用的关系运算符,假定⭕变量 a 为 10,变量 b 为 20:
-gt—>greater than大于
-lt---->lower than 小于
-ge grater equer 大于等于
Example
#!/bin/bash
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a 等于 b"
else
echo "$a -eq $b: a 不等于 b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a 不等于 b"
else
echo "$a -ne $b : a 等于 b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a 大于 b"
else
echo "$a -gt $b: a 不大于 b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a 小于 b"
else
echo "$a -lt $b: a 不小于 b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a 大于或等于 b"
else
echo "$a -ge $b: a 小于 b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a 小于或等于 b"
else
echo "$a -le $b: a 大于 b"
fi
[chentainxi@localhost ~]$ if [1 -eq 1]
> then echo "sssss"
> fi
bash: [1: command not found... #if [1 -eq 1]是错误的,不可以常量,要变量
#老师的ppt
[lei@localhost ~]$ [ 5 -gt 2 ] && echo Y || echo N #这里相当于java 5 > 2 ? y:2
Y
[lei@localhost ~]$ [ 5 -gt 22 ] && echo Y || echo N
N
[lei@localhost ~]$ arg1=6
[lei@localhost ~]$ [ $arg1 -gt 22 ] && echo Y || echo N
N
[lei@localhost ~]$ [ m -gt 22 ] && echo Y || echo N
bash: [: m: integer expression expected
N
文件测试运算符
-
-e file 文件是否存在
-
-s file 如果文件存在和文件不为空 --返回true
-
-d file 文件是否为一个目录directory
-
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是 ,则返回 true
-
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。 -w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。 -x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true Example
⭕file 表示文件 /var/www/runoob/test.sh,它的大小为 100 字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性: #!/bin/bash file="/var/www/runoob/test.sh" if [ -r $file ] then echo "文件可读" else echo "文件不可读" fi if [ -w $file ] then echo "文件可写" else echo "文件不可写" fi if [ -x $file ] then echo "文件可执行" else echo "文件不可执行" fi if [ -f $file ] then echo "文件为普通文件" else echo "文件为特殊文件" fi if [ -d $file ] then echo "文件是个目录" else echo "文件不是个目录" fi if [ -s $file ] then echo "文件不为空" else echo "文件为空" fi if [ -e $file ] then echo "文件存在" else echo "文件不存在" fi 文件可读 文件可写 文件可执行 文件为普通文件 文件不是个目录 文件不为空 文件存在
字符串测试
参数 | 说明 |
---|---|
= | 等于则为真 |
!= | 不相等则为真 |
-z 字符串 | 字符串的长度为零则为真 zero |
-n 字符串 | 字符串的长度不为零则为真 number |
num1="ru1noob"
num2="runoob"
if test $num1 = $num2 💹💹💹#可以加个test就不用 【】
then
echo '两个字符串相等!'
else
echo '两个字符串不相等!'
fi
test
条件测试语法:
- ①test表达式:[ condition-to-test ]
- ②test命令: test condition-to-test
③Example:
判断文件是否存在
a)[ -e /etc/passwd ]
test -e /etc/passwd
老师的ppt
[lei@localhost ~]$ ls
bb date_log my_shell.sh
💹#因为这里没有空格,所以表达式不会,但是这个文件是存在的
[lei@localhost ~]$ [-e date_log] && echo 'Y' || echo 'N' # [-e date_log] -e判断是否是exist存在
bash: [-e: command not found...
N
[lei@localhost ~]$ [ -e date_log ] && echo 'Y' || echo 'N' #date_log这个文件是否存在
Y
[lei@localhost ~]$ [ -e date_logo ] && echo 'Y' || echo 'N'
N
[lei@localhost ~]$ test -e date_logo && echo 'Y' || echo 'N'
N
[lei@localhost ~]$ test -e date_log && echo 'Y' || echo 'N'
Y
可以看到test命令和表达式[ condition-to-test ]作用相同。
test xxx 可以简写成 [ xxx ] 的形式。 ⭕【一定要留一个空格 】
注意:在使用"["简写test时,左中括号后面的空格和右括号前面的空格是必需的,如果没有空格,Shell不可能辨别表达式何时开始何时结束.
if test …
这种格式是经常用的
case statement (switch-case结构)
类似于java的switch结构
Example
#!/bin/bash
echo 'input a number between 1~4:'
read aNum
case $aNum in
1) echo 'your input is 1'
;;
2) echo 'your input is 2'
;;
3) echo ' your input is 3'
;;
4) echo ' your input is 4'
;;
*) echo 'your input is not in 1~4' ;;
esac
语法
case $variable-name in
pattern1)
commands
;;
pattern2)
commands
;;
patternN)
commands
;;
*) # all other cases
commands
;;
esac #推出
LinuxLecture9
Array(数组)
- bash支持一维数组(不支持多维数组),并且没有限定数组的大小。
- 数组元素的下标由 0 开始编号。
- 可以不同类型的成员,
- 比如 arr[0]=“陈” arr[1]=10
- 数组的下标不一定是连续的 可以先赋值arr[0] arr[1] arr[200] 他们是链表实现的,不是连续给200个空间
⭕⭕⭕linux中空格可能是分隔符用的
定义数组
在 Shell 中,用括号来表示数组,数组元素用"空格"符号分割开。定义数组的一般形式为:
array_name=(value0 value1 value2 value3) #这里是小括号
或者
array_name=(
value0
value1
value2
value3
)
读取数组${ }
读取数组元素值的一般格式是:
valuen=${array_name[n]}
test
[ chentainxi@localhost -1$ my arr=(1 2 'hello' hello kitty)
[chentainxi@localhost -]$ echo $[my_arr[4]]
kitty
[chentainxi@localhost -]$ my_arr=(1 2 'hellol' "hello kitty") #双引号
[chentainxi@localhost -]$ echo ${my_arr[3]}
hello kitt
⭕⭕⭕环境变量"$PWD" "`date`" PWD环境变量是大写
[chentainxi@localhost ~]$ my_arr=(1 "$PWD" "`date`")
[chentainxi@localhost ~]$ echo ${my_arr[2]}
#Fri Nov 4 01:18:37 +10 2022
[chentainxi@localhost ~]$ echo ${my_arr[1]}
#/home/chentainxi
使用 @
或*
符号可以获取数组中的所有元素
[chentainxi@localhost -]$ my_arr=(1 2 'hello' "hello kitty")
[chentainxi@localhost -]$ echo $[my_arr[*])
1 2 hello hello kitty
[chentainxi@localhost -]$ echo $[my_arr[@])
1 2 hello hello kitty
数组长度#
[chentainxi@localhost -]$ my_arr=(1 2 'hellol' "hello kitty") #双引号
# 取得数组单个元素的长度
lengthn=${#array_name[n]}
[chentainxi@localhost -]$ echo ${#my_arr[2]}
5 #这个是获取数组下标2元素的长度
[chentainxi@localhost -]$ echo ${#my_arr[*]}
4 #这个数组总长度,总共4个元素
for 循环 in
for var in item1 item2 ... itemN #do 和 done包围
do
echo "💹$var"
command2
...
commandN
done
test
[chentainxi@localhost ~]$ for loop in 1 2 3 4 5
do
echo "The value is: $loop" #里面是使用$val 表示循环的变量
done
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
test
#!/bin/bash
# A shell script to verify user password database
files="/etc/passwd /etc/group /etc/shadow /etc/gshdow"
for fil in $files
do
[ -f $fil ] && echo "$f file found" || echo "*** Error - $f file missing."#是个文件就执行前面echo,非文件就执行后面那个
done
#这里三用空格隔开的字符/etc/..串相当于数组 , 这个是老师编的/etc/gshdow, 不存在
[chentainxi@localhost ~]$ files="/etc/passwd /etc/group /etc/shadow /etc/gshdow"
[chentainxi@localhost ~]$ for f in $files
> do
> [ -f $f ] && echo "$f file found" || echo "*** Error - $f file missing."
> done
/etc/passwd file found
/etc/group file found
/etc/shadow file found
*** Error - /etc/gshdow file missing. #这个文件不存在,编出来的
[chentainxi@localhost ~]$ echo {1..5}
1 2 3 4 5
[chentainxi@localhost ~]$ echo {1..10..2}
1 3 5 7 9
[chentainxi@localhost ~]$ echo {aa,bb,cc}{1,2,3} #笛卡尔乘积
aa1 aa2 aa3 bb1 bb2 bb3 cc1 cc2 cc3
❗❗❗考试不会考,了解一下就可以了
#!/bin/bash
my_arr=(hello kitty)
my_arr[5]='Linux class'
for var in "${my_arr[*]}" #加双引号之后没有输出
do
echo $var
done
echo '--------------'
#输出hello kitty Linux class
for var in "${my_arr[@]}"
do
echo $var
done
echo '--------------'
#输出换行
hello
kitty
Linux class
for var in ${my_arr[*]}
do
echo $var
done
echo '--------------'
for var in ${my_arr[@]}
do
echo $var
done
echo '--------------'
C 语言风格for
for (( i = 1; i <= 5; i++ )) ### Outer for loop ###
do
for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ###
do
echo "$i "
done
echo "" #### print the new line ###
done
1
1
1
1
1
2
2
2
2
2
3.......
test
for command in date pwd ls #date pwd ls 分别是不同的变量
do
echo
echo "*** The output of $command command >"
#run command
$command
echo
done
输出
*** The output of date command >
Wed Nov 30 04:52:35 CST 2022 #输出以第一个变量date
*** The output of pwd command >
/home/chentainxi/test #输出以第二个变量 pwd
*** The output of ls command >
aa.txt aa.txt~ a.sh #输出以第三个变量 ls
test 2
for car in bmw ford toyota nissan
do
echo "Value of car is: $car"
done
#输出
Value of car is: bmw
Value of car is: ford
Value of car is: toyota
Value of car is: nissan
while loop
#!/bin/bash
# set n to 1
n=1
# continue until $n equals 5
while [ $n -le 5 ]
do
echo "Welcome $n times."
n=$(( n+1 )) # increments $n
done
#!/bin/bash
n=1
while (( $n <= 5 )) 💹#这种(()) 两个括号的直接用c语言的方法写, 上面的有那个{ -le }
do
echo "Welcome $n times."
n=$(( n+1 ))
done
util
#!/bin/bash
i=1
until [ $i -gt 6 ]
do
echo "Welcome $i times."
i=$(( i+1 ))
done
while和until的区别
- while 和 until 的区别
- until循环执行,直到条件为真跳出循环
- while循环将一直执行,直到条件为假跳出循环
- until循环始终至少执行一次
sh执行 .sh 带参数
执行下面这个命令带参数
💹💹💹 sh script.sh par1 par2 par3
- $0 “script.sh”脚本名称
- $1 第一个参数“par1” 的值
- $2 第二个参数“par2” 的值
- $3 第三个参数“par3” 的值
- $n 第n个参数“parn” 的值
- $# 参数数量
- $* 所有参数的值
- $@ 所有参数的值
test:
[chentainxi@localhost test]$ cat a.sh
echo "Shell 传递参数实例!";
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";
[chentainxi@localhost test]$ sh a.sh 1 2 3 #sh 文件名.sh 参数1 参数2 参数3
Shell 传递参数实例!
执行的文件名:a.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3
test2
[chentainxi@localhost test]$ vim a.sh
echo "The script name : $0"
echo "The value of the first argument to the script : $1"
echo "The value of the second argument to the script : $2"
echo "The value of the third argument to the script : $3"
echo "The number of arguments passed to the script : $#"
echo "The value of all command-line arguments (\$* version) : $*"
echo "The value of all command-line arguments (\$@ version) : $@"
[chentainxi@localhost test]$# sh a.sh 1 2 3 执行
The script name : a.sh
The value of the first argument to the script : 1
The value of the second argument to the script : 2
The value of the third argument to the script : 3
The number of arguments passed to the script : 3
The value of all command-line arguments ($* version) : 1 2 3
The value of all command-line arguments ($@ version) : 1 2 3
‘ * ’ 和 @区别
- 只有" * "将所有参数作为一个字符串。
- "@"将每个参数单独接受,所有参数作为一个单独的列表
Linuxlecture10
💹disk -->是磁盘,比如:sda 、 sdb, 分区是把磁盘分开
(57条消息) Linux中磁盘的管理(格式化、分区、挂载)_Slash · Young的博客-CSDN博客_linux磁盘格式
磁盘管理
lsblk
我在外面添加新的disk 5G大小
Select your VM (left click)
In right click menu:“设置” pick “添加”
“硬件类型” pick“硬盘” and then“下一步”
“虚拟磁盘类型”pick“SATA” or “SCSI” and then always “下一步” tile finish
[root@localhost chentainxi]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 5G 0 disk 💹💹💹刚刚在外面添加的 5G大小disk磁盘
#此时里面有sda和sdb(新加)的两个disk
fdisk 管理
fdisk 是 Linux 的磁盘(sdb)分区表操作工具。 💹新加的disk可以用fdisk来管理
-
-l :输出后面接的装置所有的分区内容。若仅有 fdisk -l 时, 则系统将会把整个系统内能够搜寻到的装置的分区均列出来。
-
/dev/ 这个是设备目录
[root@localhost chentainxi]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sdb [root@localhost chentainxi]#
Disk partitions(磁盘分区)
💹把disk(sdb)磁盘分开空间
比如说我们把磁盘分了sda1、sda2、sda3、sda4盘,我们假设sda1盘为系统盘,其他的比如说游戏、办公、软件盘,这样我们要重新分哪个区就直接在对应的盘分就可以了,不需要整块磁盘进行分区。根据用途我们也能较快的去使用相应的磁盘。
- 主分区primary 最多4个
- 扩展分区extended 最多一个
- 逻辑分区 可以是多个,宝行在扩展分区内
🤢🤢🤢注意:primary + extended <=4
创建一个磁盘分区
fdisk PhysicalPath(物理地址)
例如 fdisk /dev/sdb
①创建一个主分区分区
[root@localhost chentainxi]# cd /
[root@localhost /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
[root@localhost /]# fdisk /dev/sdb #💹关键命令。/dev/sdb是他的物理地址
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x43a17a8f.
Command (m for help):# m
Command action #x下面有很多帮助
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
💹 n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit💹 #分完区之后保存
x extra functionality (experts only)
Command (m for help): #n 输入n添加add分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): #p 选择添加主分区
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +2G 💹#这里写你分区的大小
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help):#保存 W
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
创建成功后查看
#查看方式一
[root@localhost /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bf4bb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x43a17a8f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux 💹💹💹#新加的
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
#查看方式二
[root@localhost /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 5G 0 disk
└─sdb1 8:17 0 2G 0 part 💹💹💹💹#新加的
sr0 11:0 1 4.4G 0 rom /run/media/chentainxi/CentOS 7 x86_64
[root@localhost /]#
②创建拓展分区(只能有一个)
- 创建拓展分区后
- 再创建逻辑分区
[root@localhost /]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): #m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
# n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): #n
Partition type:
p primary (1 primary, 0 extended, 3 free)
💹 e extended
Select (default p): #e
Partition number (2-4, default 2):
First sector (4196352-10485759, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-10485759, default 10485759): #回车使用默认,把所有空间都分配给他
Using default value 10485759
Partition 2 of type Extended and of size 3 GiB is set
Command (m for help): #m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
#💹💹💹创建拓展分区完成之后,可以在拓展分区里面创建多个 逻辑分区
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
💹l logical (numbered from 5)
Select (default p): #l
Adding logical partition 5
First sector (4198400-10485759, default 4198400):
Using default value 4198400
Last sector, +sectors or +size{K,M,G} (4198400-10485759, default 10485759):# +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): # W
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bf4bb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x43a17a8f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 10485759 3144704 5 Extended #这个是上面创建的拓展分区
/dev/sdb5 4198400 6295551 1048576 83 Linux #这个就是上面创建的逻辑分区
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 5G 0 disk
├─sdb1 8:17 0 2G 0 part #创建的拓展分区,本来是3GB的,但是里面创建了逻辑分区占领了1GB
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part #创建的逻辑分区
sr0 11:0 1 4.4G 0 rom /run/media/chentainxi/CentOS 7 x86_64
[root@localhost /]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Command (m for help): #p 打印分区
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x43a17a8f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 10485759 3144704 5 Extended
/dev/sdb5 4198400 6295551 1048576 83 Linux
Command (m for help):
Format a partition格式化分区
例如
mkfs.fsname PartitionPath
mkfs.xfs /dev/sdb1
df -h
获取硬盘被占用了多少空间,目前还剩下多少空间等信息。
语法:
[root@localhost chentainxi]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.6M 478M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 5.4G 12G 32% /
/dev/sda1 1014M 174M 841M 18% /boot
tmpfs 98M 24K 98M 1% /run/user/1000
/dev/sr0 4.5G 4.5G 0 100% /run/media/chentainxi/CentOS 7 x86_64
mount 挂载
在linux中,用户不能直接访问硬件设备,需要将硬件设备挂载到系统目录上,硬件才可以让用户访问到。因此我们需要将格式化后的分区挂载到具体的目录下。
挂载点:访问磁盘/分区的路径
mkdir /mymount #①创建一个挂载点
mount /dev/sdb1 /mymount #②将 /dev/sdb1分区挂载到到 mymount
例子
[root@localhost diskone]# cd ../
[root@localhost disk]# mkdir disktwo //创建被挂载的路径
[root@localhost disk]# ll
总用量 8
drwxr-xr-x. 4 root root 4096 7月 28 17:04 diskone
drwxr-xr-x. 2 root root 4096 8月 1 16:48 disktwo
[root@localhost disk]# mount /dev/sdc1 /disk/disktwo/ //进行挂载
[root@localhost disk]# df -h //再次查看,发现sdc1已经挂载在/disk/disktwo路径下
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 15G 1.5G 92% /
tmpfs 932M 76K 932M 1% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/sdb1 20G 1.2G 18G 7% /disk/diskone
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sdc1 20G 173M 19G 1% /disk/disktwo💹💹💹#这里已经显示挂载成功了,但是当开机会自动丢失
磁盘已经挂载上了
下面设置开机自动挂载
编辑/etc/fstab文件,再最后面添加
/dev/sdc1(磁盘分区) /disk/disktwo(挂载目录) ext3(文件格式)defaults 0 0
vim /etc/fstab
/dev/sdc1(磁盘分区) /disk/disktwo(挂载目录) ext3(文件格式)defaults 0 0
(57条消息) Linux下如何挂载磁盘_三国魏的博客-CSDN博客_linux磁盘挂载
Linuxlecture11
man
man命令来自于英文单词manual的缩写,中文译为帮助手册,其功能是用于查看命令、配置文件及服务的帮助信息。
Up/Down/PgUp/PgDn to roll
h for help
q for quit
按键 用处
空格键 向下翻一页。
[Page Down] 向下翻一页。
[Page Up] 向上翻一页。
[HOME] 直接前往首页。
[END] 直接前往尾页。
/关键词 从上至下搜索某个关键词,如"/linux"。
?关键词 从下至上搜索某个关键词,如"?linux"。
n 定位到下一个搜索到的关键词。
N 定位到上一个搜索到的关键词。
q 退出帮助文档。
eg:
man ls
man vim
help
💹就是从man中吧man的help拉下来,help命令是帮助你查看那些你忘记的命令
help cd
help ls
ls --help
man help
help没有man强大
owner and permissions
🤷♂️
-
’$‘ 普通用户
-
’# ‘ root用户
[root@localhost test]# chmod +wrx a.sh
[chentainxi@localhost test]$
chown
"change owner",💹专门给root用户的
- 所有者默认是创建文件的用户
- 用户和用户组拥有的
- 文件可通过chown更改所有者
格式
chown newuser file
chown newuser:newgroup file
eg:
[root@localhost lei]# ls -l
-rw-r--r--. 1 root root 137 Dec 8 12:05 forlei.sh #更改前💹前 root :表示所有者。后 root : 所属组。组下会有多个用户
[root@localhost lei]# chown lei forlei.sh
[root@localhost lei]# ls -l
-rw-r--r--. 1 💹lei root 137 Dec 8 12:05 forlei.sh #更改后
##所有者变成了user(lei)用户,这时user用户对这个文件就拥有了读、写权限
#rwxr-xr-x 2 root root 4096 06-29 14:30 Test 这行文字表示: test这个文件从属于root组 root用户 创建于:06月29日 大小为:4096 前三个字母:rwx表示root用户拥有次文件的一切权限! 算了,反正你没给分,我就答这么多了!
whatis
whatis命令是用于查询一个命令执行什么功能,并将查询结果打印到终端上,相当于 man -f
[root@localhost ~]# whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
[root@localhost ~]# whatis cp
cp (1) - copy files and directories
cp (1p) - copy files
whereis
以显示命名为str的文件的位置
文件可以是多种类型:
- 二进制(命令/可执行文件)
- 常规文件(日志等)
- 手册文件
[root@server bin]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@server bin]# whereis -b ls
ls: /usr/bin/ls
2. whereis -b/m/s:只展示其中一项。
[root@server bin]# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
[root@server bin]# whereis -s ls
ls:[root@server bin]#
选项
- -b 只搜索二进制文件。
- -m 只搜索手册文件(man)。
- -s 只搜索源文件。
修改文件权限:chmod
修改文件和目录的所有者和所属组
chmod 修改文件权限有两种使用格式:字母法与数字法。
rwx 含义
- r read 表示可读取,对于一个目录,如果没有r权限,那么就意味着不能通过ls查看这个目录的内容。
- w write 表示可写入,对于一个目录,如果没有w权限,那么就意味着不能在目录下创建新的文件。
- x 💹excute 表示可执行,对于一个目录,如果没有x权限,那么就意味着不能通过cd进入这个目录。 ⚠是x, 不是e
语法
chmod [u/g/o/a] [=/+/-] [r/w/x] 文件名
[root@localhost test]# chmod +wrx a.sh 💹全部添加wrx三个权限, 第二个参数 a=rwx是省略的
[root@localhost test]# ls -l
total 8
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
💹 -rwxrwxrwx. 1 root root 26 Nov 28 23:38 a.sh
[chentainxi@localhost test]$ ls -l
total 8
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
#----------. 1 root root 7 Nov 28 23:12 a.sh
[chentainxi@localhost test]$ vim a.sh
[chentainxi@localhost test]$ cat a.sh
#cat: a.sh: Permission denied 💹此时是没有权限的
跟台o=rwx 是更改其他用户的权限
[root@localhost test]# chmod o=rwx a.sh 💹指定o=其他用户
[root@localhost test]# ls -l
-rw-rw-r--. 1 chentainxi chentainxi 24 Nov 14 05:50 aa.txt
-rw-rw-r--. 1 chentainxi chentainxi 0 Nov 14 05:50 aa.txt~
-rwxrwxrwx. 1 root root 7 Nov 28 23:12 a.sh
更改之后就可以wrx了
[chentainxi@localhost test]$ vim a.sh
[chentainxi@localhost test]$ ./a.sh #这是执行a.sh, 里面vim的内容为 echo ‘hello eor’
hello eor
[chentainxi@localhost test]$ cat a.sh
echo 'hello eor'
Linux系统中使用以下命令来查看文件的内容:
- cat 由第一行开始显示文件内容
- tac 从最后一行开始显示
- nl 显示的时候,顺道输出行号
- more 一页一页的显示文件内容
- less与more 类似,但可以往前翻页
- head 只看头几行
- tail 只看尾巴几行
常用命令&快捷键
- sh fileName
快捷键的使用:
- Ctrl+A 将光标移到开头
- Ctrl+C 结束运行程序
- Ctrl+D 结束当前会话
- Ctrl+E 将光标移到末尾
- Ctrl+H 退格键
- Ctrl+L 清除
- Ctrl+R 搜索命令历史记录
- Ctrl+Z 挂起程序
- ctrl + shift + c 复制 (insert+ctrl)
- ctrl + shift + v粘贴 (insert+shift)
注意: tab键可以补全哦~~~~
docker
(50条消息) Docker安装指南——如何在Linux中安装Docker?(最新2022-2 for centOS stream 8)_Mymel_晗的博客-CSDN博客_docker安装
关键词和易错点老师重点
- fdisk -l 中 ,是f 不是ldisk
- array_name=(value0 value1 value2 value3) #这里是小括号,范围下标是0开始
[ chentainxi@localhost -1$ my arr=(1 2 'hello' hello kitty)
[chentainxi@localhost -]$ echo $[my_arr[4]]
-
:echo $(( )) 要两个符号 里面算术+ - * %外面是 $(( ))
-
使用变量都要$ valname, for 里面要 外面echo “ 也要”
-
用户信息的存储位置
账户信息存在 /etc/passwd
密码信息存在 /etc/shadow -
一个disk磁盘分三步, ①partitions 分区 ,②format格式化,③挂载mount (设备对目录的绑定)
-
fdisk /dev/sdb ,/dev是 device设备
-
ifconfig
- ens# 网络配置信息
- virbr0 虚拟网卡,由于libvirtd服务应该被删除
- lo 本地网络
-
cd /etc/sysconfig/network-scripts
/etc/sysconfig/network-scripts/ifcfg-ens# #ifcfg-ens# 不是固定的,有一些有的老师给了上面ifconfig中 ens33# 所以你的文件路径要变成ifcfg-ens33
-
ONBOOT=YES 他在启动的时候能用
-
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.253.129 netmask 255.255.255.0 broadcast 192.168.253.255 inet6 fe80::be20:fae6:7af2:e4e6 prefixlen 64 scopeid 0x20<link> 🤷♂️ ether 00:0c:29:6b:e2:63 txqueuelen 1000 (Ethernet) #这个是mac地址 RX packets 3617 bytes 2818640 (2.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1393 bytes 267761 (261.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
sort -n a.txt 这样的话就是11在2的前面的,,默认字母排序的话的话是2在11的前面
[root@localhost test]# cat a.txt 123 bec Fgi 89 aie des 009 ia;di FIAJ [root@localhost test]# sort a.txt 009 ia;di FIAJ 123 bec Fgi 89 aie des [root@localhost test]# sort -n a.txt 009 ia;di FIAJ 89 aie des 123 bec Fgi
-
执行方式二:(先chmod +x a.sh)这里是sh文件不是txt
-
for loop
for do done
-
算术都扔到双括号里面 (( 5+6)) ⭕还要加**$**符号
-
&& ||
-
if 之后要跟then
fi 结尾还要跟fi
-
read提示用户需要 read -p “请输入” var1 var2 var3
-
read 输入密码就是 -s
-
read 输入 -t 超时
-
case $var in 💹别漏掉in
1 )
;;
-
数组是圆括号,不是方括号
-
下标就是 方括号arr【index】
-
for in
dodone
-
for可以c语言风格,但是要(( i=1; i<5; i++))
-
取变量值的时候是 $var
-
但是使用变量名称赋值不用 —> 直接 var = 2 , ($var相当于拿var的值)
-
定义数组是 arr =(var var2 var3) ,读取的时候也要 ${ arr[0]}
-
在for i in arr KaTeX parse error: Expected '}', got 'EOF' at end of input: {arr[i]} 两个$符号的含义
-
fdisk /dev/sdb #💹关键命令。/dev/sdb是他的物理地址
-
记得一个磁盘的三个:①分区、②格式化、③挂载
作业知识点
assignment1.docx
任务6
第5题
- 里面一定要有 let i=i+1 i++不认识
- 在for var in 里面使用变量的时候 一定要 用 $var
- 在里面 mkdir “${ }_” 一定要用双引号,这个双引号表示里面可以写语法,还有使用变量 ${ } 一定要加上中括号,不然识别不出来
第五题
-
read -p "please inter path" 💹# $path这里就错了这个又不是外面读进来的,这个这是在vim里面时一个变量, ⭕读取变量不用$ ,使用变量的时候才需要
-
ead -p "please inter path:" path if [ -x $path -a -r $path ] [ xxx -a xxx] ⭕大括号里面的-a是 &&逻辑表达式 then 💹# $path在【 】里面依旧是要使用 $, ----》所有使用到变量的是有都要 ${var}/ $var ,但是上面读的时候只是变量定义 echo 'ture' else echo 'false' fi
第六题
❗注意,字符相等和数字相等比较不一样, ① 【 xxx -gt xxx】是数字的 ② 【“${path}” = “exit”】 是字符串的
ctrl +f 搜索 字符串测试 / 算术运算符
read -p "please inter path:" path
until [ "${path}" = "exit" ] ⭕⭕⭕#注意这个until里面都【 】里面的空格很严格,
do
if [ -x $path -a -r $path ]
then
echo 'ture'
else
echo 'false'
fi
read -p "please inter path:" path
done
~
第七题
是可以做出来,但是没有试过冒泡排序,一下代码就是冒泡排序
read -p "please enter numbergroup:" arr
let max=0
for ((i=0;i<${#arr};i++))
do
max=arr[$i]
for((j=$i;i<${#arr};j++)
do
if [ arr[$j] -gt $max ]
let temp=arr[$j]
arr[j]
fi
done
done
echo "the min is $min"
又写错思路
read -p "please enter numbergroup:" arr
let min=arr[0]
for ((i=0;i<${#arr};i++))
do
if $min>arr[$i]
then #这里直接比较就可以,小于min的就将它值赋给min,就可以了, 下面交换值的完全是浪费时间
let temp=${arr[$i]}
arr[$i]=$min
min=$arr[$i]
fi
done
echo "the min is $min"
正确答案
read -p "please enter numbergroup:" -a arr 💹# 一定要 -a 才是输入数组
let min=arr[0]
for ((i=0;i<${#arr[*]};i++)) ⭕#这里刚刚开始数组长度 ${#arr} 不是正确是${#arr[@]} 漏掉了 arr[❗@]
do
if [ $min -gt ${arr[$i]} ] 💹#数组的使用 ${arr[$i]} 两个$符号的含义
then
min=${arr[$i]} 💹# 赋值不需要$符号
fi
done
echo "the min is $min" 即