目录
注意:初学者,可能有错误,望提醒,谢谢
1. 初识linux
linux是操作系统内核,最初由Linus Benedict Torvalds创作,后开源代码,由全世界的程序员一起创作.
向伟大的开源精神致敬!
linux操作系统的发行版本常见的有:Debian/Ubuntu/Kali Linux/CentOS等;
这里使用CentOS 7学习;
为了方便,使用服务器学习linux;
登陆方式:
1.使用xshell软件登陆
2.使用终端登陆
常用快捷键:
复制: ctrl + insert (有些同学的 insert 需要配合 fn 来按)
粘贴: shift + insert
退出:ctrl + d
终止异常命令:ctrl + c
界面全屏:alt + enter
基础知识:
linux目录结构:
linux的目录结构,本质是一颗多叉树:
1.目录(树上的节点)
2.文件或者空目录(叶子节点)
[kozen@VM-4-7-centos /]$ pwd
/
[kozen@VM-4-7-centos /]$ ls
bin dev lib media proc run sys var
boot etc lib64 mnt public sbin tmp
data home lost+found opt root srv usr
linux下一切皆文件的设计思路:
底层的不同可以使用一层软件封装来实现上层的统一.
例如:
从键盘,普通文件,显示器执行读取/写入操作,在底层上是不同的,但是在上层使用时,却可以使用相同的方式调用,提高使用效率;
这里就有点像C++的STL设计思想,不同的容器底层不同,但提供的接口相同,比如迭代器,有的是原生指针,有的是对原生指针的封装,模拟原生指针的功能,降低使用成本.
总结:用统一的方式处理所有事物.
补充:linux是用C语言写的,以及少量汇编;
[kozen@VM-4-7-centos day_1]$ echo "hello world"
hello world
[kozen@VM-4-7-centos day_1]$ echo "hello world" > hello.txt
[kozen@VM-4-7-centos day_1]$ ll
-rw-rw-r-- 1 kozen kozen 12 Oct 6 22:53 hello.txt
[kozen@VM-4-7-centos day_1]$ cat hello.txt
hello world
[kozen@VM-4-7-centos day_1]$ cat < hello.txt
hello world
[kozen@VM-4-7-centos day_1]$ cat
54654
54654
45131
45131
cat < hello.txt 将本来应该从键盘中读取的内容,变成从文件中读取
管道:
可以批量化,流水化的处理数据;
将上一个命名的结果传递给下一条命令处理
提取490~500行
[kozen@VM-4-7-centos day_1]$head -500 hello_10000.txt | tail -10
hello 500
hello 501
hello 502
hello 503
hello 504
hello 505
hello 506
hello 507
hello 508
hello 509
管道和xargs指令组合:
xargs:将管道的输出结果作为参数,交给后面的程序
[kozen@VM-4-7-centos day_1]$ echo "-l" | xargs ls
total 140
-rw-rw-r-- 1 kozen kozen 63 Sep 23 16:46 1.c
drwxrwxr-x 3 kozen kozen 4096 Sep 23 16:46 code
-rw-rw-r-- 1 kozen kozen 202 Sep 23 16:46 create_file.c
drwxrwxr-x 2 kozen kozen 4096 Sep 23 16:46 game
-rw-rw-r-- 1 kozen kozen 108822 Sep 23 16:46 hello_10000.txt
-rw-rw-r-- 1 kozen kozen 10241 Sep 23 16:46 SeekMine.c
drwxrwxr-x 2 kozen kozen 4096 Sep 23 16:46 test
重定向:
输出重定向: > >>
1.如果目标文件不存在,创建它,否则访问该文件
2.访问:
a.输出重定向(从文件开始,覆盖式写入) >
b.追加重定向(从文件的结尾,追加式写入) >>
[kozen@VM-4-7-centos day_1]$ echo "124" >> hello.txt
[kozen@VM-4-7-centos day_1]$ cat hello.txt
hello world
124
[kozen@VM-4-7-centos day_1]$ echo "ddd" > hello.txt
[kozen@VM-4-7-centos day_1]$ cat hello.txt
ddd
shell外壳程序:
shell运行原理 ⇒ 外壳程序
图形化界面和操作指令 ⇒ 操作系统提供的外壳程序
比如linux的bash(命令行解释器)
外壳程序意义:
1.是用户和操作系统交互的中间软件层
2.在一定程度上,保护操作系统
为什么会有shell:
1.用户直接访问OS(操作系统),操作成本高
2.容易出错,不安全
常见基础指令:
rm删除指令:
注意:rm执行删除的时候,需要有写的权限才能删除,这里的写权限是指对应目录的权限,如果在根目录执行删除,rm -rf /*,那么只会删除你的登陆用户能删除的东西,比如/home/kozen(你的用户名)之下的文件;
打包压缩/解压:
1.是什么?
打包:将东西放在一切
压缩:采用某种压缩算法,压缩它的存储空间
2.为什么?
便于传输,归档
zip:
[kozen@VM-4-7-centos code]$ zip -r a.zip day_1/
-rw-rw-r-- 1 kozen kozen 29944 Oct 6 23:31 a.zip
drwxrwxr-x 2 kozen kozen 4096 Sep 23 16:46 day_1
[kozen@VM-4-7-centos code]$ rm -r day_1/
[kozen@VM-4-7-centos code]$ unzip a.zip
[kozen@VM-4-7-centos code]$ ll
total 36
-rw-rw-r-- 1 kozen kozen 29944 Oct 6 23:31 a.zip
drwxrwxr-x 2 kozen kozen 4096 Sep 23 16:46 day_1
tar:
[kozen@VM-4-7-centos code]$ tar -czf b.tar.gz day_1/
[kozen@VM-4-7-centos code]$ rm -r day_1/
[kozen@VM-4-7-centos code]$ ls
a.zip b.tar.gz
[kozen@VM-4-7-centos code]$ tar -xzf b.tar.gz
[kozen@VM-4-7-centos code]$ ls
a.zip b.tar.gz day_1
权限理解:
文件类型:
- d:文件夹
-:普通文件
l:软链接(类似Windows的快捷方式)
b:块设备文件(例如硬盘、光驱等)
p:管道文件
c:字符设备文件(例如屏幕等串口设备)
s:套接口文件
权限:
1.什么是权限
2.linux下的权限
-rw-rw-r-- 1 kozen kozen 29944 Oct 6 23:31 a.zip
这里rwx权限有3分,分别对应了3中角色:拥有者,所属组,其他人
3.权限表示方法:
没有权限:
修改权限:chown chgrp chmod sudo指令
-rw-rw-r-- 1 kozen kozen 29944 Oct 6 23:31 a.zip
[kozen@VM-4-7-centos code]$ chmod o+w a.zip
[kozen@VM-4-7-centos code]$ ll
total 68
-rw-rw-rw- 1 kozen kozen 29944 Oct 6 23:31 a.zip
[kozen@VM-4-7-centos code]$ chmod o-rw a.zip
[kozen@VM-4-7-centos code]$ ll
total 68-rw-rw---- 1 kozen kozen 29944 Oct 6 23:31 a.zip
权限掩码:
例如:
linux默认创建的目录权限是777,普通文件是666
umask默认是002
八进制下转化为二进制
777 111111111 rwxrwxrwx
666 110110110 rw-rw-rw-
002 000000010 -------w-
0777 & ~0002 结果: 111111101 rwxrwxr-x
0666 & ~0002 结果: 110110100 rw-rw-r--
例如:
-rw-rw-r-- 1 kozen kozen 31311 Oct 6 23:38 b.tar.gz
drwxrwxr-x 2 kozen kozen 4096 Sep 23 16:46 day_1
粘滞位: