Linux Bash 通配符 及用户管理

文件名通配

匹配模式元字符

*:匹配任意长度的任意字符

?:匹配任意单个字符

[]:匹配指定范围内的任意单个字符
  有几种特殊格式:
      [a-z], [A-Z], [0-9], [a-z0-9]
      [[:upper:]]:所有大写字母
      [[:lower:]]:所有小写字母
      [[:alpha:]]:所有字母
      [[:digit:]]:所有数字
      [[:alnum:]]:所有的字母和数字
      [[:space:]]:所有空白字符
      [[:punct:]]:所有标点符号            

[^]:匹配指定范围外的任意单个字符
    [^[:upper:]]
    [^0-9]
    [^[:alnum:]]

通配符使用实例

# /etc 目录下以pa开头的文件或目录
# ls -dl /etc/pa*
drwxr-xr-x. 2 root root 4096 11月 26 02:46 /etc/pam.d
-rw-r--r--. 1 root root 1023 11月 28 16:02 /etc/passwd
-rw-r--r--. 1 root root  959 11月 28 15:12 /etc/passwd-

# /etc目录下以passw开头,任意一个字符结尾的文件或目录
# ls -dl /etc/passw?
-rw-r--r--. 1 root root 1023 11月 28 16:02 /etc/passwd

# /etc目录下以大写字母开头的文件或目录
# ls -dl /etc/[[:upper:]]*
-rw-r--r--. 1 root root 5090 10月 30 2018 /etc/DIR_COLORS
-rw-r--r--. 1 root root 5725 10月 30 2018 /etc/DIR_COLORS.256color
-rw-r--r--. 1 root root 4669 10月 30 2018 /etc/DIR_COLORS.lightbgcolor
...

# /etc目录下以小写字母开头的文件或目录
# ls -dl /etc/[[:lower:]]*
-rw-r--r--.  1 root root              16 11月 26 01:22 /etc/adjtime
-rw-r--r--.  1 root root            1518 6月   7 2013 /etc/aliases
-rw-r--r--.  1 root root           12288 11月 26 01:25 /etc/aliases.db
drwxr-xr-x.  2 root root             236 11月 26 01:20 /etc/alternatives
...

# /etc目录下以非小写字母开头的文件或目录
# ls -dl /etc/[^[:lower:]]*
-rw-r--r--. 1 root root 5090 10月 30 2018 /etc/DIR_COLORS
-rw-r--r--. 1 root root 5725 10月 30 2018 /etc/DIR_COLORS.256color
-rw-r--r--. 1 root root 4669 10月 30 2018 /etc/DIR_COLORS.lightbgcolor
...

# /etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录
# ls -dl /etc/[^[:alpha:]][[:alpha:]]?*
-rw-r--r--. 1 root root 0 12月  9 19:12 /etc/0A_test.txt

# 复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
# cp -r /etc/p?*[^0-9] /tmp/mytest1     
# ll /tmp/mytest1/
总用量 32
drwxr-xr-x.  2 root root 4096 12月 10 14:51 pam.d
-rw-r--r--.  1 root root 1023 12月 10 14:51 passwd
-rw-r--r--.  1 root root  959 12月 10 14:51 passwd-
drwxr-xr-x. 10 root root  116 12月 10 14:51 pki
drwxr-xr-x.  2 root root   28 12月 10 14:51 plymouth
drwxr-xr-x.  2 root root    6 12月 10 14:51 popt.d
drwxr-xr-x.  2 root root  154 12月 10 14:51 postfix
drwxr-xr-x.  3 root root  123 12月 10 14:51 ppp
drwxr-xr-x.  2 root root   78 12月 10 14:51 prelink.conf.d
-rw-r--r--.  1 root root  233 12月 10 14:51 printcap
-rw-r--r--.  1 root root 1819 12月 10 14:51 profile
drwxr-xr-x.  2 root root 4096 12月 10 14:51 profile.d
-rw-r--r--.  1 root root 6545 12月 10 14:51 protocols
drwxr-xr-x.  2 root root   35 12月 10 14:51 python

tr 命令

# man tr
NAME
       tr - translate or delete characters
SYNOPSIS
       tr [OPTION]... SET1 [SET2]
DESCRIPTION
       Translate, squeeze, and/or delete characters from standard input, writing to standard output.

# 常见用法
用法1:把输入的数据当中的字符,凡是在SET1定义范围内出现的,通通对位转换为SET2出现的字符
    tr SET1 SET2 < /PATH/FROM/SOMEFILE
用法2:把输入的数据当中的字符,凡是在SET1定义范围内出现的,通通删除
    tr -d SET1 < /PATH/FROM/SOMEFILE

# 使用实例

# 将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
# cat /etc/issue | tr [a-z] [A-Z] > /tmp/issue.out
# cat /tmp/issue.out
\S
KERNEL \R ON AN \M

# 将/etc/issue文件中的内容空白字符全部删除后保存至/tmp/issue.out文件中
# cat /etc/issue | tr -d [:space:] > /tmp/issue.out
# cat /tmp/issue.out 
\SKernel\ronan\m

Linux 用户及用户组管理

用户管理相关命令

  • 创建用户命令:useradd
  • 删除用户命令:userdel
  • 修改密码命令:passwd
  • 修改用户账户:usermod

用户组管理相关命令

  • 创建用户组:groupadd
  • 删除用户组:groupdel
  • 修改用户组:groupmod
  • 修改用户组密码:gpasswd

用户管理命令实例

(1)、创建组distro,其GID为2019;

# groupadd -g 2019 distro

(2)、创建用户mandriva, 其ID号为1005;基本组为distro;

# useradd -u 1005 -g 2019 distro
# id distro
uid=1005(distro) gid=2019(distro) 组=2019(distro)

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

# useradd -u 1100 -d /home/linux mageia

(4)、给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期

# echo "mageedu" | passwd --stdin mageia
# passwd -x 7 mageia

(5)、删除mandriva,但保留其家目录;

# userdel mandriva

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

# useradd -u 2002 -g distro -G peguin slackware          
# id slackware
uid=2002(slackware) gid=2019(distro) 组=2019(distro),2020(peguin)

(7)、修改slackware的默认shell为/bin/tcsh;

# usermod -s /bin/tcsh slackware
# su - slackware
$ echo $SHELL
/bin/tcsh

(8)、为用户slackware新增附加组admins;

# id slackware
uid=2002(slackware) gid=2019(distro) 组=2019(distro),2020(peguin)
# usermod -G distro,peguin,admins slackware
# id slackware
uid=2002(slackware) gid=2019(distro) 组=2019(distro),2020(peguin),2021(admins)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容