linux 目录 rwx(读写执行) 知多少?

linux/unix 下的文件 rwx 想必大家很熟悉了!但对目录的rwx是怎么理解的呢?

 

今天碰到了这个问题,一时还真回答不上来,例如:

  1. 目录执行权限能干吗?
  2. 目录只有写权限就可以写入文件到该目录了吗?

于是查看了一下资料,《Advanced Programming in the UNIX》中解释如下:

 

Note that read permission for a directory and execute permission for a directory mean different things. Read permission lets us read the directory, obtaining a list of all the filenames in the directory. Execute permission lets us pass through the directory when it is a component of a pathname that we are trying to access. (We need to search the directory to look for a specific filename.)

 

原来读权限仅仅是读取目录下的list(文件列表), 执行权限是access(访问)目录下的文件

这下明白了,见用大学宿舍一哥们唱的一首《18摸》来解释:

  1. 读权限:“只能让你看,不能让你摸”
  2. 执行权限:“只能让你摸,不能让你看”
  3. 写权限:“先让摸,后让写” 换句话 “ 摸都不让,写也就没门了” (歌词没这句,呵呵!)

 

做实验证明:

 

     root$ mkdir test/d1 -p

     root$ touch test/f1

     root$ echo "test" > test/f2

 

验证读权限:

     root$ chmod 004 test  (r读权限)

     robin$  ls test (可以看到d1, f1, f2)

     robin$  cat test/f2 (Permission denied)

     root$ chmod 005 test (rx读执行权限)

     robin$  cat test/f2  (看到“test”了)

验证执行权限:

     root$ chmod 001 test (执行权限)

     robin$ ls test (这边没有东西了哦!)

     robin$ cat test/f2(可以看到“test”)

验证写权限:

     root$ chmod 002 (w写权限)

     robin$ echo "test2" > test/f3  (Permission denied)

     root$ chmod 003 (wx写执行权限)

     robin$ echo "test2" > test/f3 (成功)

 

这下清清楚楚! 不迷惑了!

由此可以看出:如果目录没有执行权限,搜索时也不会找到文件及其内容哦!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值