Linux入门基础——常用命令(三)

1.文件权限管理命令

管理员root和文件所有者可以更改文件权限

 1.1 文件权限管理命令chmod

命令名称:chmod
命令英文原意:change the permissions mode of a file
命令所在路径:/bin/chmod
执行权限:所有用户
语法:chmod [{ugoa}{±=}{rwx}] [文件或目录]
   chmod [mode=421 ] [文件或目录]
   -R 递归修改 改变目录权限的同时,会把目录下所有的文件改成这个权限
功能描述:改变文件或目录权限

权限的数字表示:
r——4
w——2
x——1
例如:
rwx rw- r–
7  6  4
rwx r-x r-x
7  5  5


u表示文件所有者,g表示文件所属组, o表示文件的其他人, a表示这三者都是。

代表字符 权限 对文件的含义 对目录的含义
r 读权限 可以查看文件内容 可以列出目录中的内容
w 写权限 可以修改文件内容 可以在目录中创建、删除文件
x 执行权限 可以执行文件 可以进入目录

权限管理命令:chmod
范例:

[cfp@bogon for]$ chmod g+w testfile
赋予文件testfile所属组写权限
[cfp@bogon for]$ chmod -R 777 testdir
修改目录testfile及其目录下文件为所有用户具 有全部权限

实例1:

[cfp@bogon for]$ ls -l a.sh
-rw-rw-r--. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod u+x a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw-r--. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod g+w,o-r a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw----. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod o=rwx a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw-rwx. 1 cfp cfp 0 Mar 11 16:51 a.sh

实例2:

[cfp@bogon for]$ ls -l b.sh
-rw-rw-r--. 1 cfp cfp 0 Mar 11 16:51 b.sh
[cfp@bogon for]$ chmod 755 b.sh
[cfp@bogon for]$ ls -l b.sh
-rwxr-xr-x. 1 cfp cfp 0 Mar 11 16:51 b.sh
[cfp@bogon for]$ chmod 777 b.sh
[cfp@bogon for]$ ls -l b.sh
-rwxrwxrwx. 1 cfp cfp 0 Mar 11 16:51 b.sh

 1.2 其他文件权限管理命令

命令名称:chown
命令英文原意:change file ownership
命令所在路径:/bin/chown
执行权限:所有用户
语法: chown [用户] [文件或目录]
功能描述:改变文件或目录的所有者
范例:[root@bogon cfp]# chown Liangjie d.txt
    #改变文件d.txt的所有者为Liangjie

只有管理员root可以改变文件所有者

[root@bogon ~]# useradd Liangjie
[root@bogon ~]# cd /home/cfp
[root@bogon cfp]# ls -l d.txt
-rw-rw-r--. 1 cfp cfp 0 Mar  7 09:16 d.txt
[root@bogon cfp]# chown Liangjie d.txt
[root@bogon cfp]# ls -l  d.txt
-rw-rw-r--. 1 Liangjie cfp 0 Mar  7 09:16 d.txt

命令名称:chgrp
命令英文原意:change file group ownership
命令所在路径:/bin/chown
执行权限:所有用户
语法: chgrp [用户] [文件或目录]
功能描述:改变文件或目录的所属组
范例:[root@bogon cfp]# chgrp lamp d.txt
    #改变文件longze的所属组为Liangjie

[root@bogon cfp]# groupadd lamp    #增加一个所属组lamp
[root@bogon cfp]# chgrp lamp d.txt
[root@bogon cfp]# ls -l  d.txt
-rw-rw-r--. 1 Liangjie lamp 0 Mar  7 09:16 d.txt

命令名称:umask
命令英文原意:the user file-creation mask
命令所在路径:Shell内置命令
执行权限:所有用户
语法: umask [-S]
    -S 以rwx形式显示新建文件缺省权限(文件缺省权限指创建文件或目录时的默认权限)
功能描述:显示、设置文件的缺省权限
范例:[root@bogon cfp]# umask -S

[root@bogon cfp]# umask -S
u=rwx,g=rx,o=rx
[root@bogon cfp]# mkdir liuyi
[root@bogon cfp]# ls -ld liuyi
drwxr-xr-x. 2 root root 4096 Mar 13 14:52 liuyi
[root@bogon cfp]# touch liu
#因为liu是一个文件,Linux中默认所有新创建的文件可执行权限都会被删除。
[root@bogon cfp]# ls -l liu
-rw-r--r--. 1 root root 0 Mar 13 14:48 liu

2.文件搜索命令

 2.1 文件搜索命令find

命令名称:find
命令所在路径:/bin/find
执行权限:所有用户
语法: find [搜索范围] [匹配条件]
功能描述:文件搜索

$ find /etc -name init
在目录/etc 中查找文件init
-iname 不区分大小写
-name 严格区分大小写

[cfp@bogon ~]$ find /home/cfp/text -name a.out
/home/cfp/text/socket/a.out
/home/cfp/text/a.out

 #通配符,只要文件名里面有'out',就会被显示出来
[cfp@bogon ~]$ find /home/cfp/text -name *out*   
/home/cfp/text/socket/a.out
/home/cfp/text/Cshell.out
/home/cfp/text/a.out
/home/cfp/text/b.out

$ find / -size +204800
在根目录下查找大于100MB的文件。Linux中一个数据块是512字节=0.5k

$ find /home -user Liangjie
在根目录下查找所有者为Liangjie的文件
$ find /home -group Liangjie
在根目录下查找所属组为Liangjie的文件

$ find /etc -cmin -5
在/etc下查找5分钟内被修改过属性的文件和目录
-amin 访问时间 access
-cmin 文件属性 change
-mmin 文件内容 modify

$ find /etc -size +163840 -a -size 204800
在/etc下查找大于80MB小于100MB的文件
-a 两个条件同时满足
-o 两个条件满足一个即可

$ find /etc -name inittab -exec ls -l {} ;
在/etc下查找inittab文件并显示其详细信息
-exec 无确认
-ok 对搜索到的结果,一个一个进行确认,再执行

[root@bogon cfp]# find /etc -name inittab
/etc/inittab
[root@bogon cfp]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 490 Oct 19 10:16 /etc/inittab

$ find /etc -inum 351351
查找文件id号为351351的文件

[root@bogon text]# ls -i
958149 a.out  958198 Cshell.c    957139 s
958178 b.out  958187 Cshell.out  958081 socket
[root@bogon text]# find /home/cfp/text -inum 957139 -exec rm {} \;
[root@bogon text]# ls
a.out  b.out  Cshell.c  Cshell.out  socket

$ find /etc -type TYPE 查找文件类型为TYPE的文件
f为文件,d为目录,l为软链接文件

[cfp@bogon text]$ ls -l
total 76
-rwxrwxr-x. 1 cfp cfp 22808 Apr  4 16:42 a.out
-rwxrwxr-x. 1 cfp cfp 22968 Apr  4 18:11 b.out
-rw-rw-r--. 1 cfp cfp  2846 Apr  4 18:28 Cshell.c
-rwxrwxr-x. 1 cfp cfp 22976 Apr  4 18:28 Cshell.out
[cfp@bogon text]$ find /home/cfp/text -type f
/home/cfp/text/.serverblock.c.swp
/home/cfp/text/Cshell.out
/home/cfp/text/a.out
/home/cfp/text/b.out
/home/cfp/text/Cshell.c

 2.2 其他文件搜索命令

命令名称:locate
命令所在路径:/usr/bin/locate
执行权限:所有用户
语法:locate 文件名
功能描述:在文件资料库中查找文件(速度块),Linux定期会维护一个文件资料库。但是/tmp目录下无法搜索到
范例:$ locate -i inittab
   # -i 不区分大小写
   $ updatedb
   #更新文件资料库

[cfp@bogon text]$ locate inittab
/etc/inittab
/usr/share/augeas/lenses/dist/inittab.aug
/usr/share/man/zh_CN/man5/inittab.5.gz
/usr/share/vim/vim82/syntax/inittab.vim
#创建一个Liangjie文件,使用locate无法查到它的位置。updatedb之后,就可查看到了
[cfp@bogon text]$ touch Liangjie
[cfp@bogon text]$ locate -i liangjie
[root@bogon text]# updatedb
[root@bogon text]# locate Liangjie
/home/cfp/text/Liangjie

命令名称:which
命令所在路径:/usr/bin/which
执行权限:所有用户
语法:which 命令
功能描述:搜索命令所在绝对路径及别命信息
范例:$ which ls

[cfp@bogon ~]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls

命令名称:whereis
命令所在路径:/usr/bin/whereis
执行权限:所有用户
语法:whereis [命令名称]
功能描述:搜索命令所在绝对路径及帮助文档路径
范例:$ whereis ls

命令名称:grep
命令所在路径:/bin/grep
执行权限:所有用户
语法:grep -iv [指定字串] [文件]
功能描述:在文件中搜寻字串匹配的行并输出
-a 将binary文件以text文件的方式查找数据,不要忽略二进制数据
-i 不区分大小写
-v 排除指定字符串
-c 计算查找到字符串的次数
范例:$ grep mysql /root/install.log
   在/root/install.log文件中查找含 mysql 的行
   $ grep -v ^# /etc/inittab
   显示把以#开头的行去掉的内容

3.帮助命令

 3.1 Linux 内部命令、外部命令

  Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但也有些细微差别。

  内部命令实际上是shell程序的一部分,其中包含的是一些比较简单的linux系统命令,这些命令由shell程序识别并在shell程序内部完成运行,通常在linux系统加载运行时shell就被加载并驻留在系统内存中。内部命令是写在bashy源码里面的,其执行速度比外部命令快,因为解析内部命令shell不需要创建子进程。比如:exit,history,cd,echo等。

  外部命令是linux系统中的实用程序部分,因为实用程序的功能通常都比较强大,所以其包含的程序量也会很大,在系统加载时并不随系统一起被加载到内存中,而是在需要时才将其调用内存。通常外部命令的实体并不包含在shell中,但是其命令执行过程是由shell程序控制的。shell程序管理外部命令执行的路径查找、加载存放,并控制命令的执行。外部命令是在bash之外额外安装的,通常放在/bin,/usr/bin,/sbin,/usr/sbin…等等。可通过“echo $PATH”命令查看外部命令的存储路径,比如:ls、vi等。

PS:可以用type 命令查看命令是否是内置命令:

[root@localhost account]# type cd
cd is a shell builtin

 3.2 man、help

命令名称:man
命令所在路径:/usr/bin/man
执行权限:所有用户
语法:man [命令或配置文件]
功能描述:获得帮助信息
范例:$ man ls 查看ls命令的帮助信息
   #可以使用[ /+要搜索的关键词]来标记搜索,浏览方式类似于命令 less
   $ man services
   #查看配置文件services的帮助信息

PS小贴士:使用 “ 命令 --help ”,会只出现关于这条命令的参数信息

[cfp@bogon ~]$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/touch>
or available locally via: info '(coreutils) touch invocation'

命令名称:help
命令所在路径:Shell 内置命令
执行权限:所有用户
语法:help 命令
功能描述:获得Shell内置命令的帮助信息
范例:$ help if
   #查看 if 的使用,包括while、case等

[cfp@bogon ~]$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.
    
    The `if COMMANDS' list is executed.  If its exit status is zero, then the
    `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
    executed in turn, and if its exit status is zero, the corresponding
    `then COMMANDS' list is executed and the if command completes.  Otherwise,
    the `else COMMANDS' list is executed, if present.  The exit status of the
    entire construct is the exit status of the last command executed, or zero
    if no condition tested true.
    
    Exit Status:
    Returns the status of the last command executed.


4.用户管理命令

命令名称:useradd
命令所在路径:/usr/sbin/useradd
执行权限:root
语法:useradd 用户名
功能描述:添加新用户
范例:$ useradd Liangjie

每个人可以更改自己的密码,root可以更改任何人的密码

命令名称:passwd
命令所在路径:/usr/bin/passwd
执行权限:所有用户
语法:passwd 用户名
功能描述:设置用户密码
范例:$ passwd Liangjie

命令名称:who
命令所在路径:/usr/bin/who
执行权限:所有用户
语法:who
功能描述:查看登录用户信息
范例:$ who

[root@bogon cfp]# who
liangjie pts/1        2020-04-05 18:08 (192.168.182.1)
参数信息:
登录用户名   登录终端:tty表示本地终端、pts远程终端   登陆时间

命令名称:w
命令所在路径:/usr/bin/w
执行权限:所有用户
语法:w
功能描述:查看登录用户详细信息
范例:$ w

[cfp@bogon ~]$ sudo su
[sudo] password for cfp: 
[root@bogon cfp]# useradd liangjie
[root@bogon cfp]# systemctl start sshd.service  #开启远程连接服务
[root@bogon cfp]# who
cfp      tty2         2020-04-05 17:01 (tty2)
liangjie pts/1        2020-04-05 18:08 (192.168.182.1)
[root@bogon cfp]# w
 18:11:12 up  1:11,  2 users,  load average: 0.05, 0.05, 0.01
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
cfp      tty2      17:01    1:11m  0.28s  0.20s /usr/libexec/gnome-session-binary
liangjie pts/1     18:08    2:45   0.15s  0.15s -bash

5. 总结

  文章总结了常见的文件权限管理命令、文件搜索命令、帮助命令、用户管理命令。写的都是一些常见的,用的比较多的命令,包括参数什么的…命令不在多,而在于要多练,熟能生巧这句话是很有道理的。与其说买一本几百页的Linux书装模做样的看,不如实实在在自己动手练习几个命令,就我这个Linux初学者而言,我觉得一点一点的敲命令这个办法,让很多Linux常见命令很容易记住的。

    在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章