文件管理练习题
显示/var 目录下所有以l 开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录
[root@localhost ~]# ls /var/ account crash games lf34asdf lock mail preserve target adm db gopher lib log nis run tmp cache empty kerberos local lsdf2dsfas opt spool yp [root@localhost ~]# ls /var/l*[0-9]*[[:lower:]] /var/lf34asdf: /var/lsdf2dsfas:
显示/etc 目录下以任意一位数字开头,且以非数字结尾的文件或目录
[root@localhost ~]# ls /etc/[0-9]*[^0-9] /etc/8yy
显示/etc/ 目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录
[root@localhost ~]# ls /etc/[^[:alpha:]]* /etc/3ll888 /etc/8yy
显示/etc 目录下所有以m 开头以非数字结尾的文件或目录
[root@localhost ~]# ls /etc/m*[^0-9] /etc/machine-id /etc/mail.rc /etc/mime.types /etc/mtab /etc/magic /etc/makedumpfile.conf.sample /etc/mke2fs.conf /etc/my.cnf /etc/mailcap /etc/man_db.conf /etc/motd /etc/modprobe.d: mlx4.conf /etc/modules-load.d: /etc/multipath: /etc/my.cnf.d: client.cnf mysql-clients.cnf server.cnf
显示/etc 目录下,所有以.d 结尾的文件或目录
[root@localhost ~]# ls -d /etc/*.d /etc/auto.master.d /etc/grub.d /etc/profile.d /etc/rwtab.d /etc/bash_completion.d /etc/init.d /etc/rc0.d /etc/setuptool.d /etc/binfmt.d /etc/ld.so.conf.d /etc/rc1.d /etc/statetab.d /etc/cgconfig.d /etc/logrotate.d /etc/rc2.d /etc/sudoers.d /etc/chkconfig.d /etc/modprobe.d /etc/rc3.d /etc/sysctl.d /etc/cron.d /etc/modules-load.d /etc/rc4.d /etc/tmpfiles.d /etc/depmod.d /etc/my.cnf.d /etc/rc5.d /etc/usb_modeswitch.d /etc/dnsmasq.d /etc/oddjobd.conf.d /etc/rc6.d /etc/xinetd.d /etc/dracut.conf.d /etc/pam.d /etc/rc.d /etc/yum.repos.d /etc/exports.d /etc/popt.d /etc/request-key.d /etc/gdbinit.d /etc/prelink.conf.d /etc/rsyslog.d
显示/etc 目录下,所有.conf 结尾,且以m,n,r,p 开头的文件或目录
[root@localhost ~]# ls /etc/[mnrp]*.conf /etc/man_db.conf /etc/nfsmount.conf /etc/request-key.conf /etc/rsyncd.conf /etc/mke2fs.conf /etc/nsswitch.conf /etc/resolv.conf /etc/rsyslog.conf
定义别名命令baketc,每天将/etc/目录下所有文件,备份到/testdir独立的子目录下,并要求子目录为格式为 backupYYYY-mm-dd,备份过程可见
[root@localhost testdir]# alias baketc='cp -av /etc/ /root/testdir/backup`date +%F`'
创建/testdir/rootdir 目录并复制/root 所有下文件到该目录内,要求保留原有权限。
[root@localhost ~]# cp --preserv=mode -r /root /testdir/rootdir[root@localhost ~]# ls2016-07-28 anaconda-ks.cfg f2 f4 f6 fb fc fd fe ff aaa f1 f3 f5 fa fB fC fD fE fF [root@localhost ~]# ll /testdir/rootdir/total 12drwxr-xr-x 2 root root 6 Jul 29 00:54 2016-07-28-rw-r--r-- 1 root root 266 Jul 29 00:54 aaa -rw------- 1 root root 2633 Jul 29 00:54 anaconda-ks.cfg -rw-r--r-- 1 root root 47 Jul 29 00:54 f1 -rw-r--r-- 1 root root 0 Jul 29 00:54 f2 -rw-r--r-- 1 root root 0 Jul 29 00:54 f3 -rw-r--r-- 1 root root 0 Jul 29 00:54 f4 -rw-r--r-- 1 root root 0 Jul 29 00:54 f5 -rw-r--r-- 1 root root 0 Jul 29 00:54 f6 -rw-r--r-- 1 root root 0 Jul 29 00:54 fa -rw-r--r-- 1 root root 0 Jul 29 00:54 fb -rw-r--r-- 1 root root 0 Jul 29 00:54 fB -rw-r--r-- 1 root root 0 Jul 29 00:54 fc -rw-r--r-- 1 root root 0 Jul 29 00:54 fC -rw-r--r-- 1 root root 0 Jul 29 00:54 fd -rw-r--r-- 1 root root 0 Jul 29 00:54 fD -rw-r--r-- 1 root root 0 Jul 29 00:54 fe -rw-r--r-- 1 root root 0 Jul 29 00:54 fE -rw-r--r-- 1 root root 0 Jul 29 00:54 ff -rw-r--r-- 1 root root 0 Jul 29 00:54 fF
如何创建/testdir/dir1/x, /testdir/dir1/y,/testdir/dir1/x/a,/testdir/dir1/x/b,/testdir/dir1/y/a, /testdir/dir1/y/b
[root@localhost ~]# mkdir -p /testdir/dir1/{x,y}/{a,b}[root@localhost ~]# tree /testdir/testdir └── dir1 ├── x │ ├── a │ └── b └── y ├── a └── b
如何创建/testdir/dir2/x, /testdir/dir2/y,/testdir/dir2/x/a, /testdir/dir2/x/b
[root@localhost ~]# mkdir -p /testdir/dir2/{x/{a,b},y}[root@localhost ~]# tree /testdir/testdir ├── dir1 │ ├── x │ │ ├── a │ │ └── b │ └── y │ ├── a │ └── b └── dir2 ├── x │ ├── a │ └── b └── y
如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5,/testdir/dir5/dir6, /testdir/dir5/dir7
[root@localhost ~]# mkdir -p /testdir/dir{3,4,5/dir{6,7}}[root@localhost ~]# tree /testdir/testdir ├── dir1 │ ├── x │ │ ├── a │ │ └── b │ └── y │ ├── a │ └── b ├── dir2 │ ├── x │ │ ├── a │ │ └── b │ └── y ├── dir3 ├── dir4 └── dir5 ├── dir6 └── dir7
将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@localhost ~]# cat /etc/issue |tr 'a-z' 'A-Z' > /tmp/issue.out[root@localhost ~]# cat /tmp/issue.out CENTOS RELEASE 6.8 (FINAL) KERNEL \R ON AN \M\L\T\NMAGE EDUCATION LEARNING SERVICESHTTP://WWW.MAGEDU.COM
将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@localhost ~]# who |tr 'a-z' 'A-Z' > /tmp/who.out[root@localhost ~]# cat /tmp/who.out ROOT TTY1 2016-07-31 21:31 ROOT PTS/0 2016-07-31 21:34 (192.168.1.110)[root@localhost ~]# who root tty1 2016-07-31 21:31 root pts/0 2016-07-31 21:34 (192.168.1.110)一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks!操作系统版本信息
[deng@localhost ~]$ cat /etc/redhat-release > f1 [deng@localhost ~]$ echo "Hello,I am `whoami`,the system version is here,please help me to check it,thanks! " >> f1 [deng@localhost ~]$ mail -s help root < f1 [root@localhost mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 1 message 1 new >N 1 deng@localhost.local Sun Jul 31 23:05 19/696 "help" & 1 Message 1: From deng@localhost.localdomain Sun Jul 31 23:05:29 2016 Return-Path: <deng@localhost.localdomain> X-Original-To: root Delivered-To: root@localhost.localdomain Date: Sun, 31 Jul 2016 23:05:29 +0800 To: root@localhost.localdomain Subject: help User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: deng@localhost.localdomain Status: R CentOS release 6.8 (Final) Hello,I am deng,the system version is here,please help me to check it,thanks!
将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@localhost ~]# ls /root/ |tr '\n' '\ 'anaconda-ks.cfg dead.letter Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos [root@localhost ~]#
file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和 file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
[root@localhost ~]# echo "1 2 3 4 5 6 7 8 9 10" > file1[root@localhost ~]# cat file1 |tr '\ ' '+' |bc55
删除Windows文本文件中的'^M'字符
[kuikui@localhost ~]$ cat az.txt -Aaaaaa^M$ccccc^M$ggggg^M$rrrrr[kuikui@localhost ~]$ cat az.txt |tr -d '\r'>f1.txt [kuikui@localhost ~]$ cat f1.txt -Aaaaaa$ccccc$ggggg$
处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
root@localhecho "xt.,l 1 jr#'cd'mn2 c*/fe3 uz4"|tr -cd '[:digit:][ ]' 1 2 3 4
将PATH变量每个目录显示在独立的一行
[root@localhost ~]# echo $PATH | tr ':' '\n'/usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /root/bin
删除指定文件的空行
[root@localhost ~]# cat ff1aaaaa sdfsdfsdf dfd dfdf dfdfdf ssssss [root@localhost ~]# cat ff1 | tr -s '\n'aaaaa sdfsdfsdf dfd dfdf dfdfdf ssssss
将文件中每个单词(字母)显示在独立的一行,并无空行
[root@localhost ~]# cat test# This is the default, if you make this bigger yum won't see if the metadata# is newer on the remote and so you'll "gain" the bandwidth of not having to# download the new metadata and "pay" for it by yum not having correct# information.[root@localhost ~]# cat test | tr -cs '[:alpha:]' '\n'Thisisthedefaultifyou makethisbigger yum won t seeifthe metadataisneweronthe remoteandso you ll gain the bandwidthofnothavingtodownload thenewmetadataandpayforitbyyumnothaving correct information
创建 用户gentoo ,附加组为bin 和root ,默认shell为 为/bin/csh ,注释信息为"Gentoo Distribution"
[root@localhost ~]# useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo[root@localhost ~]# getent passwd gentoogentoo:x:2006:2006:Gentoo Distribution:/home/gentoo:/bin/csh[root@localhost ~]# groups gentoogentoo : gentoo root bin
创建 下面的用户、组和组成员关系名字为 为admins的组
要求: 用户natasha ,使用admins 作为附属组 用户harry ,也使用admins 作为附属组 用户sarah ,不可交互登录系统, 且 不是admins的成员,natasha ,harry ,sarah 密码 都是centos [root@localhost ~]# groupadd admins[root@localhost ~]# useradd -G admins natashauseradd: user 'natasha' already exists[root@localhost ~]# userdel -r natasha [root@localhost ~]# userdel -r harry[root@localhost ~]# groupdel admins[root@localhost ~]# [root@localhost ~]# groupadd admins[root@localhost ~]# useradd -G admins natasha[root@localhost ~]# useradd -G admins harry[root@localhost ~]# useradd -s /sbin/nologin sarah [root@localhost ~]# vi mima.txt[root@localhost ~]# cat mima.txt |chpasswd[root@localhost ~]# groups natashanatasha : natasha admins [root@localhost ~]# groups harryharry : harry admins [root@localhost ~]# [root@localhost ~]# groups sarahsarah : sarah [root@localhost ~]# cat /etc/shadownatasha:$6$Gspfe/MrFJrFP$AytzknbBqJvt3LnT3f/.H7Oaw6pJBJEdXt4.XB0tHXyM6WvbRLuKHr8WyFgL7VQkFOPFoHX6GSjslTwrFJmUO1:17014:0:99999:7::: harry:$6$ZBj6W/iXuQ5/7Ay5$TrwcV2q019MYtFDW3TBC3nzaK1pguUKrrmeug9YzVQO6Un1fH1aiIVCHX.B2eZ5rswdkPDMJC1.h30yWxUQPh.:17014:0:99999:7::: sarah:$6$ljI3P/KT1KkLaR2t$Q/WhZ17E3t4c5PdNzzuSDql0sWWCTJ3Sylg8utPzGpj7pRdn4HdcHanZ4BFbd2CiUdeWMJTBxuIyf2b6dz4T6.:17014:0:99999:7:::
创建testuser uid:1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser
[root@localhost ~]# useradd -u 1234 -g bin -G root,ftp -s /bin/csh -d /testdir/testuser testuser[root@localhost ~]# getent passwd testusertestuser:x:1234:1::/testdir/testuser:/bin/csh[root@localhost ~]# groups testusertestuser : bin root ftp [root@localhost ~]# id testuseruid=1234(testuser) gid=1(bin) groups=1(bin),0(root),50(ftp)
修改testuser uid:4321,主组:root,辅助组:nobody,loginname:test,home:/home/test 家数据迁移
[root@localhost ~]# usermod -u 4321 -g root -G nobody -l test -d /home/test -m testuser[root@localhost ~]# getent passwd testtest:x:4321:0::/home/test:/bin/csh[root@localhost ~]# id testuid=4321(test) gid=0(root) groups=0(root),99(nobody) [root@localhost ~]# cd /home/test[root@localhost test]# ls -a . .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla
批量创建帐号:user1...user10uid:3000-3009,shell:/bin/csh,home:/testdir/usernamepasswd:usernamepass
注意家目录相关配置,使用户正常登录 [root@localhost ~]# vi p.txt [root@localhost ~]# newusers p.txt [root@localhost ~]# vi p1.txt [root@localhost ~]# cat p1.txt |chpasswd [root@localhost ~]# getent passwd user1:x:3000:3000::/testdir/user1:/bin/csh user2:x:3001:3001::/testdir/user2:/bin/csh user3:x:3002:3002::/testdir/user3:/bin/csh user4:x:3003:3003::/testdir/user4:/bin/csh user5:x:3004:3004::/testdir/user5:/bin/csh user6:x:3005:3005::/testdir/user6:/bin/csh user7:x:3006:3006::/testdir/user7:/bin/csh user8:x:3007:3007::/testdir/user8:/bin/csh user9:x:3008:3008::/testdir/user9:/bin/csh user10:x:3009:3009::/testdir/user10:/bin/csh [root@localhost ~]# cat /etc/shadow user1:$6$gefBI/NHwe$N9yKHNogc6IbUenccFld5R6c6r5JTBVKYJ.204kySh6tbb1dlGpx8A.f4xPuwojH6EG3Muj8cBDGnIeRjAZs6/:17014:0:99999:7::: user2:$6$BL.I1/Td7oN/$bQf8FSanKWqRcwIMFrtXj/ruhggLzDbHuag5twe1El1QruHcq98fNjw2KqBk9cibnI0yCtoV//l7v6QiH7EQP/:17014:0:99999:7::: user3:$6$Qc1cLiJFAy/repp$2.gQb70alXLWd1LQgf.Z.wu293WAtndMMuWpwkA3HUUmgMxTClql8Q17.IBF4BbrfptQUCtKpfJ8z03b7cRDP/:17014:0:99999:7::: user4:$6$E3J6QVa//m/O3b$dqVdFzsPSjZzW.kyJI74ZaQlx4V/ji9bGJd10GiXxdfPCgmAxMghlIyBQtcKU/HLtSXgUAoilxNz9SlLjn5cA1:17014:0:99999:7::: user5:$6$gnOh5/av.0i/SFF$qWy55EdNGT/DRhzodPpquJYvXxK0CF15Aarnit6SkHTqBsMPaUBfM8J6TZvpVeZ/UjFpVRTV50NNNOvXR.Qoc/:17014:0:99999:7::: user6:$6$Y6Pnlo3Y$LbjYgtMo.Dsw29r5NGrK8LEDSh4E.7x1hr3dXVgr1uyLhGgzcyx2/q0VzAHrm0p9iXEbrQ7NDVRbSaDcKiw5N1:17014:0:99999:7::: user7:$6$EK1pup.sYJjk/$hbbGc0ZEFmC18dHLDzKsbCko3R5Q.0Er6izfJ1BkK8VK7judb7rdT5aDYmv4sD..7wL.9KJtevJYU5lhbKOa.0:17014:0:99999:7::: user8:$6$kxvO1.bxnjTQ.R7/$JF2WpNBZOx/eBGUaSk3BMtPvq3b5eFSFZmnU2Duyeaz9LhhGcvnqJMyTexy3JTCBl/Gu1F6QeAbAzIAd4XjUj0:17014:0:99999:7::: user9:$6$d1uqi/hjQL$DaFuVn2V3DS3AFaUQGYMXjPgyWI2yrc5x7nCZM/ps8jU3WRtFxOyMO2Hrp9bfCv63O1/xdwShGdq9F51hpQGB/:17014:0:99999:7::: user10:$6$0LqVp/jQsUdHMhRx$ytzpPU4AH4/qLGbJmIZB7qnEH8VLy/0.MnvmgxLWBSmXAcYxhA6uDVb9tHdczk9zvI27gKSCX90owbWnB8XJA.:17014:0:99999:7::: [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user1 [root@localhost user1]# ls -a . .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user2 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user3 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user4 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user5 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user6 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user7 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user8 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user9 [root@localhost user1]# cp -r /etc/skel/.[^.]* /testdir/user10 [root@localhost ~]# su - user1 [user1@localhost ~]$ su - user2 Password: [user2@localhost ~]$