【Linux学习】--pwd命令、$0命令、chmod命令

来源:互联网 发布:php session共享实现 编辑:程序博客网 时间:2024/06/14 23:55

1.pwd命令

“print name of current/working directory”

2.$0

$0是Bash环境下的特殊变量,其真实含义是:

   Expands to the name of the shell or shell script. This is set at shell initialization.  If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

   这个$0有可能是好几种值,跟调用的方式有关系:

(1)使用一个文件调用bash,那$0的值,是那个文件的名字(没说是绝对路径噢)

(2)使用-c选项启动bash的话,真正执行的命令会从一个字符串中读取,字符串后面如果还有别的参数的话,使用从$0开始的特殊变量引用(跟路径无关了)

(3)除此以外,$0会被设置成调用bash的那个文件的名字(没说是绝对路径)

3.chmod

chmod [-cfvR] [--help] [--version] mode file...

Change the mode of each FILE to MODE.

参数说明:

1. -cfvR 部分

       -c : 若该档案权限确实已经更改,才显示其更改动作

       -f : 若该档案权限无法被更改也不要显示错误讯息

       -v : 显示权限变更的详细资料

       -R : 对目前目录下的所有档案与子目录进行相同的权限变更(即以递回的方式逐个变更), 这个-R 用的还是很多的。

 

2.  Mode 部分

这部分可以分成如下3块: [who] operator [permission]

[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.

 

who的含义是:

        u 文件属主权限

         g 同组用户权限

        o 其它用户权限

        a 所有用户(包括以上三种)

operator的含义:

       + 增加权限

      - 取消权限

        =  唯一设定权限

permission的含义:

        r 读权限

       w 写权限

       x 执行权限

       X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。

        s 文件属主和组id

        l 给文件加锁,使其它用户无法访问

3. 示例:

(1)将档案 file1.txt 设为所有人皆可读取 :

              chmod ugo+r file1.txt 

(2)将档案 file1.txt 设为所有人皆可读取 :

              chmod a+r file1.txt  
(3)将档案 file1.txt 与 file2.txt 设为该档案拥有者,与其所属同一个群体者可写入,但其他以外的人则不可写入 :

               chmod ug+w,o-w file1.txt file2.txt 

(4)将 ex1.py 设定为只有该档案拥有者可以执行 :

                chmod u+x ex1.py 

(5)将目前目录下的所有档案与子目录皆设为任何人可读取 :

                chmod -R a+r * 

        (6)收回所有用户的对file1的执行权限

                chmod a-x file1

 

4.1 先看一下文件的权限格式

 

[root@qs-wg-db2 scripts]# ll

total 20

-rw-r--r-- 1 oracle oinstall    0 Feb 24 00:00 alertlogbyday.log

-rwxr-xr-x 1 oracle oinstall  430 Feb 20 01:10 alertlogbyday.sh

-rwxr-xr-x 1 oracle oinstall    7 Feb 24 05:00 del_st_arch.log

-rwxr-xr-x 1 oracle oinstall  648 Feb 19 00:51 del_st_archive.sh

-rwxr-xr-x 1 oracle oinstall    9 Feb 24 05:00 max_sn.log

drwxr-xr-x 3 root   root     4096 Feb 23 23:40 pymonitor

  ll的结果返回七列,分别表示如下含义:

第一栏  [文件属性]

第二栏  [文件数]

第三栏  [拥有者]

第四栏  [所有者群组]

第五栏  [大小]

第六栏  [建档日期]

第七栏  [档名]

 

我们设置文件的权限就是这是第一栏里的文件属性。

 

文件属性这块共有十个字段,如:drwxr-xr-x

  我们把这10个列分成4块:[d] [rwx] [r-x] [r-x]

第一块:也就是第一列,用来表示这个文件的类型,有如下值:

        (1)[ d ]则是目录,我这里的是d,表示的是一个目录

        (2)[ - ]则是文件;

        (3)[ l ]则表示为连结档(link file);

        (4)[ b ]则表示为装置文件里面的可供储存的接口设备;

        (5)[ c ]则表示为装置文件里面的串行端口设备,例如键盘、鼠标。

第二块:第 2到4 列,表示文件拥有者的权限。

第三块:第5到7 列,表示拥有者同组人的权限。

第四块:第8到10列,表示是非拥有者组人的权限。

 

        这些权限均有[rwx] 三个参数表示,而且分别对应不同的位置。每块由3个列组成,每列对应一个值。 [ r ]代表可读、[ w ]代表可写、[ x ]代表可执行。

        举例: 如果拥有者只有只读的权限,那么第2到4列就是[r--],有读写的权限就是[rw-],有读写和执行的权利就是[rwx].

 4.2 使用数字赋权

在4.1 中了解准备知识之后,就可以使用数字赋权了。 每块用户有3个权限[rwx]. 他们对应数字:

       r -->4

       w-->2

       x-->1

 

使用数字赋权的命令格式如下:

       chmod abc filename

其中的abc 分别代表3个数字,并且分别对应问个不同的属组。 即:

       数字a 对应 第2到4位,表示拥有者的权限。

       数字b 对应 第5到7位,表示同组用户的权限。

       数字c 对应第8到10位,表示其他组的权限。

 

       rwx 对应4,2,1. 那么只读的权限用4表示[r--],读写用6(4+2)表示[rw-],写加执行用7(4+2+1)表示[rwx]。 只读加执行用5(4+1)表示[r-x]。

 

示例:

        chmod 755 file1

chmod 777 file1 <==> chmod a=rwx file 

        chmod 771 file  <==> chmod ug=rwx,o=x file 




0 0
原创粉丝点击