文件管理之创建目录、文件,查看cat、head、tail,文件删除、移动

文件管理

  • 创建
  • 查询
  • 修改
  • 删除
  • 移动
  • 权限

创建文件

  • 目录(mkdir)

    • 格式:mkdir [参数] [目录路径]

      [root@string /]# mkdir dir
      [root@string /]# ls -l
      total 20
      drwxr-xr-x    2 root root    6 Mar  5 12:18 dir
      
    • 参数:

      • -p : 当上级目录不存在时,自动创建上级目录。一般称之为:递归创建

        [root@string ~]# mkdir -p abc/bcd/cde/def
        [root@string ~]# tree abc/
        abc/
        └── bcd
            └── cde
                └── def
        
      • -v : 打印出整个目录的创建过程

        [root@string ~]# mkdir -pv one/two/treee/four
        mkdir: created directory ‘one/two’
        mkdir: created directory ‘one/two/treee’
        mkdir: created directory ‘one/two/treee/four’
        
      • -m : 设置创建文件夹的权限

  • 文件(touch)

    • 格式:touch [参数] [文件路径]

      # 创建单个文件
      [root@string test]# touch 1.txt
      [root@string test]# ls
      1.txt
      
      # 创建多个文件
      [root@string test]# touch 3.txt 2.txt
      [root@string test]# ls
      1.txt  2.txt  3.txt
      
      # 匹配创建多个文件
      [root@string test]# touch {a..z}.txt
      [root@string test]# ls
      1.txt  3.txt  b.txt  d.txt  f.txt  h.txt  j.txt  l.txt  n.txt  p.txt  r.txt  t.txt  v.txt  x.txt  z.txt
      2.txt  a.txt  c.txt  e.txt  g.txt  i.txt  k.txt  m.txt  o.txt  q.txt  s.txt  u.txt  w.txt  y.txt
      [root@string ~]# touch {abc,bcd,fgh}.txt
      [root@string ~]# ls
      abc.txt  bcd.txt  fgh.txt  
      

文件查询

  • 查看文件夹内有哪些文件(ls)

    • 格式:ls [参数] [路径]

      [root@string ~]# ls
      abc  abc.txt  {a...c}.txt  bcd.txt  dir  fgh.txt  one  test  系统优化.md
      
    • 参数:

      • -a : 查看隐藏文件

        [root@string ~]# ls -a
        .   abc      {a...c}.txt    .bash_logout   .bashrc  .cshrc  fgh.txt   one   .ssh     test      系统优化.md
        ..  abc.txt  .bash_history  .bash_profile  bcd.txt  dir 
        
      • -l : 显示文件详情

        [root@string ~]# ls -l
        total 8
        drwxr-xr-x  3 root root   17 Mar  5 12:21 abc
        -rw-r--r--  1 root root    0 Mar  5 12:35 abc.txt
        -rw-r--r--  1 root root    0 Mar  5 12:35 {a...c}.txt
        -rw-r--r--  1 root root    0 Mar  5 12:35 bcd.txt
        
        # 说明
        - : 普通文件
        d : 目录文件
        l : 链接文件
        s : 套接字文件
        c : 设备文件
        b : 设备文件
        p : 管道文件
        
      • -i : 打印文件的索引号

        [root@string ~]# ls -il
        total 8
        17307582 drwxr-xr-x  3 root root   17 Mar  5 12:21 abc
        33574993 -rw-r--r--  1 root root    0 Mar  5 12:35 abc.txt
        
      • -h : 显示可读文件大小

        [root@string ~]# ls -lh
        total 8.0K
        drwxr-xr-x  2 root root 4.0K Mar  5 12:34 test
        -rw-r--r--. 1 root root 2.3K Mar  4 10:46 系统优化.md
        
      • -d : 显示目录本身,而不是显示目录内容

        [root@string ~]# ls -d /etc/sysconfig/
        /etc/sysconfig/
        [root@string ~]# 
        
      • -F : 给文件添加一个标识符

        [root@string ~]# ls -F /root/
        abc/  abc.txt  {a...c}.txt  bcd.txt  dir/  fgh.txt  one/  test/  系统优化.md
        [root@string ~]# ls /root/
        abc  abc.txt  {a...c}.txt  bcd.txt  dir  fgh.txt  one  test  系统优化.md
        [root@string ~]# /bin/ls -F /root/
        abc/  abc.txt  {a...c}.txt  bcd.txt  dir/  fgh.txt  one/  test/  系统优化.md
        
  • 查看一个文件的内容(cat)

    • 格式:cat [参数] [查看路径]
    [root@string ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=none
    DEFROUTE=yes
    
  • 查看文件开头(head:从头打印文件,默认打印前10行)

    • 格式 :head [参数] [路径]

      [root@string ~]# head /etc/sysconfig/network-scripts/ifcfg-eth0 
      TYPE=Ethernet
      PROXY_METHOD=none
      BROWSER_ONLY=no
      BOOTPROTO=none
      DEFROUTE=yes
      IPV4_FAILURE_FATAL=no
      IPV6INIT=yes
      IPV6_AUTOCONF=yes
      IPV6_DEFROUTE=yes
      IPV6_FAILURE_FATAL=no
      
    • 参数:

      • -n : 打印出前n行

        [root@string ~]# head -n 2  /etc/sysconfig/network-scripts/ifcfg-eth0 
        TYPE=Ethernet
        PROXY_METHOD=none
        
      • -c : 打印出前n个字节内容

        [root@string ~]# head -c 100 /etc/sysconfig/network-scripts/ifcfg-eth0 
        TYPE=Ethernet
        PROXY_METHOD=none
        BROWSER_ONLY=no
        BOOTPROTO=none
        DEFROUTE=yes
        IPV4_FAILURE_FATAL=no
        IP
        
  • 查看文件结尾(tail 从尾部开始打印文件,默认打印10行)

    • 格式:tail [参数] [路径]

      [root@string ~]# tail /etc/sysconfig/network-scripts/ifcfg-eth0 
      IPV6_ADDR_GEN_MODE=stable-privacy
      NAME=eth0
      UUID=40fd9db3-b150-435d-a610-32285fc596d2
      DEVICE=eth0
      ONBOOT=yes
      IPADDR=192.168.15.100
      PREFIX=24
      GATEWAY=192.168.15.2
      DNS1=114.114.114.114
      IPV6_PRIVACY=no
      
    • 参数:

      • -n : 打印出文件最后n行的内容。

        [root@string ~]# tail -n 2 /etc/sysconfig/network-scripts/ifcfg-eth0
        DNS1=114.114.114.114
        IPV6_PRIVACY=no
        
      • -c :从文件底部开始输出n个字节的内容

        [root@string ~]# tail -c 50 /etc/sysconfig/network-scripts/ifcfg-eth0
        192.168.15.2
        DNS1=114.114.114.114
        IPV6_PRIVACY=no
        
      • -f : 实时打印出文件新增内容

        [root@string ~]# echo "string1" >> abc.txt
        
  • 查看文件夹层级的命令(tree)

    • 移动文件

      • mv

        • 格式:mv [目标文件路径] [目的文件路径]

          [root@string ~]# mv abc.txt /opt/
          [root@string ~]# ls
          abc      {a...c}.txt  dir      one   系统优化.md
          abc.rpm  bcd.txt      fgh.txt  test
          [root@string ~]# ls /opt/
          abc.txt
          
          # 重命名:移动到当前文件夹中
          [root@string ~]# mv abc.rpm  abcd.rpm
          [root@string ~]# ls
          abc       {a...c}.txt  dir      one   系统优化.md
          abcd.rpm  bcd.txt      fgh.txt  test
          [root@string ~]# mv /root/abcd.rpm /root/abc.rpm
          [root@string ~]# ls
          abc      {a...c}.txt  dir      one   系统优化.md
          abc.rpm  bcd.txt      fgh.txt  test
          
  • 删除

    • rm

      • 格式:rm [参数] [操作对象路径]

        [root@string ~]# ls
        abc      {a...c}.txt  dir      one   系统优化.md
        abc.rpm  bcd.txt      fgh.txt  test
        [root@string ~]# rm fgh.txt 
        rm: remove regular empty file ‘fgh.txt’? y
        [root@string ~]# ls
        abc  abc.rpm  {a...c}.txt  bcd.txt  dir  one  test  系统优化.md
        [root@string ~]# 
        
      • 参数:

        • -f : 免交互输入

          [root@string ~]# touch oldboy
          [root@string ~]# ls
          abc      {a...c}.txt  dir     one   系统优化.md
          abc.rpm  bcd.txt      oldboy  test
          [root@string ~]# rm oldboy 
          rm: remove regular empty file ‘oldboy’? 
          [root@string ~]# ls
          abc      {a...c}.txt  dir     one   系统优化.md
          abc.rpm  bcd.txt      oldboy  test
          [root@string ~]# rm -f oldboy 
          [root@string ~]# ls
          abc  abc.rpm  {a...c}.txt  bcd.txt  dir  one  test  系统优化.md
          
        • -i : 增加删除前的提示

          [root@string ~]# ls
          abc  abc.rpm  {a...c}.txt  dir  one  test  系统优化.md
          [root@string ~]# /usr/bin/rm -i abc.rpm 
          /usr/bin/rm: remove regular file ‘abc.rpm’? 
          
        • rm简写

          [root@string ~]# rm abc.rpm 
          rm: remove regular file ‘abc.rpm’? ^C
          [root@string ~]# \rm abc.rpm 
          [root@string ~]# \rm  等价于  /usr/bin/rm
          
posted @ 2021-03-09 14:51  小绵  阅读(300)  评论(0编辑  收藏  举报