Linux:一般基础操作,新手必读(3)find、diff、sort、tar和权限的操作

Linux:一般基础操作,新手必读(3)find、diff、sort、tar和权限的操作

Linux的基础操作

小编以一个小的操作样例给大家介绍一下Linux基本简单的操作,请先根据上一篇的文章创建如下图的目录文件结构。
Linux:一般基础操作,新手必读(3)find、diff、sort、tar和权限的操作

Find命令的使用:

  1. 查询当天修改过的文件
    [[email protected] ~]# find ./ -mtime -1
  2. 查找在系统中最后10分钟访问的文件
    [[email protected] ~]# find ./ -atime -10
  3. 查找在系统中为空的文件或者文件夹
    [[email protected] ~]# find ./ -empty
  4. 查找在系统中最后5分钟里修改过的文件
    [[email protected] ~]# find ./ -mmin -5
  5. 查找在系统中属于作废用户的文件
    [[email protected] ~]# find ./ -nouser
  6. 查权限为700的文件或目录
    [[email protected] ~]# find ./ -perm -700
  7. 查硬连接数大于2的文件或目录
    [[email protected] ~]# find ./ -links +2
  8. 查小于512k的文件
    [[email protected] ~]# find ./ -size +512k
  9. 查找大小为0的文件或空目录
    [[email protected] ~]# find ./ -empty
  10. 查.txt文件并显示
    [[email protected] ~]# find ./ -name “*.txt”
  11. 查以大写字母开头的文件
    [[email protected] ~]# find ./ -name “A-Z”
  12. 查以host开头的文件
    [[email protected] ~]# find ./ -name “host*”
  13. 列出/home内不属于本地用户的文件或目录
    [[email protected] ~]# find ./ -nouser
  14. 列出/home内不属于本地组的文件或目录
    [[email protected] ~]# find ./ -nogroup

文件的比较:

[[email protected] ~]# vi a
This is a first file
This is a second file
This is a third file
:wq
[[email protected] ~]# vi b
This is a hhh
This is a first file
This is a second file
:wq
[[email protected] ~]# vi c
This is a hhh
This is a second file
This is a third file
:wq
[[email protected] ~]# diff a b
[[email protected] ~]# diff3a b c

排序命令

[[email protected] ~]# 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
[[email protected] ~]# tar cf b.tar b
2) 列出备份文件b.tar中的文件列表
[[email protected] ~]# tar tf b.tar
3) 新建一个名为tmp的文件,并将文件加入到备份文件b.tar中
[[email protected] ~]# touch tmp
[[email protected] ~]# tar rf b.tar tmp
4) 将文件tmp从备份文件中删除
[[email protected] ~]# tar xf tmp b.tar
5) 创建a目录的打包文件a.tar
[[email protected] ~]# tar cf a.tar a
6) 将a.tar和b.tar备份文件合并
[[email protected] ~]# tar rf a.tar b.tar
7) 将user目录下的文件备份并压缩,产生备份压缩文件user.tar.gz。
[[email protected] ~]# gzip user.tar.gz user.tar
8) 解压并恢复打包文件user.tar
[[email protected] ~]# gzip -d user.tar.gz

文件的访问权限:

1)设置user目录为当前目录
[[email protected] ~]# cd /home/user
2) 在user目录下创建名为file1的文件,创建目录test
[[email protected] user]# mkdir test
[[email protected] user]# touch file1
3) 查看user目录下子目录和文件的访问权限
[[email protected] user]# ls -l
4) 设置file1文件的拥有者对文件具有执行权限
[[email protected] user]# chmod u+x file1
5) 设置同组用户对文件file1具有读写权限
[[email protected] user]# chmod g+w file1
6) 设置其它用户对文件file1具有读写权限
[[email protected] user]# chmod o+rw file1
7) 设置所有用户对文件file1具有执行权限。
[[email protected] user]# chmod a+x file1
8) 设置权限掩码为177
[[email protected] user]# umask 177
9) 查看文件file1和目录test的访问权限
[[email protected] user]# ls -l file1 test
10)设置权限掩码为022
[[email protected] user]# umask 022
11) 查看文件file1和test的访问权限
[[email protected] user]# ls -l file1 test