笔记:
1、date: 两个功能:显示和设置系统的日期和时间
date -s "指定时间字符串"
-s: set,设置的意思
"指定的日志时间字符串":
2022/03/20 01:00:00
2022.03.20 01:00:00 错误的
2022-03-20 01:00:00
20220320 01:00:00
强调:我们现在主要使用的是root用户,普通用户先放着后边再用。
2、timedatectl: time, date, ctrl: control => 命令的意思,时间和日期的控制
也用于显示或者设置系统时间和时区。语法格式:timedatectrl [参数]
**重要: 命令补全功能:tab键,可以自动补全命令。自动补全规则:如果只匹配一个匹配项,按一下tab键, 它就完整补全,如果匹配多个匹配项,按两下tab键,列出匹配到的多个选项, 你接着输入字符,根据字符再去匹配。直至匹配到你想要的命令。**
**重要: 历史命令,按方向键的上键,可以调出你之前执行过的命令, 按下键调整。**
**重要: 如果你执行了某条命令,执行错了,但是一直卡在那里,使用Ctrl + C来终止**
timedatectl:
# 输入time 按tab键,因为time匹配到了多个命令,所以得按两下tab键
[root@rhcsa ~]# time
time timedatectl timedatex timeout times
# 想要的是timedatectl
# 输入了timedate,因为timedate匹配到了多个命令,所以得按两下tab键
[root@rhcsa ~]# timedate
timedatectl timedatex
# 想要的是timedatectl
# 输入了timedatec,只能匹配到一个命令, timedatectl, 所以只需要按一下tab键
[root@rhcsa ~]# timedatec
# 按一下tab
[root@rhcsa ~]# timedatectl
timedatectl set-time
[root@rhcsa ~]# timedatectl set-time "20220320 11:30:00" #错误的
Failed to parse time specification '20220320 11:30:00': Invalid argument
[root@rhcsa ~]# timedatectl set-time "2022/03/20 11:30:00" # 错误的
Failed to parse time specification '2022/03/20 11:30:00': Invalid argument
[root@rhcsa ~]# timedatectl set-time "2022-03-20 11:30:00" # 正确的
[root@rhcsa ~]# date
Sun Mar 20 11:30:04 CST 2022
[root@rhcsa ~]# timedatectl set-time "2022.03.20 11:30:00" # 错误的
Failed to parse time specification '2022.03.20 11:30:00': Invalid argument
[root@rhcsa ~]# timedatectl set-time "12:30:00" # 只设置时间
3、设置时区:set-timezone
# 未设置之前查看
[root@rhcsa ~]# timedatectl status
Local time: Sun 2022-03-20 12:31:39 CST
Universal time: Sun 2022-03-20 04:31:39 UTC
RTC time: Sun 2022-03-20 04:31:40
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
# 设置
[root@rhcsa ~]# timedatectl set-timezone "America/New_York"
# 设置完后,查看
[root@rhcsa ~]# timedatectl status
Local time: Sun 2022-03-20 00:34:49 EDT
Universal time: Sun 2022-03-20 04:34:49 UTC
RTC time: Sun 2022-03-20 04:34:49
Time zone: America/New_York (EDT, -0400)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
reboot: 重启的意思, 重启linux系
poweroff: 关机
shutdown: 关机:他可以指定时间关机。可以实现halt,reboot,poweroff这几个功能
halt: 关机
4、名称:halt
使用权限:系统管理者 halt
使用方式:halt [-n] [-w] [-d] [-f] [-i] [-p]
说明:若系统的 runlevel 为 0 或 6 ,则关闭系统,否则以 shutdown 指令(加上 -h 参数)来取代
参数:
-n : 在关机前不做将记忆体资料写回硬盘的动作
-w : 并不会真的关机,只是把记录写到 /var/log/wtmp 档案里
-d : 不把记录写到 /var/log/wtmp 档案里(-n 这个参数包含了 -d) -f : 强迫关机,不呼叫 shutdown 这个指令
-i : 在关机之前先把所有网络相关的装置先停止
-p : 当关机的时候,顺便做关闭电源(poweroff)的动作
5、wget: web get: 网络获取,下载
还有另一种下载方式: curl
语法格式: wget [参数] url
wget www.baidu.com:
去请求百度服务器,百度服务器给我返回一堆代码:index.html的内容
[root@rhcsa ~]# wget www.baidu.com
--2022-03-20 01:05:36-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 14.215.177.39
Connecting to www.baidu.com (www.baidu.com)|14.215.177.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’
index.html 100%[====================>] 2.33K --.-KB/s in 0s
2022-03-20 01:05:38 (360 MB/s) - ‘index.html’ saved [2381/2381]
[root@rhcsa ~]# wget -b www.baidu.com
Continuing in background, pid 20107.
Output will be written to ‘wget-log’.
# -b: 后台下载, 它不在前台显示
# -O file
# 使用wget下载的时候,可以指定我们下载之后的文件名称
# wget www.baidu.com # 下载下来的文件名称,是服务器上这个文件叫什么名字,我们下载就叫什么名字
-O baidu.html
[root@rhcsa www.baidu.com]# wget -O baidu.html www.baidu.com
--2022-03-20 01:29:26-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 14.215.177.39, 14.215.177.38
Connecting to www.baidu.com (www.baidu.com)|14.215.177.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘baidu.html’
baidu.html 100%[====================>] 2.33K --.-KB/s in 0s
2022-03-20 01:29:28 (689 MB/s) - ‘baidu.html’ saved [2381/2381]
[root@rhcsa www.baidu.com]# ls
baidu.html cont-1755372-15845298_adpkg-ad_hd.mp4 index.html robots.txt
6、
ls: list 列出当前目录中的内容
cat: 查看文件的内容(文本文件)
使用Bash去执行命令:
命令的格式: 命令名 [选项] [参数]
命令名,选项,参数之间用空格隔开,然后隔开的时候使用了多个空格,shell会自动帮你把它缩进为一个空格。
命令名: 告诉要做什么
选项: 告诉了要怎么做
参数:告诉了对谁做
命令名,选项,参数是区分大小写的,而且一般是小写
7、
date: 命令
date "+%S": date [+参数]
date -s "str": date [选项] [参数]
date:命令,-s: 选项, "str": 参数
8、
命令提示符
[root@rhcsa www.baidu.com]# PS1="[\u@\h \W]\$ \d \t \v \w \#"
[root@rhcsa www.baidu.com]$ Sun Mar 20 01:42:18 4.4 ~/www.baidu.com 84
**命令帮助:非常重要**
假设你先遇到了一个命令: ls,不知道这个ls这个命令是干什么用的,怎么去用也不知道,命令名 [选项] [参数] ,所以也不知道命令有哪些参数,哪些选项。
因为在linux系统中内置了很多的命令帮助文档,新安装的软件也会有帮助文档。可以通过帮助文档来了解命令的使用。
9、type
查看命令的类型:内部命令和外部命令
外部命令一般对应的是一个程序(二进制的可执行的文件)
内部命令: 外部命令中含有的命令。(cd, 包含在我们的bash中)
# cd是shell内建的命令,包含在shell中的内部命令
cd is a shell builtin
# wget /usr/bin/wget 是一个程序,所以是外部命令
wget is hashed (/usr/bin/wget)
help
1.help 内部命令: 查看内部命令的帮助
2.外部命令 --help: 查看外部命令的帮助, 其实就是每个外部命令中会有一个--help的选项
ls --help: ls: 命令名, --help:选项
man
用来提供在线帮助,使用的是联机的用户手册。有一个权威的完整的文档来支持的。
man [选项] 命令名
man 命令之后出来一个文档,因为文档过长,所以提供了一些键盘操作。
pinfo:查看info文件
cd: change directory : 改变目录
**重要:主要使用 命令 --help以及 man 命令这是最常用的方式,需要掌握的方式**
10、Linux系统的目录结构:
在安装Linux系统的时候, 对磁盘进行了分区
/: 根目录
/boot: 存储系统文件的
swap: 交换空间
Linux中所有的目录文件的存储路径:都是从/开始的,所以你可以任务/就是我们的根目录。
安装Linux系统的,创建了一块硬盘: 20GB
硬盘划分逻辑边界: 对他进行分区
/: 15GB
swap: 2GB
/boot: 500MB
目前划分了三块: 15GB,2GB,500M, 只是对硬盘进行了划分逻辑区域,但Linux系统没有联系起来。
怎么联系起来: linux要存储文件,存储到哪儿呢,存储到硬盘的哪一块区域呢?
举例:windows上,我们要存储test.txt =>假如放到d:/data
linux要怎么操作: 1.已经划分了区域
2.对区域进行格式化(安装文件系统)
3.将区域和Linux目录映射起来(操作叫挂载,目录:叫挂载点,区域: 物理存储设备)
4.你把文件存储到目录,相当于就放在你划分的这块区域。
访问根目录:就相当于访问我的15GB磁盘区域。挂载点其实就是磁盘在Linux入口。
pwd:打印当前工作目录
bin: binary 二进制,一般可以执行的命令 叫做二进制可执行的文件
举例: wget -> wget url ->去环境变量PATH中查找路径 -》在路径去搜素wget文件 -》 其实是取找wget这个可执行文件, wget url
dev:device: 设备的意思, 磁盘,鼠标,键盘,分区也算设备, tty终端设备
在Linux上万物皆文件:目录也称为文件,目录文件, 设备也称为文件:设备文件。都当文件存储在linux中。
etc: 存放配置文件的地方: 系统大部分的配置文件都存储在/etc或者它 的子目录中。
home: 存放普通用户的目录。创建一个普通的用户,会在/home下创建一个和用户名相同的目录,来存储用户的文件。
我们在安装的时候,创建了一个普通用户rhcsa: 那么对应的/home下必有一个rhcsa的目录
测试:我使用普通用户rhcsa通过xshell登录到Linux上,登录上来之后,默认进入哪个目录: /home/rhcsa=> ~
~: 对应的就是我们用户的家目录 rhcsa -> ~-> /home/rhcsa
root用户登录默认进入/root
cd -: 返回上一次工作的目录
proc: 存放系统信息的地方: cpu信息,内存的信息
sbin: s -bin => system binary
/tmp: 系统用来存储临时文件的目录,目录有一个特点:关机后会清空,所以重要的文件不要放在这个目录下。
/usr: 不是user: user software resouce: 用户的软件资源, 安装软件就可以放在这个目录下。
/var: 存储系统的各种日志。
11、路径:路径是用来表示文件或目录在系统结构中的位置。
相对路径:针对你当前所处于目录的相对位置
cd /home => cd rhcsa
现在输入的cd rhcsa这个目录就是相对于你现在所处的/home目录而言的
绝对路径:确定的路径:从根目录开始的 /home/rhcsa => 从根目录开始的,这就是绝对路径
12、**重要:Linux文件类型:Linux中一切皆文件**
1. 普通文件:一个普通的文件:视频,音频,文本等等
2. 目录文件: 指的是目录
3. 链接文件:符号链接,用于不同目录下的文件共享, 指向另一个文件
4. 设备文件:
块设备:就是我们的磁盘
字符设备:按照字符进行操作的终端 tty, 键盘
5. 管道文件:主要用于进程间数据的传递
6. 套接字文件:主要应用于网络数据连接
7、 隐藏文件:就是在文件名称的前边加.,以.开始的文件的名,被认为是隐藏文件。必须使用-a选项的ls命令。
.: 代表当前目录的意思: ./
..: 代表上一级目录
如何查看是什么类型的文件:
**ls -l** => ls list directory contents: 列出目录内容
-l: using a long listing format: 使用长格式展示
[rhcsa@rhcsa ~]$ ls -l
total 0
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Desktop
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Documents
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Downloads
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Music
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Pictures
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Public
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Templates
drwxr-xr-x. 2 rhcsa rhcsa 6 Mar 18 23:22 Videos
-rw-rw-r--. 1 rhcsa rhcsa 0 Mar 20 05:49 file1
# 只关系第一列:第一个字符
d: directory : 目录,目录文件
-: 普通文件
[rhcsa@rhcsa ~]$ ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Aug 30 2019 /bin/sh -> bash
l: link 链接文件 /bin/sh -> bash => 类似于一个快捷方式
[rhcsa@rhcsa ~]$ ls -l /dev/nvme0n1
brw-rw----. 1 root disk 259, 0 Mar 19 02:41 /dev/nvme0n1
b: block: 块,块设备文件
[rhcsa@rhcsa ~]$ ls -l /dev/tty3
crw--w----. 1 root tty 4, 3 Mar 19 03:33 /dev/tty3
c: character: 字符, 字符设备文件
[rhcsa@rhcsa ~]$ ls -l /run/dmeventd-client
prw-------. 1 root root 0 Mar 19 02:41 /run/dmeventd-client
#p: pipe: 管道,管道文件
[rhcsa@rhcsa ~]$ ls -l /run/systemd/journal/dev-log
srw-rw-rw-. 1 root root 0 Mar 19 02:41 /run/systemd/journal/dev-log
s: socket: 套接字, 套接字文件。
file: 具体详细的文件类型信息。
[root@rhcsa www.baidu.com]$file baidu.html
baidu.html: HTML document, UTF-8 Unicode text, with very long lines, with CRLF line terminators
# HTML文档,UTF-8编码
[root@rhcsa www.baidu.com]$file cont-1755372-15845298_adpkg-ad_hd.mp4
cont-1755372-15845298_adpkg-ad_hd.mp4: ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]
[root@rhcsa www.baidu.com]$file robots.txt
robots.txt: ASCII text
13、目录操作命令:
命令规则:
长度不能超过255
不能使用/当文件名: 最常用的_来连接单词。word_count.txt wordcount.txt
严格区分大小写 word.txt 和Word.txt是两个文件
命令:
cd: change directory: 切换目录
pwd: print current/woking directory: 打印当前工作目录
ls: list directory contents: 列出目录的内容
命令的格式:命令名 [选项] [参数]
ls: 以简短的形式显示当前工作目录的内容
ls -l: 以长格式显示当前工作目录的内容
ls /home: 展示指定的目录的内容
ls -l /home: 以长格式显示当前目录的内容
-a: -a是短选项,对应它的长选项 --all
do not ignore entries starting with . 不忽略以.开始内容,显示所有包含以.开始的内容
# 注意:多个选项是可以合在一起使用的
ls -al
-d:显示给定的目录,但不显示目录的内容
[root@rhcsa ~]$ls /home
rhcsa
[root@rhcsa ~]$ls -d /home
/home
-R: 以递归的方式显示,可以显示包含的子目录中的信息,以及子目录中子目录中的信息
[root@rhcsa ~]$ls -l
total 8
-rw-------. 1 root root 1657 Mar 18 22:47 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Desktop
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Documents
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Downloads
-rw-r--r--. 1 root root 1812 Mar 18 22:57 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Music
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Pictures
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Public
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Templates
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Videos
drwxr-xr-x. 2 root root 105 Mar 20 01:29 www.baidu.com
[root@rhcsa ~]$ls -Rl
.:
total 8
-rw-------. 1 root root 1657 Mar 18 22:47 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Desktop
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Documents
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Downloads
-rw-r--r--. 1 root root 1812 Mar 18 22:57 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Music
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Pictures
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Public
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Templates
drwxr-xr-x. 2 root root 6 Mar 18 23:23 Videos
drwxr-xr-x. 2 root root 105 Mar 20 01:29 www.baidu.com
./Desktop:
total 0
./Documents:
total 0
./Downloads:
total 0
./Music:
total 0
./Pictures:
total 0
./Public:
total 0
./Templates:
total 0
./Videos:
total 0
./www.baidu.com:
total 12088
-rw-r--r--. 1 root root 2381 Mar 20 01:29 baidu.html
-rw-r--r--. 1 root root 12364433 Mar 17 07:55 cont-1755372-15845298_adpkg-ad_hd.mp4
-rw-r--r--. 1 root root 2381 Mar 20 01:14 index.html
-rw-r--r--. 1 root root 2814 Jan 3 2020 robots.txt
14、重要:创建目录: make directory
命令: mkdir
# 创建不显示详细信息
[root@rhcsa ~]$mkdir test
# -v选项就是显示详细信息
[root@rhcsa ~]$mkdir -v test1
mkdir: created directory 'test1'
-p:parent: 父母, 可以帮你创建父目录,如果父目录已存在,不会覆盖。
示例:
现在要创建一个目录: parent/son 创建son目录,但是parent的目录不存在。/root/parent/son, 没有parent目录,没办法去创建son目录
两种解决办法: 先去创建parent,然后在parent下再去创建son
mkdir -p parent/son
[root@rhcsa ~]$mkdir -p parent/son
# 执行的过程,先去判定son的父目录parent是否存在,如果存在只创建son目录可以
如果不存在他就帮我去先创建parent,然后在parent下创建son
du: 估算文件使用的空间大小
[root@rhcsa ~]$du -s
21984 .
[root@rhcsa ~]$du -sh
22M .
#-s: 只展示我们当前这个目录所使用的大小
#-h: 带单位展示:K, M
#*:代表展示我们当前这个目录中所有文件和目录,但是不显示目录的子目录
[root@rhcsa ~]$du -sh *
4.0K anaconda-ks.cfg
0 Desktop
0 Documents
0 Downloads
4.0K initial-setup-ks.cfg
0 Music
0 parent
0 Pictures
0 Public
0 Templates
0 test
0 test1
0 Videos
12M www.baidu.com
15、文件操作命令:
1.创建文件: touch
touch: 如果touch的是一个已存在的文件。只是去修改文件的时间戳。
如果touch的是一个不存在的文件,去创建一个新的空文件。
重要:每个文件都三个时间:
修改时间: mtime: modification time, 修改时间(编辑文件内容), 也是ls -l展示的时间
访问时间:atime: access time 最后一次访问时间
状态改变的时间:ctime: change time 改变状态属性的时间
[root@rhcsa test]$ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 20 06:29 file
[root@rhcsa test]$touch file
[root@rhcsa test]$ls -l
total 0
-rw-r--r--. 1 root root 0 Mar 20 06:51 file
2.stat命令:查看文件的详细信息
[root@rhcsa test]$stat file
File: file
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 3325060 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-03-20 06:51:55.398074277 -0400
Modify: 2022-03-20 06:51:55.398074277 -0400
Change: 2022-03-20 06:51:55.398074277 -0400