Linux的shell编程前奏常见命令实战二

前言: 本节会重点涉及到以下命令但不限于以下命令:chkconfig,ls,cat,tail,ln,find,wc,tar,cut,grep,egrep

1>如何过滤出已知当前目录下usr中的所有一级目录?(提示:不包含usr目录下的子目录及隐藏目录

,即只能是一级目录)

法一:

[root@lll3 /]# cd /usr;ls -l|grep ^d
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8
drwxr-xr-x.   2 root root         6 11月  5 2016 etc
drwxr-xr-x.   2 root root         6 11月  5 2016 games
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include
drwxr-xr-x.   3 root root        58 10月  5 2017 java7
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec
drwxr-xr-x.  14 root root       153 7月  28 10:17 local
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share
drwxr-xr-x.   4 root root        34 8月  11 2017 src
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7

总结: linux中的七种文件类型:

d 目录文件。

l 符号链接(指向另一个文件,类似于瘟下的快捷方式)。

s 套接字文件。

b 块设备文件,二进制文件。

c 字符设备文件。

p 命名管道文件。

- 普通文件,或更准确地说,不属于以上几种类型的文件

[root@lll3 usr]# cd /etc/ssh/;grep -Ev "^#|^$" sshd_config     -----  -E使得grep相当于egrep;^#是以#开头的;^$是空格
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UsePrivilegeSeparation sandbox          # Default for new installations.
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem       sftp    /usr/libexec/openssh/sftp-server


法二:

[root@lll3 usr]# cd /usr;ls -pl|grep /$      -----ls 的pl参数是给不同文件类型添加结尾符号,$是以什么结尾
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin/
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8/
drwxr-xr-x.   2 root root         6 11月  5 2016 etc/
drwxr-xr-x.   2 root root         6 11月  5 2016 games/
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include/
drwxr-xr-x.   3 root root        58 10月  5 2017 java7/
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib/
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64/
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec/
drwxr-xr-x.  14 root root       153 7月  28 10:17 local/
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32/
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin/
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share/
drwxr-xr-x.   4 root root        34 8月  11 2017 src/
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7/

总结: grep常用七个参数总结

-v  显示不匹配的行

-E  使用扩展的egrep命令

-n  显示匹配行及行号

-i  不区分大小写(只适用于单字符),默认是区分大小写的

-o  只输出匹配的内容

-w 只匹配过滤的单词

法三:
[root@lll3 usr]# find . -maxdepth 1 -type d ! -name "."    ----- -maxdepth深入一层
./bin
./sbin
./lib
./lib64
./share
./etc
./games
./include
./libexec
./local
./src
./java7
./tomcat7
./cmake-2.8.8
./mysql-5.5.32
法四:
[root@lll3 usr]# tree -Ld 1
.
├── bin
├── cmake-2.8.8
├── etc
├── games
├── include
├── java7
├── lib
├── lib64
├── libexec
├── local
├── mysql-5.5.32
├── sbin
├── share
├── src
├── tmp -> ../var/tmp
└── tomcat7
16 directories

总结:tree命令常用参数

-L level 遍历最大的目录层数,levle为大于零的正整数

-d  只显示目录

-F  在执行文件,目录,socket, 符号连接,管道名称等不同类型文件的结尾,各自加上"*","/",

     "=","@","|"号,类似ls命令的-F选项

思想:当一个命令的输出含有我们需要的内容的时候,我们要想到这个命令可能有对应的参数直接显示

            我们需要的内容。

法五:

[root@lll3 usr]# ls -l|sed -n /^d/p
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8
drwxr-xr-x.   2 root root         6 11月  5 2016 etc
drwxr-xr-x.   2 root root         6 11月  5 2016 games
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include
drwxr-xr-x.   3 root root        58 10月  5 2017 java7
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec
drwxr-xr-x.  14 root root       153 7月  28 10:17 local
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share
drwxr-xr-x.   4 root root        34 8月  11 2017 src
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7
[root@lll3 usr]# ls -l|awk /^d/
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8
drwxr-xr-x.   2 root root         6 11月  5 2016 etc
drwxr-xr-x.   2 root root         6 11月  5 2016 games
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include
drwxr-xr-x.   3 root root        58 10月  5 2017 java7
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec
drwxr-xr-x.  14 root root       153 7月  28 10:17 local
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share
drwxr-xr-x.   4 root root        34 8月  11 2017 src
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7
法六:
[root@lll3 usr]# ls -lF|sed -n '/\/$/p'                            ----用sed把以/结尾的显示
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin/
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8/
drwxr-xr-x.   2 root root         6 11月  5 2016 etc/
drwxr-xr-x.   2 root root         6 11月  5 2016 games/
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include/
drwxr-xr-x.   3 root root        58 10月  5 2017 java7/
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib/
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64/
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec/
drwxr-xr-x.  14 root root       153 7月  28 10:17 local/
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32/
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin/
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share/
drwxr-xr-x.   4 root root        34 8月  11 2017 src/
lrwxrwxrwx.   1 root root        10 8月  11 2017 tmp -> ../var/tmp/
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7/
[root@lll3 usr]# ls -lF|awk '/\/$/'                ----用awk把以/结尾的显示
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin/
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8/
drwxr-xr-x.   2 root root         6 11月  5 2016 etc/
drwxr-xr-x.   2 root root         6 11月  5 2016 games/
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include/
drwxr-xr-x.   3 root root        58 10月  5 2017 java7/
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib/
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64/
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec/
drwxr-xr-x.  14 root root       153 7月  28 10:17 local/
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32/
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin/
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share/
drwxr-xr-x.   4 root root        34 8月  11 2017 src/
lrwxrwxrwx.   1 root root        10 8月  11 2017 tmp -> ../var/tmp/
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7/

总结:正则表达式中^是以什么什么开头,$是以什么什么结尾,

         .是代表任意单个字符,^$是空行,[^oldboy]中^是非,

         \让一个字符脱掉马甲,还原它本来的意义。

2>Linux中怎么最简易快速回到上一次的目录?

[root@lll3 usr]# env|grep -i OLDPWD
OLDPWD=/
[root@lll3 usr]# cd /home/
[root@lll3 home]# env|grep -i OLDPWD
OLDPWD=/usr
[root@lll3 home]# cd -
/usr

总结: cd .当前目录;cd ..上级目录; cd ../..上上级目录; cd -上一次目录


3>一个目录中有很多文件(ls查看时好多屏), 想最快速查看到最近更新的文件。如何看?

[root@lll3 usr]# ls -lrt
总用量 29844
drwxr-xr-x.   2 root root         6 11月  5 2016 games
drwxr-xr-x.   2 root root         6 11月  5 2016 etc
drwxr-xr-x.   4 root root        34 8月  11 2017 src
lrwxrwxrwx.   1 root root        10 8月  11 2017 tmp -> ../var/tmp
drwxr-xr-x.   3 root root        58 10月  5 2017 java7
drwxr-xr-x.   3 root root        69 2月  20 2018 tomcat7
-rw-r--r--    1 root root  24596474 7月  22 11:08 mysql-5.5.32.tar.gz
-rw-r--r--    1 root root   5691656 7月  22 11:08 cmake-2.8.8.tar.gz
dr-xr-xr-x.   2 root root     20480 7月  28 09:58 sbin
drwxr-xr-x.  40 root root     12288 7月  28 09:58 libexec
dr-xr-xr-x.  43 root root      4096 7月  28 09:59 lib
drwxr-xr-x.  14 root root       153 7月  28 10:17 local
drwxr-xr-x.  39 root root      8192 7月  28 10:19 include
dr-xr-xr-x. 141 root root     73728 7月  28 10:19 lib64
drwxr-xr-x   32 7161 wheel     4096 7月  28 11:08 mysql-5.5.32
dr-xr-xr-x.   2 root root     49152 7月  28 13:14 bin
drwxr-xr-x. 234 root root      8192 7月  28 13:14 share
drwxr-xr-x   13 root root      4096 10月 14 17:30 cmake-2.8.8

总结:-r依相反次序排序; -t根据最后修改时间排序

思想:先走通,再变通。


4>在配置apache时执行了./configure --prefix=/application/apache2.2.17来编译apache,在make install完成后

,希望用户的访问路径更加简单,需要给/application/apache2.2.17目录做一个软链接/apolication/apache,使得

内部开发或管理人员通过/apolication/apache就可以访问到apache的安装目录/application/apache2.2.17的内容,

请给出你实现的命令。

[root@lll3 application]# mkdir -p /application/apache2.2.17;touch /application/apache2.2.17/oldboy.txt
[root@lll3 application]# mkdir -p /application/apache
[root@lll3 application]# ln -s /application/apache2.2.17/ /application/apache
[root@lll3 application]# ls -l /application/apache

总用量 0

lrwxrwxrwx 1 root root 26 10月 28 12:07 apache2.2.17 -> /application/apache2.2.17/

总结:ln -s 源文件 目标文件  注意:目标文件不能事先存在


5>已知apache服务的访问日志按天记录在服务器本地目录/app/logs下,由于磁盘空间紧张,现在要求只能保持最近7天

访问日志?请问如何解决,给出解决命令.

[root@lll3 apache]find ./ -type f -name "*.log" -mtime +7|xargs rm -f


6>调试系统服务时,希望能实时查看系统日志/var/log/messages的更新,如何去做?

tail -f /var/log/messages

总结:

-f 实时输出文件变化后追加的数据,与tailf类似。


7>打印配置文件nginx.conf内容的行号及内容,该如何去做?

法一:

[root@lll conf]# cat -n nginx.conf|more
     1
     2  #user  nobody;
     3  worker_processes  1;
     4
     5  #error_log  logs/error.log;
     6  #error_log  logs/error.log  notice;
     7  #error_log  logs/error.log  info;
     8
     9  #pid        logs/nginx.pid;
    10
    11
    12  events {
    13      worker_connections  1024;
    14  }
    15
    16
    17  http {
    18      include       mime.types;
    19      default_type  application/octet-stream;
    20
    21      #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
法二:
[root@lll conf]# less -N nginx.conf
      1 
      2 #user  nobody;
      3 worker_processes  1;
      4 
      5 #error_log  logs/error.log;
      6 #error_log  logs/error.log  notice;
      7 #error_log  logs/error.log  info;
      8 
      9 #pid        logs/nginx.pid;
     10 
     11 
     12 events {
     13     worker_connections  1024;
     14 }
     15 
     16 
     17 http {
     18     include       mime.types;
     19     default_type  application/octet-stream;
     20 
     21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

总结:分页显示文件内容more与less

less命令常用参数-N,显示行号,less不接任何参数时,满屏显示文件内容。less可以向上滚动。

more命令后面不接任何参数,满屏显示文件内容。more不能向上滚动。


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

0 关机模式

1 单用户模式

2 无NFS的多用户模式

3 文本模式

4 无用

5图形化

6 重启模式

[root@lll conf]# runlevel 

N 3

总结:linux的启动过程图解https://blog.51cto.com/mrxiong2017/2084767


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

法一:

[root@lll conf]# export LANG='zh_CN.UTF-8'             ----此法执行后,仅在当前窗口生效
[root@lll conf]# echo $LANG
zh_CN.UTF-8
法二:
[root@lll conf]# echo 'LANG="zh_CN.UTF-8"'>/etc/sysconfig/i18n
[root@lll conf]# source /etc/sysconfig/i18n
[root@lll conf]# echo $LANG
zh_CN.UTF-8


10>简述生产环境中从哪些方面优化linux系统?

修改ip地址、网关、主机名、DNS等

关闭selinux,清空iptables

添加普通用户并进行sudo授权管理

更新yum源及必要软件安装

定时自动更新服务器时间

精简开机自启动服务

定时自动清理/var/spool/clientmqueue/目录垃圾文件,放置inode节点被占满

变更默认的ssh服务端口,禁止root用户远程连接

锁定关键文件系统

调整文件描述符大小

调整字符集,使其支持中文

去除系统及内核版本登录前的屏幕显示

内核参数优化


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

a.请用tar打包/etc整个目录(打包及压缩),再解开你打包的压缩包。

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

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

[root@lll /]# tar zcvf etc.tar.gz /etc                      -----a
[root@lll /]# tar zxvf etc.tar.gz -C /tmp/              -----c
[root@lll /]# tar zcvf etc.tar.gz  /etc --exclude=/etc/services          -----b

总结:

gzip打包组合zcvf,解包组合zxvf

bzip打包组合jcvf,解包组合jxvf

z(zip),c(create),v(verbose),f(file),x(extract),j(bzip)


12>已知如下命令及其结果:

[root@lll /]# echo 'I am oldboy myqq is 49000448'>>oldboy.txt
[root@lll /]# cat oldboy.txt 
I am oldboy myqq is 49000448

如果需要从文件中过滤出"oldboy"和"49000448"字符串,请给出命令

法一:

[root@lll /]# awk '{print $3" "$6}' oldboy.txt              --------awk分割从$1开始计数
oldboy 49000448
法二:
[root@lll /]# cut -d" " -f3,6 oldboy.txt 
oldboy 49000448
法三:
[root@lll /]# cut -c 6-11,20- oldboy.txt 
oldboy 49000448

总结:从文本中提取一段文字并输出cut命令常用参数总结

-c 以字符为单位进行分割

-d 自定义分隔符,默认以tab为分割符


13>如何查看文件/etc/services有多少行?

法一:

[root@lll /]# cat -n /etc/services|tail -1
 11176  matahari        49000/tcp               # Matahari Broker
法二:
[root@lll /]# wc -l /etc/services 
11176 /etc/services
[root@lll /]# ps -ef|grep ssh|wc -l              -----通过wc -l计算进程个数来判断启动是否正常
7