码迷,mamicode.com
首页 > 其他好文 > 详细

*4 通配符及管道与反引号“`”与花括号“{}”的使用

时间:2017-03-22 23:53:48      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:通配符及管道与反引号“`”与花括号“{}”的使用

作业4:

一、通配符练习: 

1、显示/etc目录下,以字母开头,后面跟了一个非字母及其它任意长度任意字符的文件或目录; 

ll -dh /etc/[[:alpha:]][^[:alpha:]]*

drwxr-xr-x. 6 root root 4.0K 3月   6 17:22 /etc/X11



2、显示/usr/share/man目录下,所有以man开头,后跟一个数字结尾的文件或目录;

ll -dh /usr/share/man/man[0-9]

drwxr-xr-x. 2 root root  56K 3月   6 17:29 /usr/share/man/man1

drwxr-xr-x. 2 root root  20K 3月   6 17:29 /usr/share/man/man2

drwxr-xr-x. 2 root root  68K 3月   6 17:29 /usr/share/man/man3

drwxr-xr-x. 2 root root 4.0K 3月   6 17:29 /usr/share/man/man4

drwxr-xr-x. 2 root root  12K 3月   6 17:29 /usr/share/man/man5

drwxr-xr-x. 2 root root 4.0K 3月   6 17:29 /usr/share/man/man6

drwxr-xr-x. 2 root root  12K 3月   6 17:29 /usr/share/man/man7

drwxr-xr-x. 2 root root  28K 3月   6 17:29 /usr/share/man/man8

drwxr-xr-x. 2 root root 4.0K 9月  23 2011 /usr/share/man/man9



3、复制/etc目录下,所以p,m,r开头的,且以.conf结尾的文件或目录至/tmp/conf.d目录下;

mkdir -pv /tmp/conf.d

mkdir: 已创建目录 "/tmp/conf.d"

cp -a /etc/[p,m,r]*.conf /tmp/conf.d

ll -h /tmp/conf.d

总用量 44K

-rw-r--r--. 1 root root  827 7月  24 2015 mke2fs.conf

-rw-r--r--. 1 root root 2.6K 8月  17 2010 mtools.conf

-rw-r--r--. 1 root root 1.4K 8月  23 2010 pbm2ppa.conf

-rw-r--r--. 1 root root  370 10月 15 2014 pm-utils-hd-apm-restore.conf

-rw-r--r--. 1 root root 6.2K 8月  23 2010 pnm2ppa.conf

-rw-r--r--. 1 root root  789 7月  19 2013 prelink.conf

-rw-r--r--. 1 root root  966 11月 23 2013 readahead.conf

-rw-r--r--. 1 root root 1.5K 10月 15 2014 request-key.conf

-rw-r--r--. 1 root root  238 3月   6 17:35 resolv.conf

-rw-r--r--. 1 root root 3.1K 3月   6 17:26 rsyslog.conf


4、创建a123, cd6, c78m, c1 my, m.z, k 67, 8yu, 789等文件,并按照下述要求写出相应的命令;

  注意,以上文件是以逗号隔开的,其它符号都是文件名的组成部分;

  1) 显示所有以a或m开头的文件;

mkdir -pv /mnt/lianxi/{a123,cd6,c78m,‘c1 my‘,m.z,‘k 67‘,8yu,789}

mkdir: 已创建目录 "/mnt/lianxi"

mkdir: 已创建目录 "/mnt/lianxi/a123"

mkdir: 已创建目录 "/mnt/lianxi/cd6"

mkdir: 已创建目录 "/mnt/lianxi/c78m"

mkdir: 已创建目录 "/mnt/lianxi/c1 my"

mkdir: 已创建目录 "/mnt/lianxi/m.z"

mkdir: 已创建目录 "/mnt/lianxi/k 67"

mkdir: 已创建目录 "/mnt/lianxi/8yu"

mkdir: 已创建目录 "/mnt/lianxi/789"

ll -dh /mnt/lianxi/[a,m]*

drwxr-xr-x. 2 root root 4.0K 3月   6 17:44 /mnt/lianxi/a123

drwxr-xr-x. 2 root root 4.0K 3月   6 17:44 /mnt/lianxi/m.z


  2) 显示所有文件名中包含了数字的文件;

ll -dh /mnt/lianxi/*[0-9]*

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/789

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/8yu

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/a123

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/c1 my

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/c78m

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/cd6

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/k 67

ll -dh /mnt/lianxi/*[[:digit:]]*

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/789

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/8yu

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/a123

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/c1 my

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/c78m

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/cd6

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/k 67


  3) 显示所有以数字结尾且文件名中包含空白字符的文件;

ll -dh /mnt/lianxi/*[[:digit:]]

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/789

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/a123

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/cd6

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/k 67

ll -dh /mnt/lianxi/*[0-9]

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/789

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/a123

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/cd6

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/k 67


  4) 显示文件名中不以c字母开头并且不以数字结尾的所有文件;

ll -dh /mnt/lianxi/[^c]*[^[:digit:]]

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/8yu

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/m.z

ll -dh /mnt/lianxi/[^c]*[^0-9]

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/8yu

drwxr-xr-x. 2 root root 4.0K 3月   6 17:40 /mnt/lianxi/m.z


二、管道及IO重定向练习:

1、统计/usr/bin/目录下的文件个数;

ll -a /usr/bin/|wc -l

1443


2、取出/etc/passwd文件中第9至第15个用户的用户名和shell,并将其保存至/tmp/users文件中;

head -n 15 /etc/passwd|tail -n 7|tee /tmp/users

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin


3、显示/etc目录下所有以.conf开头的文件或目录,并统计其个数;

ll -dh /etc/.conf*|wc -l

ls: 无法访问/etc/.conf*: 没有那个文件或目录

0


4、把/etc/passwd文件最后三行信息中所有小写字符改为大写,并且删除所有的":"、"/"以及数字字符;

tail -n 3 /etc/passwd|tr ‘a-z‘ ‘A-Z‘|tr -d ": 、/ 0-9"

SSHDXPRIVILEGE-SEPARATEDSSHVAREMPTYSSHDSBINNOLOGIN

TCPDUMPXSBINNOLOGIN

WANQIXWANQIHOMEWANQIBINBASH

tail -n 3 /etc/passwd|tr ‘a-z‘ ‘A-Z‘|tr -d ": 、/ [:digit:]"

SSHDXPRIVILEGE-SEPARATEDSSHVAREMPTYSSHDSBINNOLOGIN

TCPDUMPXSBINNOLOGIN

WANQIXWANQIHOMEWANQIBINBASH


5、取出/etc/fstab的第6行;

head -n 6 /etc/fstab|tail -n 1

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘


6、取出/etc目录下所有以s开头的文件或目录,将其中最前面的8个文件保存到/tmp/start_with_s.out并同时进行标准输出;

ll -dh /etc/[s]*|head -n 8| ll -dh /etc/[s]*|head -n 8|tee /tmp/start_with_s.out 

drwxr-xr-x. 2 root root 4.0K 3月   6 17:22 /etc/samba

drwxr-xr-x. 3 root root 4.0K 3月   6 17:23 /etc/sane.d

drwxr-xr-x. 2 root root 4.0K 3月   6 17:23 /etc/sasl2

drwxr-xr-x. 3 root root 4.0K 3月   6 17:28 /etc/scl

-rw-------. 1 root root  122 1月  12 2010 /etc/securetty

drwxr-xr-x. 6 root root 4.0K 3月   6 17:22 /etc/security

drwxr-xr-x. 3 root root 4.0K 3月   6 17:24 /etc/selinux

-rw-r--r--. 1 root root 626K 10月  2 2013 /etc/services


三、其他练习:

1、将目录/etc备份至/backup目录中,并重命名为“etc-当前日期_当前时间”,如etc-2013-02-26_20:46:30;要求保留文件原来的属性,保持链接文件;


mkdir -pv /backup

mkdir: 已创建目录 "/backup"

cp -a /etc /backup/etc-`date +%F-%T`

ll -h /backup

总用量 12K

drwxr-xr-x. 116 root root 12K 3月  23 00:23 etc-2017-03-23-01:27:38


2、使用touch命令基于花括号展开的方式创建如下文件:ace  acf  acg  ade  adf  adg  bce  bcf  bcg  bde  bdf  bdg;


touch /mnt/lianxi/{ace,acf,acg,ade,adf,adg,bce,bcf,bcg,bde,bdf,bdg}.txt

ll -h /mnt/lianxi

总用量 0

-rw-r--r--. 1 root root 0 3月  23 01:43 ace.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 acf.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 acg.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 ade.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 adf.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 adg.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bce.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bcf.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bcg.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bde.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bdf.txt

-rw-r--r--. 1 root root 0 3月  23 01:43 bdg.txt





*4 通配符及管道与反引号“`”与花括号“{}”的使用

标签:通配符及管道与反引号“`”与花括号“{}”的使用

原文地址:http://12486569.blog.51cto.com/12476569/1909314

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!