Linux面试题集锦

1、给你一个rpm包,查询是否安装,安装了如何删除

rpm -qa rsync  查看是否安装rsync包

rpm -e  rsync 卸载rsync包  --nodeps   忽略依赖关系

2、 查看磁盘空间

df -h

3、443端口是什么服务

cat /etc/service

https

4、 有一些文件,如何把文件的扩展名.txt改为.html

方法1:
rename .txt  .html   *.txt

方法2:
find . -type f-name "*.txt" -print0 | xargs -0 rename .txt .html {}
使用find的-print0和 xargs的-0选项,可以解决文件名中包含空格的问题。

方法3:
for file in *.txt; do mv $file${file%.txt}.html; done


5、查询file里面空行的所在行号

grep -n ^$ 123.sh
sed -n '/^$/='123.sh
awk '$0 ~ /^$/{print NR}' 123.sh


6、查询file1以abc结尾的行

grep -n "tmp$" 123.sh   优

sed -n '/tmp$/=' 123.sh

awk '/tmp$/{print $0}' 123.sh


7、 打印出file1文件第一到第三行

head -3 123.sh

sed -n'1,3p' 123.sh

awk 'NR==1,NR==3{print $0}' 123.sh

8、请把ls -l nagios的9位权限字符转换为数字权限(请给出不低于两种方法)
[mads@mysql ~]$ ls -al nagios
-rw-r--r-- 1 mads mygroup 0 May  6 23:26 nagios
解答1:
[mads@mysql ~]$ stat nagios|awk -F "[0/]+" 'NR==4{print $2}'
644
解答2
[mads@mysql ~]$  stat nagios|awk 'NR==4'|cut -d0 -f2|cut -d/ -f1
644

解答3
[mads@mysql ~]$  stat nagios|sed -rn 's#^.*\(0(.*)/-.*$#\1#p'
644

解答4
[mads@mysql ~]$ stat -c %a nagios   #扩展知识点
644

解答5
[mads@mysql ~]$ ls -l nagios |tr 'rwx-' '4210'|awk -F "" '{print $2+$3+$4$5+$6+$7$8+$9+$10}'
644

解答6
使用取行取列方法结合head tail结合取行 sed awk取行 cut awk取列

9、显示倒计时

方法1:

#!/bin/bash
for i in `seq -w 10 -1 1`
  do
    echo -ne "\b\b$i";
    sleep 1;
  done
echo -e "\b\bhello world!"

通过echo的-e参数中的\b删掉前一个输出来实现的


方法2:

#!/bin/bash

for i in $(seq -w 10 -1 1)
do
        echo -en "\a${i}\r"
        sleep 1
done
echo

10、检测服务端口是否开启思路
本地查看
方法1:netstat
[root@mysql 20150504]# netstat -ltunp|grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      28700/mysqld

方法2:ss
[root@mysql 20150504]# ss -ltunp|grep 3306
tcp    LISTEN     0     600                    *:3306                  *:*      users:(("mysqld",28700,12))

方法3:lsof
[root@mysql 20150504]# lsof -i :3306
COMMAND   PID  USER  FD   TYPE DEVICE SIZE/OFF NODENAME
mysqld  28700mysql   12u  IPv4 110253      0t0 TCP *:mysql (LISTEN)

远程查看(远程方法同样适用于本地)
方法1:telnet
[root@mysql 20150504]# telnet 172.16.1.161 3308
Trying 172.16.1.161...
Connected to 172.16.1.161.
Escape character is '^]'.
Host '172.16.1.161' is not allowed to connect to this MySQLserverConnection closed by foreign host.

方法2:nmap
[root@mysql 20150504]# nmap 172.16.1.161 -p 3306
Starting Nmap 5.51 ( http://nmap.org ) at 2015-05-0820:48 CST
Nmap scan report for localhost (172.16.1.161)
Host is up (0.00012s latency).
PORT     STATESERVICE
3306/tcp open mysql
Nmap done: 1 IP address (1 host up) scanned in 6.67seconds

方法3:nc
[root@mysql 20150504]# nc -w 5  172.16.1.161 3308
Eost '172.16.1.161' is not allowed to connect to thisMySQL server


本文出自 “小杩的幸福生活” 博客,谢绝转载!

你可能感兴趣的:(linux,File,如何,空间)