摘要:包含pwd、cd、ls、cat、tac、file、echo、shutdown、date、clock(hwclock)、cal命令
此为sunny,王苗苗同学的学习笔记,持续学习,持续分享,日积月累,向着大神之路前进
Linux文件系统:
1、文件名称严格区分字符大小写
2、文件名可以使用除/以外的任意字符
3、文件名长度不能超过255字符
4、以.开头的文件为隐藏文件
.:当前目录
..:当前目录的上一级目录
常用命令:
想查看命令的帮助信息,可以使用man COMMAND或者COMMAND --help
pwd 和cd
pwd:printing working directory 显示工作目录
cd:change directory 改变目录
cd ~:切换为用户家目录,用户默认的家目录为/home/USERNAME
cd ~USERNAME:切换为指定用户家目录
cd -:切换为上一次所在目录
相关的环境变量
$PWD:当前工作目录
$OLDPWD:上一次的工作目录
实操
#pwd 查看当前目录路径
[root@redhat ~]# pwd
/root
#cd 切换目录
[root@redhat ~]# cd /etc/
[root@redhat etc]# cd ~/Desktop/web-index/
#cd - 返回上一次所在目录
[root@redhat web-index]# cd -
/etc
#cd ~:切换为用户家目录
[root@redhat etc]# cd ~
#cd 切换目录
[root@redhat ~]# cd ~/Desktop/web-index/
#pwd 查看当前目录路径
[root@redhat web-index]# pwd
/root/Desktop/web-index
ls
ls:list ,列出指定目录下的内容
ls [OPTION]... [FILE]...
-a: 显示所有文件,包括隐藏文件;
-A:显示除.和..之外的所有文件;
-l: --long, 长格式列表,即显示文件的详细属性信息;
-r: reverse, 逆序显示;
-R: recursive,递归显示;
ls -l 扩展说明
drwxr-xr-x. 3 root root 40 Mar 14 22:26 Desktop
d:文件类型,-, d, b, c, l, s, p-说明见图
rwxr-xr-x (r 可读,w 可写,x 可执行)
rwx:文件属主的权限;
r-x:文件属组的权限;
r-x:其它用户(非属主、属组)的权限;
3:数字表示文件被硬链接的次数;
root:文件的属主;
root:文件的属组;
40:数字表示文件的大小,单位是字节;
Mar 14 22:26 :文件最近一次被修改的时间;
Desktop:目录名
实操
#ls 列出指定目录下的内容
[root@redhat ~]# ls
anaconda-ks.cfg Desktop Documents Downloads haha initial-setup-ks.cfg Music Pictures Public Templates Videos
#ls -a 显示所有文件,包括隐藏文件
[root@redhat ~]# ls -a
. .bash_history .bashrc .cshrc Documents .gnome2 .ICEauthority .local Pictures .tcshrc .viminfo
.. .bash_logout .cache .dbus Downloads .gnome2_private initial-setup-ks.cfg .mozilla Public Templates
anaconda-ks.cfg .bash_profile .config Desktop .esd_auth haha .lesshst Music .ssh Videos
#ls -A:显示除.和..之外的所有文件
[root@redhat ~]# ls -A
anaconda-ks.cfg .bash_profile .config Desktop .esd_auth haha .lesshst Music .ssh Videos
.bash_history .bashrc .cshrc Documents .gnome2 .ICEauthority .local Pictures .tcshrc .viminfo
.bash_logout .cache .dbus Downloads .gnome2_private initial-setup-ks.cfg .mozilla Public Templates
#ls -l: --long, 长格式列表,即显示文件的详细属性信息
[root@redhat ~]# ls -l
total 8
-rw-------. 1 root root 1194 Mar 7 07:46 anaconda-ks.cfg
drwxr-xr-x. 3 root root 40 Mar 14 22:26 Desktop
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Documents
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Downloads
drwxr-xr-x. 2 root root 22 Mar 28 11:51 haha
-rw-r--r--. 1 root root 1245 Mar 7 00:05 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Music
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Pictures
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Public
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Templates
drwxr-xr-x. 2 root root 6 Mar 7 00:05 Videos
#ls -r: reverse, 逆序显示
[root@redhat ~]# ls -r
Videos Templates Public Pictures Music initial-setup-ks.cfg haha Downloads Documents Desktop anaconda-ks.cfg
#ls -R: recursive,递归显示
[root@redhat ~]# ls -R
.:
anaconda-ks.cfg Desktop Documents Downloads haha initial-setup-ks.cfg Music Pictures Public Templates Videos
./Desktop:
vsftpd.conf web-index
./Desktop/web-index:
index.html
./Documents:
./Downloads:
./haha:
sunny.txt
./Music:
./Pictures:
./Public:
./Templates:
./Videos:
cat、tac
cat: concatenate files and print on the standard output 连接文件并在标准输出上打印,说白了就是查看文件,不过这个只适用于小文件,大一点的文件一般使用more
-n:给显示的文本行编号
-E:显示行结束符$
tac:就是cat反过来,作用也是查看文件,只是反过来查看
实操
#cat 查看文本文件内容
[root@redhat web-index]# cat index.html
this is new web directory
whwdehejfhkef
ssdfsfsd
scsdcsdc
cdscsregtegtrh
bhuyj
# -n:给显示的文本行编号
[root@redhat web-index]# cat -n index.html
1 this is new web directory
2 whwdehejfhkef
3 ssdfsfsd
4 scsdcsdc
5 cdscsregtegtrh
6 bhuyj
# -E: 显示行结束符$
[root@redhat web-index]# cat -E index.html
this is new web directory$
whwdehejfhkef$
ssdfsfsd$
scsdcsdc$
cdscsregtegtrh$
bhuyj$
# tac 反向查看文件内容,可以看到显示内容与cat所得结果相反
[root@redhat web-index]# tac index.html
bhuyj
cdscsregtegtrh
scsdcsdc
ssdfsfsd
whwdehejfhkef
this is new web directory
file、echo
file:查看文件内容类型
echo:回显
echo [SHORT-OPTION]... [STRING]...
-n:不进行换行
-e:让转义字符生效
\n:换行
\t:制表符
STRING可以使用单引号和双引号
单引号:强引用,变量不执行替换
双引号:弱引用,变量会被替换
实操
#file:查看文件内容类型
[root@redhat web-index]# file /etc/yum.repos.d/rhe17.repo
/etc/yum.repos.d/rhe17.repo: ASCII text
[root@redhat web-index]# file /dev/sda
/dev/sda: block special
# echo 显示你输入的信息
[root@redhat web-index]# echo "hello \n world"
hello \n world
#-n: 不进行换行,-e:让转义符生效 \n:换行
[root@redhat web-index]# echo -n "hello \n world"
hello \n world[root@redhat web-index]# echo -e "hello \n world"
hello
world
#-e:让转义符生效 \t:制表符
[root@redhat web-index]# echo -e "hello \t world"
hello world
#单引号:强引用,变量引用不执行替换
[root@redhat web-index]# echo '$SHELL'
$SHELL
#双引号:弱引用,变量引用会被替换
[root@redhat web-index]# echo "$SHELL"
/bin/bash
shutdown
shutdown:shutdown - Halt, power-off or reboot the machine 机器关机或重启命令
shutdown [OPTIONS...] [TIME] [WALL...]
OPTIONS:执行的操作
-h: halt 关机
-r:reboot 重启
-c:cancel 取消
TIME:什么时候执行
now 现在
hh:mm 几点几分
+m 几分钟后
+0 相当于now
WALL:发送一个信息给所有的终端(可使用man wall查看帮助),不指定会有默认
实操
#10分钟后关机
[root@redhat web-index]# shutdown -h +10
Shutdown scheduled for Sun 2020-03-29 12:25:11 CST, use 'shutdown -c' to cancel.
[root@redhat web-index]#
Broadcast message from root@redhat (Sun 2020-03-29 12:15:11 CST):
The system is going down for power-off at Sun 2020-03-29 12:25:11 CST!
^C
#取消关机操作
[root@redhat web-index]# shutdown -c
Broadcast message from root@redhat (Sun 2020-03-29 12:15:25 CST):
The system shutdown has been cancelled at Sun 2020-03-29 12:16:25 CST!
#10分钟后重启
[root@redhat web-index]# shutdown -r +10
Shutdown scheduled for Sun 2020-03-29 12:25:35 CST, use 'shutdown -c' to cancel.
[root@redhat web-index]#
Broadcast message from root@redhat (Sun 2020-03-29 12:15:35 CST):
The system is going down for reboot at Sun 2020-03-29 12:25:35 CST!
^C
#取消重启操作
[root@redhat web-index]# shutdown -c
Broadcast message from root@redhat (Sun 2020-03-29 12:15:44 CST):
The system shutdown has been cancelled at Sun 2020-03-29 12:16:44 CST!
#自定义wall信息,hello everyone
[root@redhat web-index]# shutdown -h +10 "hello everyone"
Shutdown scheduled for Sun 2020-03-29 12:28:21 CST, use 'shutdown -c' to cancel.
[root@redhat web-index]#
Broadcast message from root@redhat (Sun 2020-03-29 12:18:21 CST):
hello everyone
The system is going down for power-off at Sun 2020-03-29 12:28:21 CST!
^C
#取消关机操作
[root@redhat web-index]# shutdown -c
Broadcast message from root@redhat (Sun 2020-03-29 12:18:32 CST):
The system shutdown has been cancelled at Sun 2020-03-29 12:19:32 CST!
#定时在23:00关机
[root@redhat web-index]# shutdown -h 23:00
Shutdown scheduled for Sun 2020-03-29 23:00:00 CST, use 'shutdown -c' to cancel.
与日期相关的命令
Linux中有硬件时钟和系统时钟,系统启动时从硬件读取日期和时间信息,读取完成后,就不再与硬件相关联
date:系统时钟
可使用man date获取帮助信息
date:显示日期时间date
[OPTION]… [+FORMAT]
FORMAT:格式符
+%F 显示年月日
+%T 显示时分秒
+%Y 年
+%m 月
+%d 日
+%H 时
+%M 分
+%S 秒
+%s 从1970年1月1号(unix元年)0点0分0秒到命令执行那一刻经过的秒数
设定日期和时间:date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]
[[CC]YY]指年份即可以使用4位数也可以使用2位数,例如2020可以用2020也可以用20
实操
#显示日期时间
[root@redhat web-index]# date
Sun Mar 29 12:39:50 CST 2020
#date +%F 显示年月日
[root@redhat web-index]# date +%F
2020-03-29
#date +%T 显示时分秒
[root@redhat web-index]# date +%T
12:41:20
# date +%Y 年
[root@redhat web-index]# date +%Y
2020
# date +%m 月
[root@redhat web-index]# date +%m
03
#date +%d 日
[root@redhat web-index]# date +%d
29
date +%H 时
[root@redhat web-index]# date +%H
12
#date +%M 分
[root@redhat web-index]# date +%M
42
# date +%S秒
[root@redhat web-index]# date +%S
24
# date +%s 从1970年1月1号(unix元年)0点0分0秒到命令执行那一刻经过的秒数
[root@redhat web-index]# date +%s
1585456937
#查看时间,此时为2020-03-29 13:59:21
[root@redhat web-index]# date
Sun Mar 29 13:59:21 CST 2020
#设置时间,将时间设置为2019-03-29 14:00:00
[root@redhat web-index]# date 032914002019.00
Fri Mar 29 14:00:00 CST 2019
#查看日期为2019-03-29
[root@redhat web-index]# date +%F
2019-03-29
#查看时间为14:00:21
[root@redhat web-index]# date +%T
14:00:21
hwclock、clock
hwclock, clock:硬件时钟
显示或设定硬件时钟
-s, --hctosys:以硬件为准,把系统调整为与硬件时间相同(以前面的那个为准)
-w, --systohc:以系统为准,把硬件时间调整为与系统时钟相同
实操
#将系统时间设置为2020-03-29 14:19:30
[root@redhat web-index]# date 0329141920.30
Sun Mar 29 14:19:30 CST 2020
[root@redhat web-index]# date "+%F %T"
2020-03-29 14:19:32
#获取硬件时间
[root@redhat web-index]# hwclock
Sun 29 Mar 2020 10:18:49 PM CST -1.027503 seconds
#将硬件时间设置为与系统时间一致
[root@redhat web-index]# hwclock -w
#查看硬件时间,变得与系统时间一致,为2020-03-29 下午2点20
[root@redhat web-index]# hwclock
Sun 29 Mar 2020 02:20:02 PM CST -0.678242 seconds
将系统时间设置为2019年
[root@redhat web-index]# date 0329141919.30
Fri Mar 29 14:19:30 CST 2019
[root@redhat web-index]# date "+%F %T"
2019-03-29 14:19:33
#将硬件时间设置为与系统时间一致也就是19年
[root@redhat web-index]# hwclock -w
[root@redhat web-index]# hwclock
Fri 29 Mar 2019 02:19:51 PM CST -0.084520 seconds
#将系统时间设置为2020年
[root@redhat web-index]# date 0329142120.30
Sun Mar 29 14:21:30 CST 2020
[root@redhat web-index]# date "+%F %T"
2020-03-29 14:21:33
#将系统时间设置为与硬件时间一致,也就是2019
[root@redhat web-index]# hwclock -s
#查看系统时间确实为2019
[root@redhat web-index]# date "+%F %T"
2019-03-29 14:20:31
#将系统时间设置为2020
[root@redhat web-index]# date 0329142120.30
Sun Mar 29 14:21:30 CST 2020
[root@redhat web-index]# date "+%F %T"
2020-03-29 14:21:32
#将硬件时间设置为与系统时间一致,也就是2020
[root@redhat web-index]# hwclock -w
#查看系统时间变为2020
[root@redhat web-index]# hwclock
Sun 29 Mar 2020 02:21:47 PM CST -0.601819 seconds
cal
cal 日历
cal [options] [[[day] month] year]
实操
#查看当月日历
[root@redhat Desktop]# cal
March 2020
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 31
#查看指定年份的日历
[root@redhat Desktop]# cal 2020
2020
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 2 3 4 1 1 2 3 4 5 6 7
5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
26 27 28 29 30 31 23 24 25 26 27 28 29 29 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 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
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 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
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 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31