Linux 练习题-2基础 命令

1、过滤出已知当前目录下etc中的所有一级目录(不包含etc目录下面目录的子目录及隐藏目录,只能是一级目录)

ls -l | grep '^d'

ls -p | grep '/$'

ls -F | grep '/$'

find . -maxdepth 1 -type d ! -name "."

 

2、快速返回上次工作目录

cd -

每切换一次目录,系统就会将上次的工作目录保存到变量OLDPWD中,cd - 实际上调用了OLDPWD变量

 

3、在一个目录中,最快速度查到最近更新的文件。

ls -lrt

r倒序显示,把最新的文件放在最后

 

4、找出/data/test/目录下7天前的日志文件并删除

模拟场景

#!/bin/bash

for n in `seq 14` :

do

date -s "2018/04/$n" ;

touch access_www_$(date +%F).log ;

done

 

 find /data/test -type f -mtime +7 | xargs rm -f

find /data/test -type f -mtime +7 -exec rm -f {} \;

 

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

tail -f /var/log/message

tailf /var/log/message

 

6、打印/etc/passwd并显示文件行号

nl /etc/passwd  #不显示空格行行号,同cat -b

cat -n /etc/passwd  #显示所有行行号

vim /etc/passwd  命令模式下输入 set nu

grep -n . /etc/passwd  # .表示任意单个字符,过滤不出空格行

grep -n ".*" /etc/passwd # .*表示所有字符,可过滤出空格行

grep -n " *" /etc/passwd

awk '{print NR,$0}' /etc/passwd

less -N /etc/passwd

 

7、设置某服务开机自启动

image.png

chkconfig一个服务,实际上就是在rc#.d/目录下创建了一个指向该服务命令的软链接。在脚本里有分配好的启动顺序编号和停止顺序编号

image.png

 

sshd的基本为例,注意最后一行2345表示 2345level下自启 55是启动顺序编号 25是停止顺序编号。

你可以自己编写一个脚本,格式同下图,自己分配好启动编号和停止编号,不能大于99。然后chkconfig -add Sservice_name。就可以通过chkconfig管理了

image.png

 

8Linu查看中文乱码,如何解决

字符集的介绍

字符集简单的说就是一套文字符号及其编码。每个国家表现自己语言所用的字符集不同。美国是ASCII码,中国是GBK23,为了统一字符集的标准,出现了集成各个国家的字符集UTF-8

1、临时生效

export LANG="zh_CN.UTF-8"

2、永久生效

echo 'LANG="zh_CN.UTF-8"' > /etc/sysconfig/i18n

source /etc/sysconfig/i18n

 

9、打包命令的练习

1、用tar打包/etc 整个目录(打包及压缩)

tar -zcvf etc.tar.gz /etc

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

tar -zcvf etc.tar.gz /etc --exclude=/etc/services

3、把1)点命令的压缩包,解压到/data/test目录下

tar -xvf etc.tar.gz -C/data/test/

4、将/data/test下的所有.txt文件打包

find /data/test -type f -name "*.txt" | xargs tar zcvf txt.tar.gz

 

10、已知test.txt的内容为"I am cbl,myphone is 12345678"

1、从文件中过滤出“cbl”和“12345678”字符串

image.png

awk -F "[ ,]+" '{print $3,$7}' test.txt

cut -c 6-8,20- test.txt

2、从文件中过滤出“cbl,12345678”字符串

awk -F "[ ,]+" '{print $3","$7}' test.txt


猜你喜欢

转载自blog.51cto.com/12758568/2123163