【Linux】日期类和查找类

x33g5p2x  于2022-03-11 转载在 Linux  
字(4.9k)|赞(0)|评价(0)|浏览(506)

时间日期类

date显示时间指令

设置日期指令

cal显示日历

搜索查找类

find指令

ls -lh直观显示 

locate定位文件路径

which查看指令位置

grep过滤指令和管道符号|

 时间日期类

date显示时间指令

基本语法:
**date ** (显示当前时间)

**date +%Y   **(显示当前年份)

**date +%m **   (显示当前月份)

**date +%d   **  (显示当前是哪一天)

**date  " +%Y-%m-%d  %H:%M:%S" **  (显示年月日时分秒)

示例:演示以上指令

[root@kongchao02 myroot]# date
2022年 03月 05日 星期六 09:01:17 CST
[root@kongchao02 myroot]# date +%Y
2022
[root@kongchao02 myroot]# date +%m
03
[root@kongchao02 myroot]# date +%d
05
[root@kongchao02 myroot]# 
[root@kongchao02 myroot]# date "+%Y-%m-%d %H:%M:%S"  
2022-03-05 09:03:25 
[root@kongchao02 myroot]#

设置日期指令

基本语法:**date  -s  字符串时间 **

示例设置系统当前时间,

[root@kongchao02 myroot]# date
2022年 03月 05日 星期六 09:07:55 CST
[root@kongchao02 myroot]# date -s "2022-3-5 09:07:00"
2022年 03月 05日 星期六 09:07:00 CST
[root@kongchao02 myroot]# date
2022年 03月 05日 星期六 09:07:05 CST
[root@kongchao02 myroot]#

 cal显示日历

查看日历指令 cal
基本语法:**cal  [选项] ** (不加选项显示本月日历)

查看某年日历如2022年的,cal  2022

示例1,显示当前月  cal

[root@kongchao02 myroot]# cal
      三月 2022     
日 一 二 三 四 五 六
       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@kongchao02 myroot]#

示例2:显示2022年日历**  cal 2022**

[root@kongchao02 myroot]# cal 2022
                               2022                               

        一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   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
        四月                   五月                   六月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                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

        七月                   八月                   九月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                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
        十月                  十一月                 十二月       
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   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

[root@kongchao02 myroot]#

 搜索查找类

find指令

find指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件或目录显示在终端
基本语法:find  [搜索范围] [选项]

选项说明:

| 选项 | 功能 |
| -name<查询方式> | 按照指定的文件名查找模式文件 |
| -user<用户名> | 查找属于指定用户名所有文件 |
| -size<文件大小> | 按照指定的文件大小查找文件 |

示例1:按文件名查找,在/home下查找hello.txt

[root@kongchao02 ~]# find /home -name hello.txt
/home/hello.txt
[root@kongchao02 ~]#

示例2:按拥有者,在/home查找kc用户

[root@kongchao02 /]# ls /home
hello1.txt  hello.txt  kc  kongchao  kongchao1  kongchao2
[root@kongchao02 /]# find /home -user kc
/home/kc
/home/kc/.mozilla
/home/kc/.mozilla/plugins
/home/kc/.mozilla/extensions
/home/kc/.bash_profile
/home/kc/.bash_logout
/home/kc/.bashrc
[root@kongchao02 /]#

示例3:按大小查找,查找整个linux系统下大于100M的文件(+n大于 -n小于  n等于,单位有k,M,G)

[root@kongchao02 ~]# find / -size +100M
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/proc/kcore
find: ‘/proc/6122/task/6122/fd/5’: 没有那个文件或目录
find: ‘/proc/6122/task/6122/fdinfo/5’: 没有那个文件或目录
find: ‘/proc/6122/fd/6’: 没有那个文件或目录
find: ‘/proc/6122/fdinfo/6’: 没有那个文件或目录
/var/cache/yum/x86_64/7/updates/packages/firefox-91.6.0-1.el7.centos.x86_64.rpm
/var/lib/rpm/Packages
/run/media/root/CentOS 7 x86_64/LiveOS/squashfs.img
/usr/lib/locale/locale-archive
/usr/lib64/firefox/libxul.so
[root@kongchao02 ~]#

ls -lh直观显示 

ls -lh以符合人的形式显示

[root@kongchao02 ~]# ls -l
总用量 40
-rw-------. 1 root root 1834 2月  28 19:57 anaconda-ks.cfg
-rw-r--r--. 1 root root 1865 2月  28 19:59 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4096 2月  28 20:00 公共
drwxr-xr-x. 2 root root 4096 2月  28 20:00 模板
drwxr-xr-x. 2 root root 4096 2月  28 20:00 视频
drwxr-xr-x. 2 root root 4096 2月  28 20:00 图片
drwxr-xr-x. 2 root root 4096 2月  28 20:00 文档
drwxr-xr-x. 2 root root 4096 2月  28 20:00 下载
drwxr-xr-x. 2 root root 4096 2月  28 20:00 音乐
drwxr-xr-x. 2 root root 4096 3月   1 21:09 桌面
[root@kongchao02 ~]# ls -lh
总用量 40K
-rw-------. 1 root root 1.8K 2月  28 19:57 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.9K 2月  28 19:59 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 公共
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 模板
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 视频
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 图片
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 文档
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 下载
drwxr-xr-x. 2 root root 4.0K 2月  28 20:00 音乐
drwxr-xr-x. 2 root root 4.0K 3月   1 21:09 桌面
[root@kongchao02 ~]#

 locate定位文件路径

locate指令
locate指令可以快速定位文件路径。locate指令利用事先建立的系统中所有文件名称及路径的locate数据库实现快速定位给定的文件。Locate指令无需遍历整个文件系统,查询速度较快。为了保证查询结果的准确度,管理员必须定期更新locate时刻

基本语法:** locate 搜索的文件**

特别说明

由于locate指令基于数据库进行查询,所以第一次运行前,必须使用updatedb指令创建locate数据库。第一查找如果不执行则会检索不到

应用实例
案例1:请使用locate指令快速定位hello.txt文件所在目录

[root@kongchao02 ~]# updatedb
[root@kongchao02 ~]# locate hello.txt
/home/hello.txt
[root@kongchao02 ~]#

 which查看指令位置

which指令可以查看某个指令在哪个目录下

基本语法:which  指令

示例:查看ls、cd指令在哪个目录下

[root@kongchao02 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@kongchao02 ~]# which cd
/usr/bin/cd
[root@kongchao02 ~]#

grep过滤指令和管道符号|

grep过滤查找。管道符" |",表示将前一个命令的处理结果输出传递给后面的命令处理
基本语法:grep  [选项]  查找内容  源文件

常用选项:

| 选项 | 功能 |
| -n | 显示匹配以及行号 |
| -i | 忽略字母大小写 |

示例:在hello.txt文件中,查"yes"所在行,并显示行号

写法①:cat /home/hello.txt | grep -n "yes"
写法②:grep -n "yes" /home/hello.txt
[root@kongchao02 /]# ls /home
hello.txt  kc  kongchao  kongchao1  kongchao2
[root@kongchao02 /]# cat -n /home/hello.txt 
     1	hello world
     2	yes 
     3	NO
     4	are you ok
     5	kongchao who is
     6	yes
     7	
[root@kongchao02 /]# cat /home/hello.txt | grep -n "yes"
2:yes 
6:yes
[root@kongchao02 /]# grep -n "yes" /home/hello.txt
2:yes 
6:yes
[root@kongchao02 /]#

相关文章