1.1.文件内容操作
1.使用cat命令查看/etc/resolv.conf
[root@localhost~]# cat /etc/resolv.conf
;generated by /sbin/dhclient-script
searchlocaldomain
nameserver192.168.211.2
2.使用more命令查看/etc/mail/sendmail.cf
[root@localhost~]# more /etc/mail/sendmail.cf
#
#Copyright (c) 1998-2004 Sendmail, Inc. and its suppliers.
# All rights reserved.
...
#
##### $Id: cfhead.m4,v 8.116 2004/01/28 22:02:22 caExp $ #####
--More--(1%)
3.使用less命令查看/etc/mail/sendmail.cf
[root@localhost~]# less /etc/mail/sendmail.cf
#
#Copyright (c) 1998-2004 Sendmail, Inc. and its suppliers.
# All rights reserved.
...
##### $Id: cfhead.m4,v 8.116 2004/01/28 22:02:22 caExp $ #####
/etc/mail/sendmail.cf
4.对比cat more less区别和特点
cat——查看文件内容比较少
more——可以分页显示
less——比more更全面
5.查看/etc/passwd前5行
[root@localhost/]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6.查看/etc/passwd尾5行
[root@localhost/]# tail -n 5 /etc/passwd
avahi-autoipd:x:100:101:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayonuser:/home/sabayon:/sbin/nologin
tom:x:500:500::/home/tom:/bin/bash
jack:x:501:501::/home/jack:/bin/bash
7.查看/etc/passwd的第8-12行
[root@localhost/]# head -n 12 /etc/passwd | tail -n 5
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
8.统计系统中有多少个账户
[root@localhost/]# wc -l /etc/passwd
35/etc/passwd
9.计算/etc目录下.conf配置文件的个数
[root@localhost/]# find /etc/ -name *.conf | wc -l
241
10.显示/etc/hosts中127.0.0.1的内容
[root@localhost/]# grep 127.0.0.1 /etc/hosts
127.0.0.1 localhost.localdomain localhost
11.显示/etc/passwd中以root开头的内容
[root@localhost/]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
12.显示/etc/passwd中以bash结尾的内容
[root@localhost/]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
13.去除/etc/hosts.allow中的空行,把结果显示出来
[root@localhost~]# grep -v ^$ /etc/hosts.allow
#
#hosts.allow This file describes thenames of the hosts which are
# allowed to use the local INETservices, as decided
# by the '/usr/sbin/tcpd' server.
#
14.查找Linux识别的eth接口的信息
[root@localhost~]# dmesg | grep eth
e1000:eth0: e1000_probe: Intel(R) PRO/1000 Network Connection
e1000:eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
8021q:adding VLAN 0 to HW filter on device eth0
eth0:no IPv6 routers present
15.显示/etc/hosts里面不以#号开头的内容
[root@localhost~]# grep -v ^# /etc/hosts
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
16.计算以/bin/bash作登陆shell的用户个数
[root@localhost~]# grep /bin/bash$ /etc/passwd | wc -l
1
17.查找/etc/hosts中包含127.0.0.1或者localhost的内容 (这个管道两旁没有空格)
[root@localhost~]# grep -E "127.0.0.1|localhost" /etc/hosts
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
1.2.压缩和归档
1.以易读的属性并长格式显示/root下的内容将结果重定向到/root/gztest.txt里面
[root@localhost~]# ls -lh /root/ > /root/gztest.txt
[root@localhost~]# cat /root/gztest.txt
总计 80K
-rw-------1 root root 1015 07-10 17:29 anaconda-ks.cfg
drwxr-xr-x2 root root 4.0K 07-23 00:38 Desktop
-rw-r--r--1 root root 0 10-21 13:42 gztest.txt
-rw-r--r--1 root root 33K 07-10 17:29 install.log
-rw-r--r--1 root root 3.8K 07-10 17:27 install.log.syslog
2.分别使用gzip和bzip2和zip对/root/gztest.txt进行压缩和解压
//用gzip压缩
[root@localhost~]# gzip /root/gztest.txt
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt.gz install.log.syslog
Desktop install.log
//用gzip解压缩
[root@localhost~]# gzip -d /root/gztest.txt.gz
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt install.log.syslog
Desktop install.log
//用bzip2压缩
[root@localhost~]# bzip2 /root/gztest.txt
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt.bz2 install.log.syslog
Desktop install.log
//用bzip2解压缩
[root@localhost~]# bzip2 -d /root/gztest.txt.bz2
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt install.log.syslog
Desktop install.log
//用zip压缩
[root@localhost~]# zip /root/gztest.txt.zip gztest.txt
adding: gztest.txt (deflated 50%)
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt install.log
Desktop gztest.txt.zip install.log.syslog
//用unzip解压缩
[root@localhost~]# unzip /root/gztest.txt.zip
Archive: /root/gztest.txt.zip
replacegztest.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: gztest.txt
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt install.log
Desktop gztest.txt.zip install.log.syslog
3.把/etc/mail打包并压缩到/root/mail.tar.gz
[root@localhost~]# tar -zcf /root/mail.tar.gz /etc/mail
tar:从成员名中删除开头的“/”
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt install.log mail.tar.gz
Desktop gztest.txt.zip install.log.syslog
4.把/etc/mail打包并压缩到/root/mail.tar.bz2
[root@localhost~]# tar -jcf /root/mail.tar.bz2 /etc/mail
tar:从成员名中删除开头的“/”
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt.zip mail.tar.bz2
Desktop install.log mail.tar.gz
gztest.txt install.log.syslog
5.将mail.tar.gz解压到/tmp下,递归查看/tmp/etc下的内容,然后删除/tmp/etc目录
//解压缩
[root@localhost~]# tar -zxf mail.tar.gz -C /tmp/
//递归查看
[root@localhost~]# ls -R /tmp/etc/
/tmp/etc/:
/tmp/etc/mail:
access helpfile Makefile submit.mc
access.db local-host-names sendmail.cf trusted-users
domaintable mailertable sendmail.mc virtusertable
domaintable.db mailertable.db submit.cf virtusertable.db
//删除目录
[root@localhost~]# rm -rf /tmp/etc
//验证
[root@localhost~]# ls /tmp/etc
ls:/tmp/etc: 没有那个文件或目录
6.将mail.tar.bz2解压到/tmp下,递归查看/tmp/etc下的内容,然后删除/tmp/etc目录
//解压缩
[root@localhost~]# tar -jxf mail.tar.bz2 -C /tmp/
//递归查看
[root@localhost~]# ls -R /tmp/etc
/tmp/etc:
/tmp/etc/mail:
access helpfile Makefile submit.mc
access.db local-host-names sendmail.cf trusted-users
domaintable mailertable sendmail.mc virtusertable
domaintable.db mailertable.db submit.cf virtusertable.db
//删除目录
[root@localhost~]# rm -rf /tmp/etc/
//验证
[root@localhost~]# ls /tmp/etc/
ls:/tmp/etc/: 没有那个文件或目录
7.将/root目录打包保留文件及目录绝对路径和权限不变并压缩到/opt/root-home.tgz,打完包后删除原文件
//打包压缩
[root@localhost~]# tar -zcpPf /opt/root-home.tgz /root/ --remove
//验证打包成功
[root@localhost~]# ls /opt/root-home.tgz
/opt/root-home.tgz
//验证源文件被删除(只剩目录,无文件)
[root@localhost~]# ls -F
Desktop/
8.分屏查看/opt/root-home.tgz里面的内容
[root@localhost~]# tar -ztf /opt/root-home.tgz | more
/root/
/root/gztest.txt.zip
/root/.bashrc
/root/.metacity/sessions/
/root/.metacity/sessions/1381415844-3491-2407919490.ms
/root/.metacity/sessions/1381330808-3497-1570597326.ms
/root/.metacity/sessions/1381416669-6135-838692241.ms
/root/.metacity/sessions/1381253506-3653-2029340814.ms
/root/.scim/
/root/.scim/sys-tables/
/root/.scim/config
/root/.scim/pinyin/
/root/.scim/pinyin/pinyin_phrase_index
/root/.scim/pinyin/pinyin_table
/root/.scim/pinyin/phrase_lib
--More--
9.将/opt/root-home.tgz解压到目标主机
//确认未还原状态
[root@localhost~]# ls -F
Desktop/
//解压到源文件夹下
[root@localhost~]# tar -zxpPf /opt/root-home.tgz -C /
//确认解压成功
[root@localhost~]# ls
anaconda-ks.cfg gztest.txt.zip mail.tar.bz2
Desktop install.log mail.tar.gz
gztest.txt install.log.syslog
1.3.使用vim
1.请在 /tmp 这个目录下建立一个名为 vimtest 的目录
[root@localhost~]# mkdir /tmp/vimtest
[root@localhost~]# ls /tmp/
vimtest
2.进入vimtest 这个目录当中
[root@localhost~]# cd /tmp/vimtest/
[root@localhostvimtest]#
3.将 /etc/man.config 复制到本目录底下
[root@localhostvimtest]# cp -a /etc/man.config /tmp/vimtest/
[root@localhostvimtest]# ls
man.config
4.使用 vim 打开本目录下的man.config
[root@localhostvimtest]# vim /tmp/vimtest/man.config
5.在 vim 中设定一下行号
1 #
2 # Generated automatically from man.conf.inby the
3 # configure script.
4 #
5 # man.conf from man-1.6d
:set nu
6.移动到第 58 行,向右移动 40 个字元,请问你看到的双引号内是什么目录?
按:58,再向右移动到40字符
/dir/bin/foo
7.移动到第一行,并且向下搜寻一下‘ bzip2 ’这个字串,请问他在第几行?
按1G跳到第一行,再在末行模式键入下面命令
/bzip2
结果为
118COMPRESS /usr/bin/bzip2
8.我要将 50 到 100 行之间的‘小写 man 字串’改为‘大写 MAN 字串’,并且一个一个挑选是否需要修改,如何下达指令?如果在挑选过程中一直按‘y’,结果会在最后一行出现改变了几个 man 呢?
:50,100 s/man/MAN/cg
替换为 MAN(y/n/a/q/l/^E/^Y)?
25 次替换,共23 行
最后一行是96行,共替换了一次
96NROFF /usr/bin/nroff -c--legacy NROFF_OLD_CHARSET -MANdoc 2>/dev/
9.修改完之后,突然反悔了,要全部复原,有哪些方法?
第一个:在命令行模式下输入u。
第二个:在末行模式下:q!不保存强制退出。
第三个:在末行模式下在换回去:50,100 s/MAN/man/g
10.我要复制 65 到 73 这九行的内容(含有MANPATH_MAP),并且贴到最后一行之后
:65,73y
在用G跳转到最后一行,点p粘贴
11.21 到 42 行之间的开头为 # 符号的注解资料我不要了,要如何删除?
按:21跳到21行,按v后,用方向键选中21~33行,删除,第二次选中22到29行,删除,只保留34行
34FHS
12.将这个档案另存成一个 man.test.config 的档名
:w /tmp/vimtest/man.test.config
13.去到第 27 行,并且删除 15 个字元,结果出现的第一个单字是什么?
:27
再按15下x键。
27ons of the FHSrecommend putting formatted versions of
14.在第一行新增一行,该行内容输入‘I am astudent...’
:1
按o插入空行。再键入文字,结果如下。
2 I am a student...
15.储存后离开吧
:wq