1.Linux常见指令及权限详解

前言

  1. 为什么要进行指令操作?

指令操作,对于非专业人员是没有必要的。
对于专业人员可以是必要的,也可以是不必要的。
对于OS原理、编程学习,掌握指令操作是必要的。

  1. 先有指令还是先有图形化界面?

指令,也就是先有键盘。
早期的OS,键盘给OS、计算机相关数据。
如何输入数据给计算机?从打字机衍化过来的键盘。

所有的操作系统,刚开始的时候全都支持指令操作。

指令距离OS比图形化界面更近,便于更好的学习OS。

基础指令

ls

list

如果创建了一个空文件,也要占用磁盘空间。
文件有属性数据,也是数据,因此就需要被保存起来。
因此所有的文件操作也就被分为两类:

  • 对文件的内容操作
  • 对文件的属性操作

Linux的路径分隔符:/
Windows的路径分隔符:\

语法: ls [ 选项 ] [ 目录或文件 ]

**功能 :**对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。

  • -a 列出目录下的所有文件,包括以 . 开头的隐含文件。
  • -d 将目录象文件一样显示,而不是显示其下的文件。 如: ls –d 指定目录
  • -i 输出文件的 i 节点的索引信息。 如 ls –ai 指定文件
  • -k 以 k 字节的形式表示文件的大小。 ls –alk 指定文件
  • -l 列出文件的详细信息。
  • -h 配合-l人性化显示文件大小
    在这里插入图片描述
    ls -lh 也行
  • -n 用数字的 UID,GID 代替名称。 (介绍 UID , GID )
  • -F 在每个文件名后附上一个字符以说明该文件的类型, “*” 表示可执行的普通文件; “/” 表示目录; “@” 表示符号链接; “|” 表示 FIFOs ; “=” 表示套接字 (sockets) 。(目录类型识别)
  • -r 对目录反向排序。
  • -t 以时间排序。
  • -s 在 l 文件名后输出该文件的大小。(大小排序,如何找到目录下最大的文件)
  • -R 列出所有子目录下的文件。 ( 递归 )
  • -1 一行只输出一个文件。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

通配符

通配符含义
*任意多个字符,包括空格
?任意一个字符,必须有1个
[]匹配字符组中的任意一个
[a-f]匹配冲a-f范围内任意一个
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ ls
112.txt  123.txt  223.txt  231.txt  233.txt  hello.txt
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ ls 1* # 以1开头的文件
112.txt  123.txt
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ ls *3* # 以3结尾的文件
123.txt  223.txt  231.txt  233.txt

cd

change directory

命令含义
cd ~切换到当前用户的主目录/home/yzq
cd -最近两次工作目录来回切换
cd切换到当前用户的主目录/home/yzq
cd …切换到上级目录

Linux目录结构实质是一颗多叉树

  • 每一个子节点,既可以是一个目录(可以再放目录或者普通文件,递归式的定义),也可以是一个普通文件。
  • 这颗多叉树的叶子节点,一定是一个普通文件或者空目录。
  • 任何一个节点可以有多个子节点,但是任何一个子节点,都只有一个父节点。也就是反推回去时,路径是唯一的。

绝对路径、相对路径

/home/yzq/test.c :Linux下的 “绝对路径”(具有唯一性,永远有效)

在这里插入图片描述

在这里插入图片描述

[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mystest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls ../
5-13  5-14  6-14  README.md
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls ../5-14
test.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ cd ../5-14 #相对路径
[yzq0207@VM-16-3-centos:~/Linux/5-14]$ ls
test.c
[yzq0207@VM-16-3-centos:~/Linux/5-14]$ 

cd ~

[yzq0207@VM-16-3-centos:~/Linux/5-14]$ pwd
/home/yzq0207/Linux/5-14
[yzq0207@VM-16-3-centos:~/Linux/5-14]$ cd ~ #回到当前用户的家目录
[yzq0207@VM-16-3-centos:~]$ ls
Cpp_Code  install.sh  Linux

cd -

[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mystest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ cd ~
[yzq0207@VM-16-3-centos:~]$ cd - # 跳转至上一次我所处的路径
/home/yzq0207/Linux/6-14
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mystest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ cd ~
[yzq0207@VM-16-3-centos:~]$ ls
Cpp_Code  install.sh  Linux
[yzq0207@VM-16-3-centos:~]$ cd -
/home/yzq0207/Linux/6-14
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mystest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ 

pwd

printf working directory

显示当前文件目录

touch

  • 如果文件不存在,创建文件
  • 如果文件存在,修改文件末次修改日期

在这里插入图片描述

[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ nano mytest.c # 自带
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ll
total 20
-rwxr-xr-x 1 yzq0207 root 8400 Jun 15 11:31 a.out
drwxr-xr-x 2 yzq0207 root 4096 Jun 14 16:58 code
-rw-r--r-- 1 yzq0207 root  127 Jun 15 11:37 mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ cat mytest.c 
#include<stdio.h>

int main()
{
  printf("hello world\n");
  printf("hello world\n");
  printf("hello hello\n");
  return 0;
}
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ gcc mytest.c 
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ./a.out 
hello world
hello world
hello hello

mkdir

-p递归创建目录;parents no error if existing, make parent directories as needed
-mmode=MODE set file mode (as in chmod), not a=rwx - umask
-vverbose print a message for each created directory
-Zcontext=CTX set the SELinux security context of each created directory to CTX

注意,Linux下,同一个路径,目录和文件不允许重名。

一切皆文件

[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ mkdir dir #创建一个目录
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  dir  mytest.c


[yzq0207@VM-16-3-centos:~/Linux/6-14]$ mkdir -p dir1/dir2/dir3/dir4 # 创建一串路径
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  dir  dir1  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ cd dir1
[yzq0207@VM-16-3-centos:~/Linux/6-14/dir1]$ ls
dir2

rm

删除文件或目录,rm删除后是不可恢复的

指令说明
rm xxx
rm -r xxx递归删除,删除目录需要加-r
rm -rf xxx
-f强制删除,即使文件不存在也不会有提示
*rm 结合通配符使用
rm -r *删除当前目录所有文件
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  code  dir  dir1  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rmdir code #只能删除空目录,不常用
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  dir  dir1  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rmidr dir1
-bash: rmidr: command not found
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rmdir dir1
rmdir: failed to remove ‘dir1’: Directory not empty
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rm dir1 # rm 默认是删除普通文件的
rm: cannot remove ‘dir1’: Is a directory
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ tree dir1
dir1
`-- dir2
    `-- dir3
        `-- dir4

3 directories, 0 files
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rm -r dir1 # 递归式删除
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  dir  mytest.c
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ rm -r dir
[yzq0207@VM-16-3-centos:~/Linux/6-14]$ ls
a.out  mytest.c

tree

以树状图列出文件目录结构

-d只显示目录

在这里插入图片描述

在这里插入图片描述

–help

command --help

显示command命令的帮助信息

在这里插入图片描述

man

man [选项] 命令

Linux 的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问 Linux 手册页的命令是man 语法 :

man是manual的缩写

常用选项

  1. -k 根据关键字搜索联机帮助

  2. num 只在第 num 章节找

  3. -a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用 a 选项,当按下q 退出,他会继续往后面搜索,直到所有章节都搜索完毕。
    解释一下 , man手册分为 8 章

在这里插入图片描述

1 是普通的命令
2 是系统调用 , 如 open,write 之类的 ( 通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
3 是库函数 , 如 printf,fread
4 是特殊文件 , 也就是 /dev 下的各种设备文件
5 是指文件的格式 , 比如 passwd, 就会说明这个文件中各个字段的含义
6 是给游戏留的 , 由各个游戏自己定义
7 是附件还有一些变量 , 比如向 environ 这种全局变量在这里就有说明
8 是系统管理用的命令 , 这些命令只能由 root 使用 , 如 ifconfifig

如果没有 man手册,在root权限下,yum install -y man-pages

操作建功能
Enter一次滚动手册一行
b回滚一屏
f前滚一屏
q退出

cp

-i覆盖文件前进行提示
-r递归复制该目录的所有子目录和文件
cp xxx/xxx .将xxx文件直接复制到当前目录
yzq0207@VM-16-3-centos:~/Linux/6-16]$ touch file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ nano file.txt 
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat file.txt 
this is a test
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cp file.txt file02.txt # 拷贝
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file02.txt  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat file02.txt 
this is a test

cp的时候,如果拷贝在同一个路径,文件名必须不同。

如果拷贝到不同路径,文件名可以相同。

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file02.txt  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cp file.txt ../
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cd ../
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  file.txt  README.md
[yzq0207@VM-16-3-centos:~/Linux]$ rm file.txt
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  README.md
[yzq0207@VM-16-3-centos:~/Linux]$ 

一般是不允许直接拷贝目录的,需要加-r 进行递归拷贝

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file02.txt  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cp file.txt ../
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cd ../
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  file.txt  README.md
[yzq0207@VM-16-3-centos:~/Linux]$ rm file.txt
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  README.md
[yzq0207@VM-16-3-centos:~/Linux]$ 

cp -rf xxx xxx #强制拷贝

[yzq0207@VM-16-3-centos:~/Linux]$ cp 6-20 ./6-22
cp: omitting directory ‘6-20’
[yzq0207@VM-16-3-centos:~/Linux]$ cp -r 6-20 ./6-22
[yzq0207@VM-16-3-centos:~/Linux]$ tree

mv

移动/重命名文件、目录

-i文件覆盖前进行提示
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file02.txt  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ mv file.txt ../ #移动到上级目录
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ll
total 4
-rw-r--r-- 1 yzq0207 root 15 Jun 16 21:33 file02.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cd ../
[yzq0207@VM-16-3-centos:~/Linux]$ ll
total 28
drwxr-xr-x 2 yzq0207 root 4096 May 13 19:09 5-13
drwxr-xr-x 2 yzq0207 root 4096 May 14 21:33 5-14
drwxr-xr-x 2 yzq0207 root 4096 Jun 15 11:51 6-14
drwxr-xr-x 2 yzq0207 root 4096 Jun 15 23:18 6-15
drwxr-xr-x 2 yzq0207 root 4096 Jun 16 21:52 6-16
-rw-r--r-- 1 yzq0207 root   15 Jun 16 21:33 file.txt
-rw-r--r-- 1 yzq0207 root   51 May 15 12:16 README.md
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  file.txt
[yzq0207@VM-16-3-centos:~/Linux]$ mv 6-14 6-15 #拷贝整个目录
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-15  6-16  file.txt
[yzq0207@VM-16-3-centos:~/Linux]$ cd 6-15
[yzq0207@VM-16-3-centos:~/Linux/6-15]$ ls
6-14
[yzq0207@VM-16-3-centos:~/Linux/6-15]$ mv 6-14 ../
[yzq0207@VM-16-3-centos:~/Linux/6-15]$ cd ../
[yzq0207@VM-16-3-centos:~/Linux]$ ls
5-13  5-14  6-14  6-15  6-16  file.txt

也可以用于改名

[yzq0207@VM-16-3-centos:~/Linux]$ cd 6-16
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file02.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ mv file02.txt file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ mkdir dir
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ mv dir dir2
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt

cat

cat对应concatenate,查看文件内容
-b对非空输出行编号
-n对输出的所有行编号
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat file.txt # 正着显示文件内容
i am a student.
this is a test.
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ tac file.txt # 倒着显示
this is a test.
i am a student.
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat -b file.txt 
     1	i am a student.
     2	this is a test.
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat -n file.txt # 对输出的所有行编号
     1	i am a student.
     2	this is a test.
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat -s file.txt # 不输出多行空行
i am a student.
this is a test.
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ tac -n file.txt 
tac: invalid option -- 'n'
Try 'tac --help' for more information.

这样的cat有什么用呢?

比较适合查看短文本,或者代码

cat默认会从键盘(标准输入)读入
键盘文件

在这里插入图片描述

more/less

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cnt=1; while [ $cnt -le 100000 ]; do echo "hello world $cnt"; let cnt++; done > file.txt # 形成10w行文本
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ll
total 1752
drwxr-xr-x 2 yzq0207 root    4096 Jun 16 22:05 dir2
-rw-r--r-- 1 yzq0207 root 1788895 Jun 16 22:24 file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ wc -l file.txt # 统计数据量
100000 file.txt

cat不适合处理大数据量

more vs less ,less支持前后翻,推荐。

more xxx

分频显示文件内容,每次只显示一页

space显示下一屏
Enter滚动一行
b回滚一屏
f前滚一屏
q退出
/xxx可以搜索指定内容

一般用less命令查看日志文件

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ echo "hello world"
hello world
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ echo "hello world" > hello.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt  hello.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat hello.txt 
hello world

echo

在终端中显示参数指定的文字

在这里插入图片描述

重定向

> 本来应该写到显示器中,我们把它写到文件中了,称之为输出重定向

注意,它会清空原始文件的内容,进行重新写入。

ls -lh > a.txt # h表示更人性化显示文件信息,并直接写入a.txt

>>追加重定向

不会清空原始文件内容,而是在原始文件结尾进行新增式的写入。

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ echo "hello China" > hello.txt # 能直接创建一个hello.txt文件
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat hello.txt 
hello China
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ echo "hello China" >> hello.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat hello.txt 
hello China
hello China
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ echo "hello world" >> hello.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cat hello.txt 
hello China
hello China
hello world

<输入重定向,把本来应该从键盘文件读取数据的方式,改成从指定的文件中读取

[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat hello.txt 
hello China
hello China
hello world
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat < hello.txt 
hello China
hello China
hello world
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat < hello.txt # 输入重定向
hello China
hello China
hello world

暂停与终止

ctrl+z是暂停,任务还在的,用job是可以查看当前的任务,fg x则可以切换回去

ctrl+c是终止

[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat
^Z
[1]+  Stopped                 cat
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat
^Z
[2]+  Stopped                 cat
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ jobs
[1]-  Stopped                 cat
[2]+  Stopped                 cat
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ fg 1
cat
^C
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ fg 2
cat
^C

Linux下一切皆文件

键盘、显示器、网卡、显卡、鼠标等所有设备都看做文件,看待事物的方式就非常简单了,可以使用同一套处理事情的方式来解决所有问题。

管道|

Linux允许将一个命令的输出通过管道作为另一个命令的输入

ls -lha ~ | more # 借助管道分屏显示内容

在这里插入图片描述

head/tail

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head file.txt # 默认查看前10行
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9
hello world 10
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head -20 file.txt # 查看前20
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ tail file.txt # 默认查看后10行
hello world 99991
hello world 99992
hello world 99993
hello world 99994
hello world 99995
hello world 99996
hello world 99997
hello world 99998
hello world 99999
hello world 100000
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ tail -30 file.txt # 指定查看后30

如何查看文本的【30000,30020】行呢?

  1. 临时文件法
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head -30020 file.txt > tmp.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ ls
dir2  file.txt  hello.txt  tmp.txt
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ tail -20 tmp.txt 
  1. 管道法
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head -30020 file.txt | tail -20
hello world 30001
hello world 30002
hello world 30003
hello world 30004
hello world 30005
hello world 30006
hello world 30007
hello world 30008
hello world 30009
hello world 30010
hello world 30011
hello world 30012
hello world 30013
hello world 30014
hello world 30015
hello world 30016
hello world 30017
hello world 30018
hello world 30019
hello world 30020

|看做一个管道,head -30020 file.txt | 把前30020行数据写入到管道里面,| tail -20 再对管道二次加工,取出后20个

# 级联多个命令,流水线式的处理数据
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head -100 file.txt | tail -10
hello world 91
hello world 92
hello world 93
hello world 94
hello world 95
hello world 96
hello world 97
hello world 98
hello world 99
hello world 100
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ head -100 file.txt | tail -10 | head -3
hello world 91
hello world 92
hello world 93

date

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date
Mon Jun 20 10:56:05 CST 2022
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%Y-%m-%d_%H:%M:%S
2022-06-20_10:56:42
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%s
1655693872
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%s
1655693879
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%s
1655693880
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%s
1655693881
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%s #显示时间戳1655693896

时间戳可以作为日志的一部分

时间戳转换为日期

date -d@xxxx

yzq0207@VM-16-3-centos:~/Linux/6-16]$ date -d@0
Thu Jan  1 08:00:00 CST 1970
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%Y-%m-%d_%H:%M:%S -d@0
1970-01-01_08:00:00
[yzq0207@VM-16-3-centos:~/Linux/6-16]$ date +%Y-%m-%d_%H:%M:%S -d@12399390
1970-05-24_20:16:30

cal

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cal
      June 2022     
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ cal 2022
                               2022                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

grep

行文本过滤工具

一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

grep全称是Global Regular Expr ession Print,表示全局正则表达式版本,它的使用权限是所有用户。

-n显示匹配行及行号
-v显示不包含匹配文本的所有行(反向匹配)
-i忽略大小写

搜索内容注意加引号

常用模式
^a行首,搜索以a开头的行
ke$行尾,搜索以ke结束的行
-E表示使用扩展表达式
^匹配正则表达式的开始行
$匹配正则表达式的结束行

在这里插入图片描述

[yzq0207@VM-16-3-centos:~/Linux/6-16]$ grep '1234' file.txt 
hello world 1234
hello world 11234
hello world 12340
hello world 12341
hello world 12342
hello world 12343
hello world 12344
hello world 12345
hello world 12346
hello world 12347
hello world 12348
hello world 12349
hello world 21234
hello world 31234
hello world 41234
hello world 51234
hello world 61234
hello world 71234
hello world 81234
hello world 91234
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ vim hello.txt 
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ cat hello.txt 
hello China
hello China
hello world
C++ c
c++ C
python
Python
go
GO

[yzq0207@VM-16-3-centos:~/Linux/6-20]$ grep 'c++' hello.txt 
c++ C
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ grep -i 'c++' hello.txt # 忽略大小写
C++ c
c++ C
[yzq0207@VM-16-3-centos:~/Linux/6-20]$ grep -v 'c++' hello.txt # 反向匹配,过滤掉含有关键字的
hello China
hello China
hello world
C++ c
python
Python
go
GO

[yzq0207@VM-16-3-centos:~/Linux/6-20]$ grep -iv 'c++' hello.txt # 反向匹配,过滤掉含有关键字的,大小写都过滤掉
hello China
hello China
hello world
python
Python
go
GO

bc

Linux下的计算器

在这里插入图片描述

uname

查看电脑或者操作系统相关信息

在这里插入图片描述

热键

ctrl + c终止失控程序
TAB自动补齐
ctrl + r搜索历史命令
ctrl + d快速退出账号
history显示所有的历史命令

远程管理常用命令

shutdown

shutdown -r now立刻重启
shutdown +1010分钟后关机
shutdown 20:50
shutdown -c取消之前指定的关机计划

查看或配置网卡信息

网卡

  • 网卡是一个专门负责网络通信的硬件设施
  • IP地址是设置在网卡上的硬件设施
  • 每台电脑IP地址不能相同

ifconfig

查看/配置计算机当前的网卡配置信息

在这里插入图片描述

eth0,lo都是网卡名称,不需要关注。

127.0.0.1 是本地回环地址,用来测试本机网卡是否正常。

ping

ping IP地址检测到目标IP地址连接是否正常
ping 127.0.0.1检测本机网卡是否正常

在这里插入图片描述

ssh

ssh客户端是一种使用Secure Shell协议连接到远程计算机的软件程序

在这里插入图片描述

域名

  • 遗传用点分隔的名字组成,例如:www.itcast.com
  • 是IP地址的别名,方便用户记忆

在这里插入图片描述

112.80.248.75其实就是百度的IP地址。

访问百度IP地址时还涵盖了默认的Web服务器端口号80

也就是http://112.80.248.75:80/

端口号

  • 通过IP地址可以找到网络上的计算机
  • 通过端口号可以找到计算机上运行的程序
  • ssh服务器默认端口号是22,默认端口号连接的时候可以省略
序号服务端口号
01SSH服务器22
02Web服务器80
03HTTPS443
04FTP服务器21

ssh [-p port] user@remote

  • user 是在远程机器上的用户名,不指定的话默认为当前用户
  • remote 是远程机器地址,可以说IP/域名/别名
  • port 是SSH Server监听的端口,默认是22

ssh这个终端命令只在Linux或者UNIX系统下使用

使用exit退出当前用户的登录

ssh配置的相关文件都在.ssh目录下面

在这里插入图片描述

非对称加密算法

  • 使用公钥加密的数据,需要使用私钥解密
  • 使用私钥加密的数据,需要使用公钥解密

SSH服务器存有公钥,SSH客户端通过私钥加密的数据传送过去能被服务器解密,服务器再通过公钥加密传回给客户端,也能被私钥解密。

配置别名

~/.ssh/config追加一下内容:

Host myserver
    HostName ip地址
    User xxx
    Port xx

之后就可以用ssh myserver替代ssh -p port user@remote

scp

secure copy

ssh [-P port] user@remote

注意是大写

把当前目录下的文件复制到远程家目录下面。

scp -P port 01.py user@remote:Desktop/01.py

用户和权限

权限 = 人 + 事物属性

文件类型和访问权限

Linux不以文件名后缀区分文件类型,a.out a.ext a.txt都能正常运行
在这里插入图片描述

Linux是以ll显示的第一个字符区分文件类型。

  1. -:普通文件,文本、源代码、可执行程序、第三方动静态库
  2. d:目录文件
  3. l:链接文件
  4. p:管道文件
  5. b:块设备文件,磁盘
  6. c:字符设备文件

在这里插入图片描述

在这里插入图片描述

  • 用户管理包括用户管理和组管理
  • 在Linux中,可以指定每一个用户针对不同文件、目录有不同的权限
  • 权限包括:
缩写数字代号
readr4
writew2
excutex1

文件访问者的分类

  1. 文件和文件目录的所有者:u—User
  2. 文件和文件目录的所有者所在的组的用户:g—Group(组)
  3. 其它用户:o—Others

所属组

其实就是一个开发组,组内的人拥有相同的权限。

在这里插入图片描述

第一个字符是d表示目录,-表示文件

Linux中,很多时候,会出现组和用户名同名的情况

权限后面的数字表示硬链接数,其实就是有多少种方式可以访问到当前目录/文件。

一个目录每增多一个子目录,硬链接数便+1

对图中的dir,有2种方式访问。

  1. cd ~/Linux/6-22/dir/ #绝对路径
  2. cd . #相对路径

在这里插入图片描述

当增加一个子目录后,硬链接数变为3了。

在这里插入图片描述

在这里插入图片描述

权限修改

只有文件的拥有者和root才可以改变文件的权限。

chmod xxx+/-rwx 文件/目录

chmod a=rwx xxx
修改所有人权限都为rwx

不添加对谁更改权限,默认是更改所有用户的权限,包括拥有者、所属组、其他用户。

在这里插入图片描述

在这里插入图片描述

权限修改后,不可读不可写

在这里插入图片描述

在这里插入图片描述

增加可执行权限后,文件变绿了。

在这里插入图片描述

为py文件中添加python的解释器

在这里插入图片描述

才可以执行

./01.py

在这里插入图片描述

目录权限修改

针对目录
-x可以进入目录cd xxx
-r可以浏览目录信息ls xxx(可以查看的是文件列表,文件内容是否允许查看取决于文件的权限)
-w可以删除移动目录内文件touch、move…
-wx可以进入、可以在目录下创建文件,不允许查看
-rx可以进入、可以在目录下查看文件列表,不允许创建文件
–x只能进入,不能查看文件列表和创建文件文件内容是否可以查看由文件自身权限决定

当一个目录没有可执行权限时,对目录的指令操作就不够权限了。

在这里插入图片描述

在这里插入图片描述

当恢复可执行权限,去掉可读可写权限时,无法查看目录的内容。

在这里插入图片描述

恢复可读权限后没有可写权限,无法在目录新增东西。

在这里插入图片描述

只针对拥有者修改相关权限:

chmod u+rwx py.tar

在这里插入图片描述

也可以同时操作多个用户

chmod u+rwx, g+rx, o+r py.tar

注意,root用户并不受普通用户权限的约束。root虽然对应yzq0207来说属于others,但由于是超级用户,约束不起作用。

在这里插入图片描述

超级用户

  • Linux系统中的root账号通常用于系统的维护和管理,对操作系统的所有资源具有所有访问权限。
  • Linux安装过程中,系统会自动创建一个用户账号,即"标准用户"
  • su -切换到root用户
  • su root root可以省略

sudo

  • su是substitute user的缩写,表示使用另一个用户的身份
  • sudo命令用来以其他身份执行命令,预设的身份是root
  • 用户使用sudo时,必须输入密码,并有5分钟有效期
  • 未经授权的用户企图使用sudo,会发出警告邮件给管理员
  • 超级用户命令提示符#;普通用户命令提示符$

修改权限拥有者

sudo chown 用户名 文件名/目录名修改拥有者给别人东西要经过别人同意的,利用sudo提升权限
sudo chgrp -R 组名 文件名/目录名修改组
sudo chmod -R xxx 文件名/目录名修改权限rwx-R表示递归执行
sudo chown root:root 文件全改成root

权限掩码

chmod在设置权限时,可以简单的使用3个数字分别对应拥有者/组/其他用户的权限

之前的chmod +/-rwx 文件名/目录名不能精确到拥有者/组/其他用户。

在这里插入图片描述

755 就表示拥有者是rwx权限,组是rx权限,其他用户是rx权限

为何当前系统默认创建的目录和文件权限都是755和644?

在这里插入图片描述

这就涉及到权限掩码问题:
假设默认权限是mask,则实际创建的出来的文件权限是: mask & ~umask

umask查看当前系统权限掩码

在这里插入图片描述

  1. 默认的目录权限是777
  2. 默认的文件权限是666

文件掩码的第一个0表示八进制

022对应000 010 010

最终权限 = 默认权限"去掉"umask中存在的权限
也就是 最终权限 = 默认权限 & (~umask)

777对应111 111 111

–> 111 101 101 --> 755

666对应110 110 110

–> 110 100 100 --> 644

其实就是 666 & (~022) --> 644

如果假设umask = 244

010 100 100

111 111 111

101 011 011 533 r-x -wx -wx

110 110 110

100 010 010 422 r-- -w- -w-

超级用户默认掩码 0022
普通用户默认掩码 0002

在这里插入图片描述

组管理

创建/删除组都需要通过sudo执行

命令作用
01groupadd 组名添加组
02groupdel 组名删除组
03cat/etc/group查看组信息
04chgrp 组名 文件/目录名修改文件/目录所属组
  • 组信息保存在/etc/group文件中
  • /etc是专门用来保存系统配置信息的目录
  • 在实际应用中,预先针对组设置好权限,然后将不同的用户添加到对应的组中。

使用sudo groupadd dev添加组之后,在/etc/group里面查看

cat /etc/group

在这里插入图片描述

利用sudo chgrp dev Python学习/修改目录所属的组

在这里插入图片描述

用户管理

说明
useradd -m -g 组 用户名添加新用户-m 自动创建用户家目录并设定用户权限
-g 指定用户所在的组,否则会创建一个同名的组
passwd 用户名设置用户密码
userdel -r 用户名删除用户-r 会自动删除用户家目录
cat /etc/passwd | grep 用户名确认用户信息新建用户后,用户信息保存在/etc/passwd文件中

查看用户信息

id [用户名]查看用户的UID和GID信息[]表示可加可不加
who查看当前所有登录的用户数
whoami查看当前登录用户的账户名

cat -n /etc/passwd | grep yzq0207

在这里插入图片描述

x表示密码是加密的,1002对应uid(用户代号),0对应gid(组代号)

在这里插入图片描述

usermod

  • 主组:通过在新建用户时指定,在etc/passwd的第4列GID对应的组
  • 附加组:在etc/group中最后以列表示该组的用户列表,用于指定用户的附加权限

注意:设置了用户的附加权限之后,需要重新登录才能生效

usermod -g 组 用户名修改用户主组
usermod -G 组 用户名修改用户附加组
usermod -s /bin/bash 用户名修改用户登录Shell

登录使用的Shell就是登录之后使用的终端命令,ubantu默认是dash

在这里插入图片描述

which

  • /etc/passwd是用于保存用户信息的文件
  • /user/bin/passwd是用于修改用户密码的程序

which可以查看执行命令所在的位置

在这里插入图片描述

user下的passwd是可执行的程序,而etc下的passwd只是文件。

bin和sbin

  • Linux中,绝大多数可执行文件都是保存在/bin/sbin/user/bin/user/sbin中的

  • /bin是二进制执行文件目录,主要用于具体应用

  • /sbin(system binary)是系统管理员专用的二进制代码存放目录,主要用于系统管理

  • user/bin(user commands for applications)后期安装的一些软件

  • user/sbin(super user commands for applications)超级用户的一些管理程序

切换用户

su - 用户名切换用户、并且切换目录-可以切换到用户家目录
exit退出当前登录用户
su -切换到超级用户不安全,不推荐

su - yzq0207切换为普通用户

su -切换为超级用户

在这里插入图片描述

注意exit只是退出当前登录用户。

粘滞位

Linux下可以存在一些目录,拥有者和所属组都是root,其他人允许以other的身份在该目录下进行文件的创建、读取、修改。(例如公共的tmp目录)

在这里插入图片描述

关于之前目录的权限问题,好像存在一个bug
只要用户具有目录的写权限,用户就可以删除目录中的文件,而不论这个用户是否有这个文件的写权限。
这好像不太科学啊,我张三创建的一个文件,我不让你写不让你读,但凭什么你李四可以删掉?

如果我还想在共享目录下,形成临时文件,但是不允许除了我之外的人删除。
解决办法:利用超级用户给共享的目录加上粘滞位

粘滞位目录下的文件只能由root、目录拥有者、文件拥有者删除。

在这里插入图片描述

other的权限由 rwx 变成了 rwt

file

辨识文件类型

-c详细显示指令执行过程,便于排错或分析程序执行的情形
-z尝试去解读压缩文件的内容

在这里插入图片描述

系统信息相关命令

  • 时间和日期
    date
    cal
    cal -y查看一年的日历

  • 磁盘信息

    df -hdisk free 显示磁盘剩余空间-h以人性化的方式显示大小
    du -h [目录名]disk usage 显示目录下的文件大小
  • 进程信息
    进程:当前正在执行的一个程序

    ps auxprocess status查看经常的详细状况a 显示终端上所有进程
    u 显示进程详细状态
    x 显示没有控制终端的进程,也就是不是通过终端启动的进程
    top动态显示运行中的进程并且排序退出需要输入q
    kill [-9] 进程代号终止指定代号的进程,-9表示强制终止[]表示可加可不加
    • ps默认只会显示当前用户通过终端启动的应用程序

    • 使用kill命令时,最好只用来终止当前用户开启的进程,而不要终止root用户开启的进程,否则可能导致系统崩溃

在这里插入图片描述

其他命令

find

find [路径] -name "*.py"查找指定路径下扩展名为.py的文件,包括子目录

  • 如果省略路径,表示在当前文件夹下查找
  • 可以搭配通配符一起使用

软链接

ln -s 被连接的源文件 链接文件类似快捷方式
  • 如果没有-s选项,建立的就是硬链接文件
  • 源文件要使用绝对路径,不能使用相对路径,如果用相对路径创建的话,移动快捷方式之后就找不到原来的了。

在这里插入图片描述

硬链接

硬链接:有多少种方式可以访问文件、目录

在这里插入图片描述

Linux中,文件名和文件的数据分开存储。

即使删除链接的源文件,依旧能通过硬链接访问原来的内容。

在这里插入图片描述

这里的删除其实只是删除的文件名,文件的数据已经存储着的。

ls -l 可以查看一个文件的硬链接数。

打包压缩

Linux下几乎支持所有种类的压缩包,因为Linux面对的平台非常多。

为什么要打包压缩呢?

  1. 节约空间
  2. 软件发布方提供的是压缩包
  3. 所有的软件写好之后,不是只有一个文件的,压缩能有效避免网络传输过程中文件丢失
  4. 开发人员开发的软件,将来都是要上传部署到Linux的,需要打包压缩

tar是Linux最常用的备份工具,可以把一系列文件打包到一个大文件中,也可以把一个打包的大文件恢复成一系列文件。

zip则是把打包压缩的过程合并了,unzip这是把解压缩解包的过程合并了。

zip -r 压缩文件.zip 被压缩文件递归压缩
unzip xxx解压文件
unzip xxx -d 指定路径解压缩到指定路径
tar -cvf 打包文件.tar 被打包文件/路径打包文件tar只负责打包,不负责压缩。用gzip压缩打包后的文件,生成的文件扩展名xxx.tar.gz
tar -xvf 打包文件.tar解包文件
c生成档案文件,创建打包文件
x解开档案文件
v列出归档解档的详细过程,显示进度
f指定档案文件名称,f后面一定是.tar 文件,所以必须放选项最后
z-z 调用gzip
t查看tarfile里面的文件
tar -zcvf 打包文件.tar.gz 被压缩文件/路径压缩文件-z 会自动调用gzip。tar.gz可以简写成tgz
tar -zxvf 打包文件.tar.gz解压缩文件
tar -zxvf 打包文件.tar.gz -C 目标路径解压缩到指定路径-C解压缩到指定目录,目录必须事先存在。
tar -jcvf 打包文件.tar.bz2 被压缩文件/路径压缩文件-j 调用bz2进行压缩
tar -jxvf 打包文件.tar.bz2解压缩文件
tar -jxvf 打包文件.tar.bz2 -C 目标路径解压缩到指定路径

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

tar.gz简写成tgz

在这里插入图片描述

apt

aptAdvanced Packaging Tool是Linux下的一款安装包管理工具

sudo apt install 软件包安装软件
sudo apt remove 软件名卸载软件
sudo apt upgrade更新已安装的包

centos 用 yum替代apt

Shell运行原理

Linux严格意义上说的是一个操作系统,我们称之为“核心(kernel)“ ,但我们一般用户,不能直接使用kernel。而是通过kernel的“外壳”程序,也就是所谓的shell,来与kernel沟通。如何理解?为什么不能直接使用kernel?

Shell:外壳程序,命令行解释器

负责传递用户指令,交给OS,OS执行完毕,将结果交给Shell,Shell再交给用户。

存在意义:降低OS操作成本;保护操作系统,拦截非法请求等。

  • 将使用者的命令翻译给核心(kernel)处理。
  • 同时,将核心的处理结果翻译给使用者。

对比windows GUI,我们操作windows 不是直接操作windows内核,而是通过图形接口,点击,从而完成我们的操作(比如进入D盘的操作,我们通常是双击D盘盘符或者运行起来一个应用程序)

对比Linux,shell对我们的指令进行解析,然后给Linux内核,通过内核运行出结果,通过shell解析给用户。

Centos7常用的命令行解释器(shell)叫做bash

尾声

🌹🌹🌹

写文不易,如果有帮助烦请点个赞~ 👍👍👍

Thanks♪(・ω・)ノ🌹🌹🌹

😘😘😘

👀👀由于笔者水平有限,在今后的博文中难免会出现错误之处,本人非常希望您如果发现错误,恳请留言批评斧正,希望和大家一起学习,一起进步ヽ( ̄ω ̄( ̄ω ̄〃)ゝ,期待您的留言评论。
附GitHub仓库链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值