一、Linux下文件查找命令
1、命令查找
注意:Linux下一切皆文件
which 命令:找出命令的绝对路径
whereis 命令:找出命令的路径以及文档手册信息
[root@rh ~]# which cd
/usr/bin/cd
[root@rh ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
2、文件查找(find)
find命令:精确查找,磁盘搜索,IO读写,CPU开销大
用法1:找出来输出到屏幕
根据需求查找出来直接输出到屏幕
find 查找路径 选项 关键字
常见选项 | ||
---|---|---|
-name | 按照文件名查找文件 | |
-iname | 按照文件名忽略大小写查找 | |
-size | 按照文件大小来查找 | +1M 大于1M -1M 小于1M 1M 等于1M |
-type | 按照文件类型来查找 | |
-mtime | 按文件修改时间来查找文件 | -n指n天以内,+n指n天以前 |
-atime | 按文件访问时间来查 | |
-ctime | 按文件创建时间来查找文件 | |
-perm | 按照文件权限来查找文件 |
举例说明:
1)根据文件名查找
[root@heima ~]# find /test -name "file1"
[root@heima ~]# find /test -iname "file1"
[root@heima ~]# find /etc -name "*.conf"
2)根据文件类型查找
[root@heima ~]# find /usr/bin/ -type l
[root@heima ~]# find /dev -type b
[root@heima ~]# cd /test
[root@heima test]# find . -type d
[root@heima test]# find . -type f
3)根据文件大小查找
[root@heima test]# find . -type f -size +1M
[root@heima test]# find . -type f -size -1M
[root@heima test]# find . -type f -size -1024k
[root@heima test]# find . -type f -size 9M
4)根据文件属性(权限,创建者和所属组)
[root@heima test]# find . -user heima -group sxy -type f
-mtime选项举例:
[root@heima test]# find ./ -type f -mtime +2
[root@heima test]# find ./ -type f -mtime -2
[root@heima test]# find ./ -type f -mtime 2
用法2:找出来执行命令
根据需求查找出来后执行某个动作(命令)
find 路径 选项 关键字 动作
常见动作 | |
---|---|
-exec | 对查找到的文件直接执行该参数后的shell命令 |
-ok | 对查找到的文件询问式执行该参数后的shell命令 |
-delete | 删除查找到的文件 |
-ls | 列出查找到的文件,详细信息 |
打印出查找到的文件(默认选项) |
举例说明:
语法结构: find /test/ -type f -ok cp {} /tmp \; 将查询出来的内容拷贝到根目录下的tmp中
注意:
1. -exec或者-ok后面写完命令必须以空格反斜杠\;结尾( \;)
2. {}表示find命令所找出来的内容
二、Linux下文件压缩工具
1、常见的压缩与解压缩工具
压缩工具 | 说明 | |
---|---|---|
zip | 兼容类unix与windows,可以压缩多个文件或目录 | unzip |
gzip | 压缩单个文件,压缩率相对低,cpu开销相对低 | gunzip |
bzip2 | 压缩单个文件,压缩率相对高,cpu开销相对高 | bunzip2 |
xz | 压缩单个文件,压缩率高,压缩时间相对长,解压速度快,cpu开销高 | unxz |
2、工具的用法
1.zip
压缩:
zip 压缩后的文件 需要压缩的文件
选项:
-r 递归压缩,压缩目录
注意:
zip压缩默认压缩后的格式就是.zip;当然也可以加后缀.zip,一般都加上
解压缩: -d 指定解压缩路径
2.gzip
压缩:
gzip 需要压缩的单个文件
选项:
-d 解压缩
-r 递归压缩(目录)
解压缩: gunzip 需要解压的文件
或者
gzip -d 需要解压的文件
gunzip file* 一次解压多个文件,*代表通配符;file*表示以file开头所有文件
3.bzip2
压缩:
bzip2 需要压缩的文件
选项:
-d 解压缩
4.xz
压缩: xz 文件名
选项:
-z 压缩,默认
-d 解压缩 或者 unxz
解压缩: unxz 文件名
或者
xz -d 文件名
三、Linux下文件打包工具
tar 命令:可以将多个文件打包成一个并且压缩,不会改变文件的属性,很常用。
用法:
tar 选项 打包后的文件 需要打包的文件
常用选项 | 说明 |
---|---|
-c | 创建tar包(打包) |
-z | 调用gzip工具压缩 |
-j | 调用bzip2工具压缩 |
-J | 调用xz工具压缩 |
-v | 显示详细信息 |
-f | 指定包名 |
-r | 往tar包里追加文件 |
-x | 解压 |
-C | 指定解压路径 |
-t | 列出或查看tar包内容 |
注意:
-
以上选项前面的横杠"-"可以省略
-
如果已经将文件压缩打包,那么就不能追加;如果只是打包就可以追加。
-
参数顺序需要注意,最好把-f参数放到所有参数后面。
-
当出现以下提示时,加一个大P参数解决。
tar: Removing leading `/' from member names
举例说明:
1. 将/tmp目录里的dir1目录和/etc/hosts文件打包到/tmp/dir4里叫dabao.tar
[root@localhost tmp]# tar -cvf /tmp/dir4/dabao.tar ./dir1 /etc/hosts
./dir1/
./dir1/test1.gz.bz2
./dir1/aaa/
./dir1/aaa/file2.gz
./dir1/file1.gz.bz2
tar: Removing leading `/' from member names /etc/hosts 注意:以上错误提示可以忽略
查看打包后的文件内容:
[root@localhost tmp]# tar -tf dir4/dabao.tar
./dir1/
./dir1/test1.gz.bz2
./dir1/aaa/
./dir1/aaa/file2.gz
./dir1/file1.gz.bz2
etc/hosts
2. 将/boot目录和/root/install.log文件打包并压缩到/tmp目录下叫backup_boot.tar.gz
[root@localhost ~]# tar -cvzf /tmp/backup_boot.tar.gz /boot install.log
3. 解压tar包
[root@localhost tmp]# tar -xf backup_boot.tar.gz 解压到当前路径
[root@localhost tmp]# tar -xf backup_boot.tar.gz -C dir1/ 解压到指定路径
四、Linux日期相关指令
1、date命令
date :打印或者设置当前系统日期和时间
1.打印日期或时间
打印系统当前日期或时间
[root@heima ~]# date
[root@heima ~]# date +%D
[root@heima ~]# date +%F
[root@heima ~]# date +%Y-%m-%d
[root@heima ~]# date +%T
[root@heima ~]# date +%X
[root@heima ~]# date +'%F %X'
[root@heima ~]# date +%c
打印系统非当前日期或时间
[root@heima ~]# date -d '+3days' +%F
[root@heima ~]# date -d '-3days' +%F
[root@heima ~]# date -d '3days' +%F
[root@heima ~]# date -d '3days ago' +%F
[root@heima ~]# date --date='30days' +%F
2.设置系统日期或时间
选项:-s 设置当前系统时间,只有root权限才能设置,其他只能查看。 date -s 20200523 设置成20100523,这样会 把具体时间设置成空00:00:00
date -s "01:01:01 2020-05-2" 这样可以设置全部时间
date -s "01:01:01 20200523" 这样可以设置全部时间
date -s "2020-05-23 01:01:01" 这样可以设置全部时间
date -s "20200523 01:01:01" 这样可以设置全部时间
3.应用案例
以当前日期命名创建目录或文件
创建目录和文件,以当前系统日期命名
[root@heima ~]# mkdir $(date +%F)
[root@heima ~]# touch $(date -d '+3days' +%Y%m%d).log
2、cal命令
cal :查看日历
cal 或者 cal -1 表示直接输出当前月份的日历
cal -3 表示输出上一个月+本月+下个月的日历 (-4等不行,只能-3)
cal -y 年份 表示输出某一个年份的日历
Comments | 0 条评论