linux常用命令--文件/目录权限设置/查找/增删改/打包解压

  1. 修改文件所属用户/权限
    改变文件所属群组:chgrp
    改变文件拥有者: chown
    改变文件权限:chmod
    这三各命令加上-R 参数可以递归修改整个文件夹

[root@localhost test1]# ll -h
total 0
drwxr-xr-x. 2 root root 6 Mar  3 10:27 test2
drwxr-xr-x. 2 root root 6 Mar  3 10:33 test3
[root@localhost test1]# chgrp -R es test2    #递归修改子目录
[root@localhost test1]# chown es test2
[root@localhost test1]# chmod 777 test3
[root@localhost test1]# ll
total 0
drwxr-xr-x. 2 es   es   6 Mar  3 10:27 test2
drwxrwxrwx. 2 root root 6 Mar  3 10:33 test3
  1. 命令&文件查找
    which 命令:查看命令所在的路径
    find /home -mtime 0:查询出24小时内/home目录下改动过的东西
    find /home -mtime +2:2天前(不含2天)修改过内容的目录/文件
    find /home -mtime -2 :2天内(含2天)修改过内容的目录/文件
    find /home -mtime 2:2天前当前修改过的内容
    find /home -ctime +2 :2天前(不含2天)创建的目录/文件。-exec rm -f {}
    find /home -ctime +2 -exec sudo rm -rf {} \; :2天前(不含2天)创建的目录/文件 并删除
    如图所示:在这里插入图片描述
    find /home -type f -name "*abc*":查询/home目录下文件名包含abc的文件
    find /home -type c -name "*abc*":查询/home目录下目录名包含abc的目录
    find / -size +1G :查询/home目录下大于1G的文件
    find /home|xargs grep test:查找home目录下文件中包含test的文件
  2. 创建目录: mkdir
    mkdir -p test1/test11/test12: 递归创建目录
    mkdir -m 774 test2 : 创建目录并赋权限
[root@localhost home]# mkdir -m 774 test2
[root@localhost home]# ll
total 64896
-rw-r--r--. 1 es   root 66451796 Jul 29  2020 jenkins.war
drwxr-xr-x. 3 root root      119 Sep 29 10:23 ServerAgent-2.2.1
drwxr-xr-x. 3 root root       20 Mar  3 15:48 test
drwxr-xr-x. 4 root es         32 Mar  3 10:33 test1
drwxrwxr--. 2 root root        6 Mar  3 15:52 test2  #拥有774的权限

  1. 复制文件/目录
    cp source_file target_file:复制文件
    cp -r source_dir target_dir 或者cp -R source_dir target_dir:复制文件夹
[root@localhost home]# ls -l
total 64896
-rw-r--r--. 1 es   root 66451796 Jul 29  2020 jenkins.war
drwxr-xr-x. 3 root root      119 Sep 29 10:23 ServerAgent-2.2.1
drwxr-xr-x. 3 root root       19 Mar  3 17:45 test
[root@localhost home]# cp  /home/test/test1/test11 .
cp: omitting directory ‘/home/test/test1/test11’
[root@localhost home]# cp -R /home/test/test1/test11 .    #复制到当前目录
[root@localhost home]# ll 
total 64896
-rw-r--r--. 1 root root        0 Mar  3 17:52 1.txt
-rw-r--r--. 1 es   root 66451796 Jul 29  2020 jenkins.war
drwxr-xr-x. 3 root root      119 Sep 29 10:23 ServerAgent-2.2.1
drwxr-xr-x. 3 root root       32 Mar  3 17:52 test
drwxr-xr-x. 2 root root        6 Mar  3 17:49 test11
  1. 不同服务器间复制文件/目录
    scp file/dir remote_user@remote_ip:/remote_dir:将本机文件复制到远程服务器上
    scp remote_user@ip地址或者域名:/remote_file /dir: 将远程服务器上的文件复制到本机

  2. 删除文件/目录:
    rm -rf 文件/目录:强制递归删除文件/目录(目录可以时空目录)
    rmdir:只能删除空目录

  3. 移动文件/目录
    mv source_file target_file:移动文件(如果原文件和目标文件在同一目录相当于重命名)
    mv source_dir target_dir/:移动目录
    mv source_f1 source_f2 target_dir:将多个原文件移到同一个目录下

  4. 重命名文件/目录
    mv source_name target_name:只能针对单个文件
    rename source_name target_name source_file:可单个也可批量修改

[root@localhost home]# ll
total 64896
-rw-r--r--. 1 root root        0 Mar  3 18:11 1.html
-rw-r--r--. 1 root root        0 Mar  3 17:52 2.html
-rw-r--r--. 1 es   root 66451796 Jul 29  2020 jenkins.war
drwxr-xr-x. 3 root root      119 Sep 29 10:23 ServerAgent-2.2.1
drwxr-xr-x. 3 root root       32 Mar  3 17:52 test
drwxr-xr-x. 2 root root        6 Mar  3 17:49 test_or
[root@localhost home]# rename test test33 test*  # 将test开头文件都改为test33开头
[root@localhost home]# ll
total 64896
-rw-r--r--. 1 root root        0 Mar  3 18:11 1.html
-rw-r--r--. 1 root root        0 Mar  3 17:52 2.html
-rw-r--r--. 1 es   root 66451796 Jul 29  2020 jenkins.war
drwxr-xr-x. 3 root root      119 Sep 29 10:23 ServerAgent-2.2.1
drwxr-xr-x. 3 root root       32 Mar  3 17:52 test33
drwxr-xr-x. 2 root root        6 Mar  3 17:49 test33_or

9.查看文件
file filename/dirname: 查看文档类型
cat 查看整个文件内容
head查看文件开头几行
tail查看末尾几行
tailf基本上同tail -f命令,一般用于实时查看日志
10. 文件和目录隐藏属性
chattr +i file/dir:设置文件或者目录不能被删除、修改、新增内容
chattr +a file/dir :设置文件或者目录不能被删除、修改内容,只能新增,只有root能设置,使用输出导向符(>>)增加。【输出导向符> 表示覆盖,>>表示追加】
chattr -[ia] file/dir:可以取消对应的属性
lsattr file/dir:可以查看隐藏属性

[root@localhost home]# chattr +a testdir   #给testdir赋予a属性,执行新增文件不能修改和删除目录中的内容
[root@localhost testdir]# ll
total 0
drwxr-xr-x. 2 root root  6 Mar  3 18:18 test31
drwxr-xr-x. 3 root root 32 Mar  3 18:18 test33
[root@localhost testdir]# rm -rf test31/
rm: cannot remove ‘test31/’: Operation not permitted
[root@localhost testdir]# touch 1.txt
  1. 文件打包、压缩、解压
    tar -czvf test.tar.gz testdir zd:将testdir和zd目录进行打包,并通过gzip压缩
    tar -tzvf test.tar.gz:查看包中的内容
    tar -xzvf test.tar.gz : 解压到当前目录
    tar -xzvf test.tar.gz -C dir: 解压到指定的目录(此目录必须存在)
    tar -xzvf test.tar.gz testdir:只解压出包中的testdir目录
  2. 其他
    ls -lrt :列出当前目录中的文件并且按日期顺序排
    basename dir:取dir最终的名字
    dirname dir :取dir的目录名
[root@localhost 22]# dirname `pwd`   #查看当前目录的dirname,pwd两边的是反引号
/home
[root@localhost 22]# basename `pwd` #查看当前目录的basename 
22
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值