1如何过滤出已知当前目录oldboy中的所有一级目录 {不包含oldboy目录下目录的子目录及隐藏目录只能是一级目录}

解答:

分析过程:要先完成此题 我们首先要想如何区分目录的方式

思路思想方法:

1.1 根据颜色分区文件和目录(浅蓝色的是目录 默认是深蓝色

1.2 ls -l 输出结构中以d directory)开头的就是目录


[root@oldboy lvxx]# ls -l

drwxr-xr-x 3 root   root   4096 4月  24 02:27 ect

drwxr-xr-x 2 root   root   4096 4月  24 02:27 test

-rw-r--r-- 1 root   root     12 4月  23 22:35 xue

 [root@oldboy lvxx]# ls -l|grep "^d"

drwxr-xr-x 3 root   root   4096 4月  24 02:27 ect

drwxr-xr-x 2 root   root   4096 4月  24 02:27 test


1.3 通过给目录加标识然后通过过滤标识 就是过滤目录(ls-Fls-p
 

[root@oldboy lvxx]# ls -F

ect/   xiaofang/ xingfujie/  xue

然后过滤出以 / 为结尾的

[root@oldboy lvxx]# ls -F|grep "/$"

ect/

test/


1.4 通过find直接查找指定类型的文件(-d就是目录)

-maxdepth 对于find可以指定目录层次的深度


[root@oldboy lvxx]# find -maxdepth 1 -type d ! -name "." 

./test

./xiaohong


find 参数-maxdepth 查找深度 接数字 就是第几层

[root@oldgir oldboy]# find /oldboy/ -maxdepth 1 -type d

/oldboy/

/oldboy/xiaofan

/oldboy/ext

/oldboy/xiaodong


1.5 ls -l输出结果中第二列数字大于1的就是目录(文件没有硬链接的情况下)

[root@oldboy lvxx]# ls -l|awk '{if($2>1) print $0}'

drwxr-xr-x 3 root   root   4096 4月  24 02:27 ect

drwxr-xr-x 2 root   root   4096 4月  24 02:27 test

 

使用正则表达式

[root@oldboy~]#  ls -l|awk '/^d/'

drwxr-xr-x. 8oldboy oldboy     4096 4月  24 02:27 lvxx

drwxr-xr-x  3 root  root       4096 4月  23 12:42 oldboy


[root@oldboy ~]# ls -l|sed -n '/^d/p'

drwxr-xr-x. 8oldboy oldboy     4096 4月  24 02:27 lvxx

drwxr-xr-x  3 root   root      4096 4月 23 12:42 oldboy

 

[root@oldgir oldboy]# ll | awk '$2>1'

total 20

drwxr-xr-x. 3 root root 4096 May 20 00:53 ext

drwxr-xr-x. 2 root root 4096 May 20 00:53 tes


 假如现在目录是:

[root@oldboy xue]# pwd

/xue

现在因为需要进入/tmp 目录下进行操作 执行命令如下:

[root@oldboy xue]# cd /tmp/

[root@oldboy tmp]# pwd

/tmp

操作完成后 希望快速回答上一次进入的目录即/xue 目录该如何做(不能用 cd /tmp

解答

cd - 表示回到上一次的位置

[root@oldboy tmp]# cd -

/xue

 

1.2 一个目录中很多文件(ls)查看时好多屏想最快的速度查看新文件如何看?

解答:

ls-lrt 执行这个命令最新的文件会在最下面

显示时间 r是倒序

#以长格式按照时间修改倒序排列文件和目录这样最近创建或修改的文件目录就在窗口的最下面


[root@oldboy ~]# ls -lrt /etc

rw-r--r--. 2 root root     76 5月   1 15:05 resolv.conf


1.3
 已知apache 服务的访问日志 按天记录在服务器本地目录/app/logs 由于磁盘空间紧张

现在要求只能保留最近7天的访问日志 请问如何解决?请给出解决办法或配置或处理命令

(提示:可以从apache配置上着手 也可以从生成的日志着手)

 

模拟环境:

先创建文件 for n in `seq 10` ;doecho $n 后接文件 sleep 1done 每秒一次的插入

[root@oldboy app]# for n in 'seq 1000';do echo $n>>/var/log/messages;sleep 1;done


将时间改成后7

[root@oldgir logs]# date -s 20150516

Tue May 16 00:00:44 CST 2017

[root@oldgir logs]# ll

total 0

-rw-r--r--. 1 root root 0 May  1 00:00 access_www_2017-05-01.log

-rw-r--r--. 1 root root 0 May  2 00:00 access_www_2017-05-02.log

-rw-r--r--. 1 root root 0 May  3 00:00 access_www_2017-05-03.log 剩下省略




 方法(1find查找所有以.log为结尾的 -mtime +77天以前 -7 7天以后 7就代表第7


[root@oldboy logs]# find /app/logs -type f mtime +7 -name '*.log'|xarge rm -f


方法(2使用find命令和参数-exec 进行删除


[root@oldboy logs]# find /app/logs -type f mtime +7 -name '*.log' -exce rm -f {} \;


方法(3 $( )=``反引号 

[root@oldboy logs]# rm -f $(find -type f -name"*.log" -mtime +7) 


 

1.4 调试系统服务时 希望时实查看系统日志 /var/log/messages的更新如何做

解答:

tail -f 时实监控文件尾部的变化

[root@oldboy ~]# tail -f /var/log/messages 


1.5在配置apache 执行了./configure --prefix=/application/apache2.2.17 来编译apche,在make install 完成后,希望用户访问 apache 路径更简单,需要给/application/apache2.2.17 目录做一 个软链接/application/apache,使得内部开发或管理人员通过/application/apache 就可以访问到 apache 的安装目录/application/apache2.2.17 下的内容,请你给出实现的命令。(提示:apache 为一个 web 服务)

 解答:

ln -s 创建软连接接源文件  目标文件

ln-s /application/apache2.2.17 /application/apache


1.6打印配置文件nginx.conf内容的行号和内容 该怎么做?

模拟环境:

[root@oldgir oldboy]# echo stu{1..10} |xargs -n1>nginx.conf

[root@oldgir oldboy]# cat nginx.conf

stu1

stu2

......

stu10



方法(1cat -n 参数显示行号

[root@oldboy xue]# cat -n nginx.cof cat-n 显示行号

     1  1

     2  2

[root@oldgir oldboy]# cat -n nginx.conf

     1  stu1

     ......

    10 stu10



方法(2)使用vim 命令进入之后直接按:set nu 回车直接显示行号

[root@oldgiroldboy]# vim nginx.conf

 

  1 stu1

.......

 10 stu10   

 :set nu     


                                              

方法(3)nl专业显示行号

[root@oldboy xue]# nl nginx.cof

     1  1

     2  2



方法(4grep -n 打印行号 . 点代表人员一个字符

[root@oldgir oldboy]# grep "6"nginx.conf

stu6

[root@oldgir oldboy]# grep "."nginx.conf

stu1

。。。

stu110


[root@oldgir oldboy]# grep -n "." nginx.conf

1:stu1

。。。

10:stu10



方法(5 上下显示行号

[root@oldgir oldboy]# sed '=' nginx.conf sed"="

1

stu1

。。。

10

stu10


[root@oldgir oldboy]# sed '=' nginx.conf |xargs-n2 对行号进行分组

1 stu1

。。。

10 stu10


方法(6)awk NR 行号 $0 整行

[root@oldgir oldboy]# awk '{print NR,$0}'nginx.conf

1 stu1

。。。

10 stu10


1.6 linux 系统运行级别一般为 0-6,请分别写出每个级别的含义


运行级别在  /etc/inittab里面

[root@oldgir oldboy]# cat /etc/inittab

 

0 关机

1 单用户模式

2 没有NFS的多用户模式

3 命令行的多用户模式

4 未使用的

5 桌面模式

6 从启


查看现在正在运行的级别

[root@oldgir oldboy]# runlevel

N 3

临时切换运行级别 init 5

关闭 还可以使用 shutdown


1.7装完系统后希望iptables仅在3级别上开机自启动如何做?

解答:

什么是开机自启动?

开机自己自动起来(比如Windows下的360


方法(1)(以iptabls为例)

chkconfig 查看操作系统在每一个执行等级中执行哪些系统服务

[root@oldgir oldboy]# chkconfig |grep iptables

iptables         0:off  1:off  2:on   3:on   4:on   5:on   6:off

两种都可查询 --list

[root@oldgir oldboy]# chkconfig --list iptables

iptables         0:off  1:off  2:on   3:on   4:on   5:on   6:off

设置运行级别 --level

[root@oldgir oldboy]# chkconfig --level 3 iptables on

[root@oldgir oldboy]# chkconfig --list iptables

iptables         0:off  1:off  2:off  3:on   4:off  5:off  6:off




文件配置方法:可以把要启动的命令放在 /etc/rc.d里

[root@oldboy xuexue]# vi /etc/rc.d

/etc/init.d/sshd start      文件最好可以加注释#方便别人可以看懂 


方法(2

chkconfig 管理的服务是 /etc/init.d下面的服务

chkconfig--list 查看所有运行级别对应的服务 工作级别 3 直接过滤出来 on 表示自动启动

[root@oldboy xuexue]# chkconfig --list|grep"3:on"

[root@oldboy xuexue]# chkconfig --list iptables

sshd           0:off  1:off  2:on   3:on   4:on   5:on   6:off



1.8linux 系统中查看中文乱码,请问如何解决乱码问题?

排查: 

1)看当前系统的字符集看变量里的内容

[root@oldgir oldboy]# echo $LANG

en_US.UTF-8


2)查看远程连接软件 xshell

在标准按钮一栏中 点击编码 (像个小地球)然后选择en_US.UTF-8 

            

解决方法:

[root@oldgir oldboy]# cat /etc/sysconfig/i18n

LANG="en_US.UTF-8"

SYSFONT="latarcyrheb-sun16"

vi编辑 或 sed 替换

[root@oldgir oldboy]# source /etc/sysconfig/i18n

最后使用source使


1.9如何优化 linux 系统(可以不说太具体)?

http://oldboy.blog.51cto.com/2561410/988726 具体可以看老师的博客

 


1.10/etc/目录为 linux 系统的默认的配置文件及服务启动命令的目录 

a.请用 tar 打包/etc整个目录(打包及压缩)

b.请用 tar 打包/etc整个目录(打包及压缩,但需要排除/etc/services 文件)

c.请把 a 点命令的压缩包,解压到/tmp指定目录下(最好只用 tar 命令实现)



a解答:

tar zvf 进行打包先接要打包的位置最后接想要打哪个包

[root@oldgir tmp]# tar zvf /tmp/etc.tar.gz /etc/

查看一下打包 tar tf 接包名

[root@oldgir tmp]# tar tf etc.tar4-pc.gz

 

解压包 tar xf 接包名

[root@oldgir tmp]# tar xf etc-pc.tar.gz /root

[root@oldgir tmp]# ll /root

drwxr-xr-x. 78 root root  4096 May 20 2017 etc


b 解答:

方法(1--exclude 排除不想要的内容

tar zcvf /tmp/etc-pc.tar.gz /etc --exclude=etc/services这里要根相对路径 相对于/tmp/etc-pc.tar.gz

                                                                        所以不接根

[root@oldgir tmp]# tar tf etc-pc.tar.gz |grep services 查找tar包里是否排除 

etc/init/readahead-disable-services.conf


[root@oldgir tmp]# touch 1.txt

[root@oldgir tmp]# echo init.d >1.txt

[root@oldgir tmp]# echo services >>1.txt  把要排除的包都装在一个文件里


[root@oldgir tmp]# tar zcf etc.tar4-pc.gz /etc --exclude-from=1.txt 然后一起排除文档中的所以文件

[root@oldgir tmp]#  tar tf etc.tar4-pc.gz |grep init.d

[root@oldgir tmp]#  tar tf etc.tar4-pc.gz |grep services

etc/init/readahead-disable-services.conf


c 解答:如果解压的时候想指定位置 tar -C 然后指定路径

[root@oldgir tmp]# tar xf etc.tar.gz -C /root/

[root@oldgir tmp]# ll /root

drwxr-xr-x. 78 root root  4096 May 20 2017 etc


1.11.已知如下命令及结果:

[oldboy@test~]$ echo "I am oldboy,myqq is 31333741">>oldboy.txt

[oldboy@test~]$ cat oldboy.txt 

Iam oldboy,myqq is 3133374

 

现在需要从文件中过滤出“oldboy”和“31333741”字符串,请给出命令.

方法(1 sed+sed 把不要的删掉用替换的方法

[root@oldgiroldboy]# sed 's#I am##g' oldboy.txt

 oldboy,myqq is 31333741

[root@oldgiroldboy]# sed 's#I am##g' oldboy.txt |sed 's#,myqq is##'

 oldboy 31333741

(2)

[root@oldgir oldboy]# sed 's#,# #g' oldboy.txt|awk '{print $3,$6}'

oldboy 31333741

(3)

[root@oldgir ~]# awk '{print $3,$5}'oldboy.txt|sed 's#,myqq##'

oldboy 31333741


 (4)

[root@oldgir oldboy]# awk -F '[ ,]' '{print$3,$6}' oldboy.txt

oldboy 31333741


5 cut

[root@oldgir oldboy]# cut -d " " -f3,5oldboy.txt |sed 's#,myqq##g'

oldboy 31333741

 

1.12.如何查看/etc/services 文件内容有多少行?

wc命令的功能为统计指定文件中的字节数、字数、行数, 并将统计结果显示输出。

- c 统计字节数。
- l  统计行数。
- w 统计字数。

方法(1)

[root@oldgir etc]# wc -l /etc/services

10774 /etc/services


方法(2) cat 参数 -n 显示行号

[root@oldgir oldboy]# cat -n /etc/services |tail-1

 10774 iqobject       48619/udp               # iqobjec


方法(3)

可以把进程和进程数过滤出来

[root@oldgir oldboy]# ps -ef | grep /sbin/sshd

root      1502      1  0 May15 ?        00:00:00 /usr/sbin/sshd

root      6787   6655  0 05:07 pts/0    00:00:00 grep --color=auto /sbin/sshd

[root@oldgir oldboy]# ps -ef | grep"/sbin/sshd"|wc -l

2


1.13.过滤出/etc/services 文件包含 3306 1521 两数据库端口的行的内容。

使用egrep过滤 相当于 grep -E

[root@oldgiroldboy]# egrep "3306|1521" /etc/services

mysql           3306/tcp                        # MySQL

mysql           3306/udp                        # MySQL

ncube-lm        1521/tcp                # nCube License Manager

ncube-lm        1521/udp                # nCube License Manager