Linux 命令(2)-文件管理

117 阅读6分钟

文件管理

  1. 绝对路径

    /home/test

  2. 当前目录

    [root@localhost ~]# pwd
    /root
    
  3. 特殊目录

    • . 当前目录
    • .. 当前目录父级目录
  4. 相对路径

  5. 文件操作

    1. touch 创建文件

      [test@localhost tmp]$ touch test.txt
      [test@localhost tmp]$ ls
      total 0
      -rw-r--r--. 1 test users  0 Jan  8 06:40 test.txt
      [test@localhost tmp]$
      
    2. rm 删除文件

      [root@localhost tmp]# rm test.txt
      rm: remove regular empty file ‘test.txt’? y
      [root@localhost tmp]#
      
    3. mv 移动或删除文件

      [root@localhost tmp]# ls
      
      [root@localhost tmp]# touch test.txt
      [root@localhost tmp]# ls
      test.txt
      [root@localhost tmp]# ls /mnt/
      [root@localhost tmp]# mv test.txt /mnt/
      [root@localhost tmp]# ls /mnt/
      test.txt
      [root@localhost tmp]# ls
      
      
    4. cat 查看文件

      [root@localhost ~]# cat instll.log
      
      [root@localhost ~]# cat -n instll.log # 显示行号
      
    5. head 查看文件头

      [root@localhost ~]# head get-docker.sh
      #!/bin/sh
      set -e
      
      # This script is meant for quick & easy install via:
      #   $ curl -fsSL https://get.docker.com -o get-docker.sh
      #   $ sh get-docker.sh
      #
      # For test builds (ie. release candidates):
      #   $ curl -fsSL https://test.docker.com -o test-docker.sh
      #   $ sh test-docker.sh
      [root@localhost ~]#
      // -n 显示指定的行数
      [root@localhost ~]# head -n 20 get-docker.sh
      #!/bin/sh
      set -e
      
      # This script is meant for quick & easy install via:
      #   $ curl -fsSL https://get.docker.com -o get-docker.sh
      #   $ sh get-docker.sh
      #
      # For test builds (ie. release candidates):
      #   $ curl -fsSL https://test.docker.com -o test-docker.sh
      #   $ sh test-docker.sh
      #
      # NOTE: Make sure to verify the contents of the script
      #       you downloaded matches the contents of install.sh
      #       located at https://github.com/docker/docker-install
      #       before executing.
      #
      # Git commit from https://github.com/docker/docker-install when
      # the script was uploaded (Should only be modified by upload job):
      SCRIPT_COMMIT_SHA=4957679
      
      [root@localhost ~]#
      
    6. tail 查看文件尾

      head命令

      动态查看文件尾

      [root@localhost ~]# tail -f get-docker.sh
      
    7. doc2unix 文件格式转换

      [root@localhost ~]# doc2unix test.txt
      
  6. 目录操作

    1. cd 进入目录

      [root@localhost /]# cd
      [root@localhost ~]# pwd
      /root
      [root@localhost ~]# cd /tmp
      [root@localhost tmp]# pwd
      /tmp
      [root@localhost tmp]# cd /mnt
      [root@localhost mnt]# pwd
      /mnt
      [root@localhost mnt]#
      
    2. mkdir 创建目录

      [root@localhost ~]# cd
      [root@localhost ~]# mkdir dir1
      [root@localhost ~]# cd dir1/
      [root@localhost dir1]# mkdir dir2
      [root@localhost dir1]# cd dir2/
      [root@localhost dir2]# pwd
      /root/dir1/dir2
      [root@localhost dir1]#
      

      创建多层目录

      [root@localhost dir2]# mkdir -p dir3/dir4
      [root@localhost dir2]# cd dir3/dir4/
      [root@localhost dir4]# pwd
      /root/dir1/dir2/dir3/dir4
      [root@localhost dir4]#
      
    3. rmdir rm 删除目录

      [root@localhost dir2]# rmdir dir3/
      rmdir: failed to remove ‘dir3/’: Directory not empty
      [root@localhost dir2]# rmdir dir3/dir4/
      [root@localhost dir2]# rmdir dir3/
      [root@localhost dir2]#
      
      [root@localhost dir2]# cd
      [root@localhost ~]# rm -r dir1/
      rm: descend into directory ‘dir1/’? y
      rm: remove directory ‘dir1/dir2’? y
      rm: remove directory ‘dir1/’? y
      [root@localhost ~]#
      

      删除目录及文件无提示

      [root@localhost ~]# rm -rf dir1/
      
    4. cp 文件和目录复制

      复制文件

      [root@localhost test]# cp test.txt test.copy.txt
      [root@localhost test]# ls
      test.copy.txt  test.txt
      [root@localhost test]#
      

      复制目录

      [root@localhost test]# ls
      a  test.copy.txt  test.txt
      [root@localhost test]# cp a b
      cp: omitting directory ‘a’
      [root@localhost test]# cp -r a b
      [root@localhost test]# ls
      a  b  test.copy.txt  test.txt
      [root@localhost test]#
      
  7. 文件和目录权限

    1. ls -al 查看文件或目录权限

      [root@localhost test]# ls -al
      total 0
      drwxr-xr-x. 4 root root  61 Jan  9 04:21 .
      dr-xr-x---. 4 root root 180 Jan  9 04:14 ..
      drwxr-xr-x. 2 root root   6 Jan  9 04:20 a
      drwxr-xr-x. 2 root root   6 Jan  9 04:21 b
      -rw-r--r--. 1 root root   0 Jan  9 04:15 test.copy.txt
      -rw-r--r--. 1 root root   0 Jan  9 04:14 test.txt
      [root@localhost test]#
      
      • 第一列是文件类别和权限,这列由10个字符组成,
        • 第一个字符表明该文件的类型。接下来的属性中,每3个字符为一组,
        • 第2~4个字符代表该文件所有者(user)的权限,
        • 第5~7个字符代表给文件所有组(group)的权限,
        • 第8~10个字符代表其他用户(others)拥有的权限。每组都是rwx的组合,如果拥有读权限,则该组的第一个字符显示r,否则显示一个小横线;如果拥有写权限,则该组的第二个字符显示w,否则显示一个小横线;如果拥有执行权限,则第三个字符显示x,否则显示一个小横线。
      • 第二列代表“连接数”,除了目录文件之外,其他所有文件的连接数都是1,目录文件的连接数是该目录中包含其他目录的总个数+2,也就是说,如果目录A中包含目录B和C,则目录A的连接数为4。
      • 第三列代表该文件的所有人,
      • 第四列代表该文件的所有组,
      • 第五列是该文件的大小,
      • 第六列是该文件的创建时间或最近的修改时间,
      • 第七列是文件名。
      第一个个字符含义
      d目录
      -普通文件
      l链接文件
      b块文件
      c字符文件
      ssocket文件
      p管道文件
    2. lsattr 查看文件隐藏属性

      [root@localhost ~]# lsattr anaconda-ks.cfg
      ---------------- anaconda-ks.cfg
      [root@localhost ~]#
      
    3. chattr 设置文件隐藏属性

      [root@localhost ~]# chattr +a anaconda-ks.cfg
      -----a---------- anaconda-ks.cfg
      [root@localhost ~]#
      
    4. chmod 改变文件权限

      命令作用
      chmod u+r somefile给某文件添加用户读权限
      chmod u-r somefile给某文件删除用户读权限
      chmod u+w somefile给某文件添加用户写权限
      chmod u-w somefile给某文件删除用户写权限
      chmod u+x somefile添加用户对某文件读写执行权限
      chmod u-x somefile删除用户对某文件读写执行权限
      chmod u=rwx somefile给某文件设定用户拥有读写执行权限
    5. chown 改变文件拥有者

      [root@localhost test]# touch a.txt
      [root@localhost test]# ll a.txt
      -rw-r--r--. 1 root root 0 Jan  9 20:50 a.txt
      [root@localhost test]# chown john a.txt
      [root@localhost test]# ls -l a.txt
      -rw-r--r--. 1 john root 0 Jan  9 20:50 a.txt
      [root@localhost test]# chown :john a.txt
      [root@localhost test]# ls -l a.txt
      -rw-r--r--. 1 john john 0 Jan  9 20:50 a.txt
      [root@localhost test]# chown john:john a.txt
      
    6. chgrp 改变文件的拥有组

      [root@localhost test]# touch b.txt
      [root@localhost test]# ls -l b.txt
      -rw-r--r--. 1 root root 0 Jan  9 20:52 b.txt
      [root@localhost test]# chgrp john b.txt
      [root@localhost test]# ls -l b.txt
      -rw-r--r--. 1 root john 0 Jan  9 20:52 b.txt
      [root@localhost test]#
      
    7. file 查看文件类型

      [root@localhost test]# file /root
      /root: directory
      [root@localhost test]# file /tmp
      /tmp: sticky directory
      [root@localhost test]# file /etc/passwd
      /etc/passwd: ASCII text
      [root@localhost test]# file /usr/bin/passwd
      /usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux2.6.32, BuildID[sha1]=1e5735bf7b317e60bcb907f1989951f6abd50e8d, stripped
      [root@localhost test]#
      
  8. 查找文件

    1. find 一般查找

      find PATH -name FILENAME

      [root@localhost test]# find ./ -name a.txt
      ./a.txt
      [root@localhost test]#
      
      参数含义
      -name filename查找文件名为filename的文件
      -perm根据文件权限查找
      -user username根据用户名查找
      -mtime -n/+n查找n天内/n天前更改过的文件
      -atime -n/+n查找n天内/n天前访问过的文件
      -ctime -n/+n查找n天内/n天前创建的文件
      -newer filename查找更改时间比filename新的文件
      -type b/d/c/p/l/f/s查找块/目录/字符/管道/链接/普通/套接字文件
      -size根据文件大小查找
      -depth n最大的查找目录深度
    2. locate 数据库查找

    3. which \ whereis 查找执行文件

      [root@localhost ~]# which passwd
      /usr/bin/passwd
      [root@localhost ~]# whereis passwd
      passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
      [root@localhost ~]#
      
  9. 文件压缩

    1. gzip/gunzip

      压缩单个文件

      [root@localhost test]# gzip test.txt
      [root@localhost test]# ls
      test.txt.gz
      [root@localhost test]# gunzip test.txt.gz
      [root@localhost test]# ls
      test.txt
      [root@localhost test]#
      
    2. tar

      [root@localhost test]# tar -zcvf boot.tgz /boot
      
      参数含义
      -zgzip压缩
      -c创建压缩文件
      -v显示当前被压缩的文件
      -f指使用文件名
      [root@localhost test]# tar -zxvf boot.tgz
      
      参数含义
      -z解压
      [root@localhost test]# tar -zxvf boot.tgz -C /tmp
      

      指定压缩存放目录

    3. bzip2

      [root@localhost test]# bzip2 test.txt
      [root@localhost test]# bzip2 -d test.txt.bz2
      [root@localhost test]#
      
    4. cpio