Linux:find、diff、sort、tar和权限的操作
Linux的基础操作
小编以一个小的操作样例给大家介绍一下Linux基本简单的操作,请先根据上一篇的文章创建如下图的目录文件结构。
Find命令的使用:
- 查询当天修改过的文件
[user@localhost ~]# find ./ -mtime -1 - 查找在系统中最后10分钟访问的文件
[user@localhost ~]# find ./ -atime -10 - 查找在系统中为空的文件或者文件夹
[user@localhost ~]# find ./ -empty - 查找在系统中最后5分钟里修改过的文件
[user@localhost ~]# find ./ -mmin -5 - 查找在系统中属于作废用户的文件
[user@localhost ~]# find ./ -nouser - 查权限为700的文件或目录
[user@localhost ~]# find ./ -perm -700 - 查硬连接数大于2的文件或目录
[user@localhost ~]# find ./ -links +2 - 查小于512k的文件
[user@localhost ~]# find ./ -size +512k - 查找大小为0的文件或空目录
[user@localhost ~]# find ./ -empty - 查.txt文件并显示
[user@localhost ~]# find ./ -name “*.txt” - 查以大写字母开头的文件
[user@localhost ~]# find ./ -name “A-Z” - 查以host开头的文件
[user@localhost ~]# find ./ -name “host*” - 列出/home内不属于本地用户的文件或目录
[user@localhost ~]# find ./ -nouser - 列出/home内不属于本地组的文件或目录
[user@localhost ~]# find ./ -nogroup
文件的比较:
[user@localhost ~]# vi a
This is a first file
This is a second file
This is a third file
:wq
[user@localhost ~]# vi b
This is a hhh
This is a first file
This is a second file
:wq
[user@localhost ~]# vi c
This is a hhh
This is a second file
This is a third file
:wq
[user@localhost ~]# diff a b
[user@localhost ~]# diff3a b c
排序命令
[user@localhost ~]# sort a b c
This is a first file
This is a first file
This is a first hhh
This is a first hhh
This is a second file
This is a second file
This is a second file
This is a third file
This is a third file
Tar命令的使用:
1) 使用tar命令创建b目录的打包文件b.tar
[user@localhost ~]# tar cf b.tar b
2) 列出备份文件b.tar中的文件列表
[user@localhost ~]# tar tf b.tar
3) 新建一个名为tmp的文件,并将文件加入到备份文件b.tar中
[user@localhost ~]# touch tmp
[user@localhost ~]# tar rf b.tar tmp
4) 将文件tmp从备份文件中删除
[user@localhost ~]# tar xf tmp b.tar
5) 创建a目录的打包文件a.tar
[user@localhost ~]# tar cf a.tar a
6) 将a.tar和b.tar备份文件合并
[user@localhost ~]# tar rf a.tar b.tar
7) 将user目录下的文件备份并压缩,产生备份压缩文件user.tar.gz。
[user@localhost ~]# gzip user.tar.gz user.tar
8) 解压并恢复打包文件user.tar
[user@localhost ~]# gzip -d user.tar.gz
文件的访问权限:
1)设置user目录为当前目录
[user@localhost ~]# cd /home/user
2) 在user目录下创建名为file1的文件,创建目录test
[user@localhost user]# mkdir test
[user@localhost user]# touch file1
3) 查看user目录下子目录和文件的访问权限
[user@localhost user]# ls -l
4) 设置file1文件的拥有者对文件具有执行权限
[user@localhost user]# chmod u+x file1
5) 设置同组用户对文件file1具有读写权限
[user@localhost user]# chmod g+w file1
6) 设置其它用户对文件file1具有读写权限
[user@localhost user]# chmod o+rw file1
7) 设置所有用户对文件file1具有执行权限。
[user@localhost user]# chmod a+x file1
8) 设置权限掩码为177
[user@localhost user]# umask 177
9) 查看文件file1和目录test的访问权限
[user@localhost user]# ls -l file1 test
10)设置权限掩码为022
[user@localhost user]# umask 022
11) 查看文件file1和test的访问权限
[user@localhost user]# ls -l file1 test