Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 460|回复: 0

linux文件的操作

[复制链接]
  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2013-3-11 23:18:44 | 显示全部楼层 |阅读模式

    检查文件

    ls –l     以长模式查看文件的详细信息


    file 检查文件类型


    cd 和cd ~ 都是直接回到家


    文件类型

    在linux中,所有的东西都被当成文件。

    文件权限前的第一个字母用来标识文件类型

    -:        一般文件

    d:        目录文件

    b:        块设备文件

    c:        字符设备文件

    l:        链接文件,类型windows系统中的快捷方式

    p:        人工管道(管道文件)


    文件权限

    对于每一个文件,Linux都提供了一套文件权限系统。

    文件权限系统,将操作文件的用户都分成三类。

    文件的拥有者(u)

    文件所属组的成员(g)

    其他用户(o)


    文件权限类型

    读(r):用户是否有权限读取文件

    写(w):用户是否有权限写文件

    执行(x):用户是否有权限执行文件


    例:[root@localhost root]# ls -l

    total 32

    - rw- r-- r-- 1 root root 1201 Oct 21 05:37 anaconda-ks.cfg

    d rwx r-x r-x 12 luowei luowei 4096 Oct 22 11:57 httpd-2.2.4

    文件类型 u g o 文件硬连接参数 文件的拥有者 文件的所属群组 文件大小


    更改文件的权限

    例:

    [root@localhost root]# su - luowei

    [luowei@localhost luowei]$ ls

    dirtest test2

    [luowei@localhost luowei]$ ls -l

    total 8

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -rw-rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

    [luowei@localhost luowei]$ chmod u-w test2    删除拥有者对此文件的写的权限

    [luowei@localhost luowei]$ ls -l

    total 8

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -r--rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

    [luowei@localhost luowei]$ chmod u+w test2    给拥有者添加对此文件写的的权限

    [luowei@localhost luowei]$ ls -l

    total 8

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -rw-rw-r-- 1 luowei luowei 149 Oct 22 10:30 test2

    [luowei@localhost luowei]$ chmod g-w test2    删除此群组对此文件的写权限

    [luowei@localhost luowei]$ ls -l

    total 8

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -rw-r--r-- 1 luowei luowei 149 Oct 22 10:30 test2

    [luowei@localhost luowei]$ chmod o+w test2    给其他人添加对此文件的写权限

    [luowei@localhost luowei]$ chmod g+x test2    给此群组添加对此文件的执行权限

    [luowei@localhost luowei]$ chmod u=rwx test2 给拥有者设置对此文件读写执行权限


    通过数字的方式来更改

    4 读    2 写    1 执行

    [luowei@localhost luowei]$ chmod 644 test2 设置权限

    [luowei@localhost luowei]$ ls -l

    total 8

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -rw-r--r-- 1 luowei luowei 149 Oct 22 10:30 test2


    只有文件的拥有者和root才可以改变文件的权限


    例2:

    [luowei@redhat luowei]$ su - root

    Password:

    [root@redhat root]# cd /home/luowei

    [root@redhat luowei]# mkdir test

    [root@redhat luowei]# chown luowei:luowei test    修改test文件夹的所属用户及用户组

    [root@redhat luowei]# chmod 677 test        设置它的权限,拥有者只能读和写,不能执行

    [root@redhat luowei]# su - luowei

    [luowei@redhat luowei]$ ls -l

    总用量 4

    -rwxrwxrw- 1 luowei luowei 0 10月 24 19:45 aa

    drw-rwxrwx 2 luowei luowei 4096 10月 24 19:54 test

    [luowei@redhat luowei]$ cd test

    -bash: cd: test: 权限不够


    建立链接

    硬链接

    语法:ln 源文件 新建链接名

    例:

    [luowei@localhost luowei]$ ln /home/luowei/test.txt tom.txt 建立硬链接文件

    [luowei@localhost luowei]$ ls

    dirtest test.txt tom.txt

    [luowei@localhost luowei]$ ls -l

    total 12

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    -rw-r--r-- 2 luowei luowei 149 Oct 22 10:30 test.txt

    -rw-r--r-- 2 luowei luowei 149 Oct 22 10:30 tom.txt

    [luowei@localhost luowei]$ echo "hello world" >/home/luowei/test.txt 输入内容

    [luowei@localhost luowei]$ cat /home/luowei/test.txt

    hello world

    [luowei@localhost luowei]$ cat tom.txt

    hello world

    [luowei@localhost luowei]$ rm -f /home/luowei/test.txt    删除链接文件

    [luowei@localhost luowei]$ cat tom.txt        打开文件

    hello world

    [luowei@localhost luowei]$

    软链接

    语法:ln –s 源文件 新建链接名

    例:

    [luowei@localhost luowei]$ ls

    dirtest tom.txt

    [luowei@localhost luowei]$ touch /home/luowei/test.txt

    [luowei@localhost luowei]$ echo "hello" >/home/test.txt

    -bash: /home/test.txt: No such file or directory

    [luowei@localhost luowei]$ echo "hello" >/home/luowei/test.txt    //另一个方式向文件中写入文件

    [luowei@localhost luowei]$ ln -s /home/luowei/test.txt jack.txt

    [luowei@localhost luowei]$ ls -l

    total 12

    drwxrwxr-x 2 luowei luowei 4096 Oct 22 10:34 dirtest

    lrwxrwxrwx 1 luowei luowei 21 Oct 22 13:55 jack.txt -> /home/luowei/test.txt

    -rw-rw-r-- 1 luowei luowei 6 Oct 22 13:55 test.txt

    -rw-r--r-- 1 luowei luowei 12 Oct 22 13:49 tom.txt

    [luowei@localhost luowei]$ cat jack.txt

    hello

    [luowei@localhost luowei]$ rm -f /home/luowei/test.txt

    [luowei@localhost luowei]$ cat jack.txt

    cat: jack.txt: No such file or directory

    [luowei@localhost luowei]$


    注:硬链接不能给目录做链接,软链接可以给目录做链接。


    ext2/3中文件的构成

    在ext2和ext3文件系统中,文件以inod+block的方式存在。一旦用rm指令删除文件中的inode记录。文件无法被找回。stat 指令可以用来检查文件的block与inode状况。


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-25 06:35 , Processed in 0.400848 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表