Linux学习之日期与时间命令,搜索命令,解压缩命令
1.日期与时间命令
1.1 cal
cal[选项][参数]:cal指令用于显示当前日历,或者指定日期的日历。
1.2 data
date[选项][参数]:查看日期或者时间。显示当前系统的日期和时间(硬件时间 BIOS Basic Input Output System )
//获取单独的时间数据
[root@localhost ~]# date
2021年 05月 30日 星期日 09:04:18 CST [root@localhost ~]# date %Y
date: 无效的日期"%Y"
[root@localhost ~]# date +%Y
2021
[root@localhost ~]# date +%j
150
[root@localhost ~]# date +%d
30
[root@localhost ~]# date +%p
上午
//按照自定义的形式显示时间 2021-05-30 09:08:30
[root@localhost ~]# date +"%Y-%m-%d %H:%M:%S %p"
2021-05-30 09:10:13 上午
//修改当前的系统时间
[root@localhost ~]# clock
//硬件时间
2021年05月30日 星期日 09时10分54秒 -0.335433 秒
[root@localhost ~]# date -s
"2222-02-22 22:22:22" 2222年 02月 22日 星期五 22:22:22 CST
[root@localhost ~]# clock
2021年05月30日 星期日 09时13分59秒 -0.302826 秒
1.3 timedatectl
timedatectl:设置系统时间
[root@localhost ~]# timedatectl status
Local time: 五 2222-02-22 22:25:06 CST
Universal time: 五 2222-02-22 14:25:06 UTC
RTC time: 日 2021-05-30 01:16:08
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
//修改时区
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
//可能会出现的问题 无时钟同步服务器
[root@localhost ~]# timedatectl set-ntp no
//修改年月日
[root@localhost ~]# timedatectl set-time 2025-03-30
//修改时间
[root@localhost ~]# timedatectl set-time 08:08:08
1.4 clock
clock 查看硬件时间.
2.搜索相关命令
2.1 whereis
whereis:搜索系统命令的命令,这个命令不能搜索普通文件,而只能搜索命令。
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
// 命令的所在路径 命令的帮助文档 命令的帮助文档
2.2 which
which 同whereis一样的,但是搜命令的所在地址 搜命令别名
[root@localhost ~]# which ll
alias ll='ls -l --color=auto'
/usr/bin/ls
2.3 locate
locate 可以按照文件名搜索普通文件的命令
-
优点:按照Linux自带的内置数据库搜索,搜索速度较快,消耗资源也少,默认每天更新一下这个数据库,也可以手动去更新。
==/var/lib/mlocate/mlocate.db,手动更新数据库updatedb == -
缺点:只能通过文件名来搜索文件(权限、大小、修改时间)。
[root@localhost ~]# updatedb
//先手动更新数据库 再去寻找新建的文件
[root@localhost ~]# locate Demo.py
[root@localhost ~]# locate python | more
[root@localhost ~]# locate python*
2.4 find
find 用于按照指定条件来查找文件,文件名、大小、修改时间、访问时间。
- 命令格式:find path -option -print -exec -ok |xargs |grep command{} ;
- path:要查找的目录路径。
- ~ 表示$HOME目录
- .表示当前目录
- / 表示根目录
-
print:表示将结果输出到标准输出。
-
exec:对匹配的文件执行该参数所给出的shell命令。
形式为command {} ;,注意{}与;之间有空格。 -
ok:与exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行。
-
|xargs:与exec作用相同,起承接作用。区别在于|xargs主要用于承接删除操作,而-exec都可用如复制、移动、重命名等。
-
options :表示查找方式。
//1.匹配文件名查找
[root@localhost ~]# find /root -name haha.java //-name文件名
/root/haha.java
[root@localhost ~]# find / -name haha.java
/root/haha.java
[root@localhost /]# find / -iname abc //-iname 忽略大小写文件名
/root/abc
/root/ABC
/root/aBC
/root/Abc
/root/aBc
[root@localhost ~]# find / -inum 53562145
//-inum 按照文件的inode找
/root/abc
[root@localhost ~]# find / -name *python //以python结尾的文件
//2.匹配文件大小
[root@localhost ~]# find /root -size +10000 //超过10000b文件
[root@localhost ~]# find /root -size -100 //小于100b文件
//3.匹配修改时间
[root@localhost ~]# find /root -mtime -1
-mtime 修改时间
-atime 访问时间
-ctime 权限修改时间 +n n天以前
-n n天以内 n 表示n~n+1天
//4.匹配所有者/组
[root@localhost ~]# find / -uid 1000
[root@localhost ~]# find /root -user root
-uid 用户id
-gid 组id
-user 用户名
-group 组名
-nouser 无所有者
-nogroup 无所有组
//5.匹配文件类型
b块设备
d目录
c字符设备
p管道设备
l链接文件
s套接字
f普通文本文件
[root@localhost ~]# find /dev -type l
//6.逻辑运算
[root@localhost ~]# find /root -size +10000 -a -user root
-a and
-o or
-not not
3.输入输出相关
3.1 管道符 |
管道 |:Linux 允许将 一个命令的输出可以通过管道做为另一个命令的 输入。可以理解现实生活中的管子,管子的一头塞东西进去,另一头取出来,这 里 | 的左右分为两端,左端塞东西(写),右端取东西(读)。
常用的管道命令有:
- more :分屏显示内容
- grep :在命令执行结果的基础上查询指定的文本
管道符不去处理前一个命令的错误输出!
//查看所有安装的软件,并筛选出Python相关的包
[root@localhost ~]# rpm -qa | grep python
//将用户按照UID大小排序,并取前10个 后将数据写入到文件中
[root@localhost ~]# sort -t : -k 3 -n /etc/passwd | head > a.txt
3.2重定向操作符
输出重定向 >覆盖 >>追加
输入重定向 <覆盖 <<追加
在Linux中 默认的输入设备是键盘 默认的输出设备是显示器(命令行), 所谓的重定向就是将默认的输入或输出的位置进行更改
。
-
command > file :
将command的输出结果覆盖到指定文件,如果文件不存 在则创建 -
command 1>file :
将command的输出结果覆盖到指定文件,如果文件不存在则创建 -
command 2>file:
将command的错误输出结果覆盖到指定文件,如果文件不存在则创建 -
command &> file:
将command的正确的或错误的结果覆盖到指定文件,如果文件不存在则创建 -
command >> file:追加
-
command < file:
使命令从指定的文件中读取数据 -
command < < 结束标识符:
读取命令输入,直到遇到输入行为指定的结束标
[root@localhost ~]# ll > list.txt
//正确输出 list有内容
[root@localhost ~]# cd /a/b/c > list.txt
//错误输出 没有正确内容 但是覆盖 覆盖空内容
-bash: cd: /a/b/c: 没有那个文件或目录
[root@localhost ~]# cat list.txt //没东西
[root@localhost ~]# cd /a/b/c 2> list2.txt
//错误的给list2
[root@localhost ~]# cat list2.txt
//有东西 错误信息
-bash: cd: /a/b/c: 没有那个文件或目录
[root@localhost ~]# ll 2> list2.txt
//正确输出 正确的内容不进list2 进空 但是覆盖
[root@localhost ~]# cat list2.txt //没东西
[root@localhost ~]# ll &> list3.txt
//正确的输出 给list3
[root@localhost ~]# cat list3.txt
//有内容 正确
[root@localhost ~]# cd /a/b/c/d &> list3.txt
//错误的输出 给list3 覆盖
[root@localhost ~]# cat list3.txt //有内容 错误
[root@localhost ~]# ll > list4.txt 2> list5.txt
//正确输出 给list4 list5空
[root@localhost ~]# cd /haha > list4.txt 2> list5.txt
//错误输出 list4空覆盖 list5有错误 信息
4. 归档打包 压缩解压命令
4.1 zip和unzip
zip和unzip,zip既是归档也是压缩命令,可以压缩目录。
命令格式:zip [参数] [压缩包名] [压缩的目录或者文件的路径]
unzip [参数] [压缩文件] (-d [目录])
//如果不是用括号里面的内容,则解压文件在当前工作目录
4.2 gzip和gunzip
//直接压缩一个文件 压缩包的名字为 文件名.gz
[root@localhost demo]# gzip demo1.txt
//压缩一个目录
//添加一个文件到压缩包
[root@localhost demo]# gzip demo3.txt > demo3.txt.gz //不保留
[root@localhost demo]# gzip -c demo4.txt > demo1.txt.gz //-c 保留源文件
4.3 bzip2和bunzip2
最新的Linux压缩工具,要比前两个的效率要高一些。
[root@localhost ~]# bzip2 -z 傲慢与偏见txt精校版.txt
//不保留源文件
[root@localhost ~]# bzip2 -9 -c pride-and-prejudice.txt > ppp.bz2
[root@localhost ~]# bunzip2 傲慢与偏见.txt.bz2
4.4 xz和unxz
[root@localhost~]# xz pride-and-prejudice.txt
//压一个文件
[root@localhost test]# xz dir1/ *
//压一个单级目录 没有-r
[root@localhost test]# unxz dir1.zip.xz
//解压
4.5 tar
打包文件们并压缩
//1.创建非压缩的包文件
[root@localhost ~]# tar cvf test.tar test/
//2.创建带压缩的包文件 c[z|j|J|]vf
[root@localhost ~]# tar cJvf test2.tar test
//3.列出包中文件列表
[root@localhost ~]# tar tvf test.tar
//4.解包
[root@localhost ~]# mkdir haha
[root@localhost ~]# tar xJvf test2.tar -C haha
5. 关于邮件发送
[root@localhost ~]# mail -s "Homework" menzi
You have a lot of homework to do today
You must do it immediately
Come on
EOT
[root@localhost ~]# su - menzi
[heng@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help
"/var/spool/mail/menzi": 1 messages 1 new 1 unread
U 1 root Fri June 1 10:11 26/854 "Homework"
&1
Message 7:
From root@localhost.localdomain Fri June 1 10:11:04 2021
Return-Path: root@localhost.localdomain
X-Original-To: menzi
Delivered-To: menzi@localhost.localdomain
Date: Fri June 1 10:11:04 2021 +0800
To:menzi@localhost.localdomain
Subject: Homework
User-Agent: Heirloom mailx 12.5 1/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
You have a lot of homework to do today
You must do it immediately
Come on
& q