zoukankan      html  css  js  c++  java
  • 003_Linux常用命令之文件操作

    1.文件操作
    • touch命令
      • 创建文件,如果文件名称不存在,那么直接创建;如果存在,那么更改访问时间
      • touch [option] filename1 filename2...
    root@ubuntu:~/Test# touch hello.c
    root@ubuntu:~/Test# ls
    hello.c
    
    • rm命令:删除文件或者目录

      • 参数 -r 递归删除子目录
      • rm -rf * 删除当前目录内全部内容(强制删除,谨慎使用)
    • cp和mv命令,相当于Windows平台复制和剪切

      • cp [option] srcpath despath
      • despath是一个目录,将srcpath拷贝到despath目录下
      • despath不是一个目录,在despath上级目录(.../xxx),在.../下创建一个xxx文件,并将srcpath的内容拷贝进来
    • cat命令

      • cat filename:直接显示文件信息到屏幕
    • more和less,分屏幕显示文件信息

      • more:

        • 回车逐行显示
        • 空格,一页一页的显示
      • less:

        • 回车或者上下方向键可以反复查看文件内容
    • head和tail命令

      • head 查看文件头,默认显示10行内容
        • head -n 可以指定的行数
      • tail查看文件尾,默认显示10行内容
        • -n 可以指定函数
        • -f 可以跟踪文件末尾
    2.统计信息相关
    • wc命令:英文单词为word cout,也就是统计文件内容
      • -l 显示行
      • -w 单词
      • -c 字节数
    root@ubuntu:~/Test# wc hello.c 
     8 10 91 hello.c
    root@ubuntu:~/Test# wc -l hello.c 
    8 hello.c
    root@ubuntu:~/Test# wc -w hello.c 
    10 hello.c
    root@ubuntu:~/Test# wc -c hello.c 
    91 hello.c
    
    • df 显示磁盘空间信息
    root@ubuntu:~# df -h 
    文件系统        容量  已用  可用 已用% 挂载点
    udev            973M     0  973M    0% /dev
    tmpfs           199M  9.0M  190M    5% /run
    /dev/sda1        21G  9.2G   11G   48% /
    tmpfs           992M  256K  992M    1% /dev/shm
    tmpfs           5.0M  4.0K  5.0M    1% /run/lock
    tmpfs           992M     0  992M    0% /sys/fs/cgroup
    tmpfs           199M   60K  199M    1% /run/user/1000
    
    3.文件权限和用户属性
    • 解释相应的字段
    root@ubuntu:~/Test# ls -l
    总用量 4
    -rw-r--r-- 1 root root 91 10月 27 14:16 hello.c
    
    • -表示文件类型,d代表目录文件

    • rw- 归属用户的权限,该用户具有可读可写的权限

    • r-- 归属组的权限,该组仅有可读权限

    • r-- 其他用户权限,也是只具有可读权限

    • 我们还可以用8进制的数字来表示权限位

      • rw- --->110--->6 用户位
      • r-- --->100--->4 组权限位
      • r-- --->100--->4 其他权限位
      • 最后将他们组合就是起来0664
    • 哦,对了,后面还有一个1,那个1代表硬链接的计数,下面会有命令来进行演示

    • 创建硬链接-ln src des

    root@ubuntu:~/Test# ln hello.c hello.c.hard
    root@ubuntu:~/Test# ls
    hello.c  hello.c.hard
    root@ubuntu:~/Test# ln hello.c hello.c.hard1
    root@ubuntu:~/Test# ls -l
    总用量 12
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c.hard
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c.hard1
    

    此时的硬链接计数变成了3,当我们的硬链接的计数变为0的时候,那么文件也会被删除

    • 创建软链接:ln -s 文件或者目录
    root@ubuntu:~/Test# ln -s hello.c hello.c.soft
    root@ubuntu:~/Test# ls -l
    总用量 12
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c.hard
    -rw-r--r-- 3 root root 91 10月 27 14:16 hello.c.hard1
    lrwxrwxrwx 1 root root  7 10月 27 14:43 hello.c.soft -> hello.c
    
    • 删除软硬链接:unlink
    4.改变文件权限
    • chmod命令

      • chmod [u|g|o|a] [+|-][r|w|x] filename
      • 用数字的方式改变文件权限 例如:chmod 0664 main.c
    • chown和chgrp改变用户和改变组

      • 如果当前不是root用户,需要用管理员修改文件归属
      • chown 用户:组 文件名|目录
      • chgrp 组 文件名|目录
  • 相关阅读:
    jqeuery $.ajax 与后台jsone交互
    SpringMVC3.2+JPA使用注解的方式环境搭建
    SpringMVC+JPA使用注入的方式环境搭建
    EasyUI_tree根据数据库数据生成树形结构JSON格式
    ACM竞赛高手比其他程序员水平高很多吗?
    hdu 1028 Ignatius and the Princess III(DP)
    9513 防空洞
    hdu 1398 Square Coins(简单dp)
    FZU 1608 Huge Mission(线段树)
    poj2528 Mayor's posters(线段树之成段更新)
  • 原文地址:https://www.cnblogs.com/LittleFishC/p/13208603.html
Copyright © 2011-2022 走看看