Linux学习小习题集合----不断更新中

Linux基础知识题:

1).假设在/tmp下有一个test.txt文件内容如下:

[root@moban ~]# cat /tmp/test.txt
59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:44:02 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 200 3583 "http://oldboy.blog.51cto.com/static/flex/AdobeVideoPlayer.swf?width=590&height=328&url=/[[DYNAMIC]]/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"

现在需要统计每个URL访问的总大小,并取得TOP10的排名

 

解题思路:

1.用AWK将日志文件中需要的URL及其文件的大小取出来

[root@moban ~]# awk '{print $7,$10}' /tmp/test.txt
/static/images/photos/2.jpg 11299
/static/images/photos/2.jpg 11299
/static/flex/vedioLoading.swf 3583
/?= 46232
/static/js/web_js.js 4460
/static/js/jquery.lazyload.js 1627

2.将取出来的文件按照URL进行排列,然后通过uniq进行去重复和统计

[root@moban ~]# awk '{print $7,$10}' /tmp/test.txt | sort | uniq -c
1 /?= 46232
1 /static/flex/vedioLoading.swf 3583
2 /static/images/photos/2.jpg 11299
1 /static/js/jquery.lazyload.js 1627
1 /static/js/web_js.js 4460

3.再次使用AWK将URL和文件大小与次数的乘积打印出来

[root@moban ~]# awk '{print $7,$10}' /tmp/test.txt | sort | uniq -c | awk '{print $2,$1*$3}'
/?= 46232
/static/flex/vedioLoading.swf 3583
/static/images/photos/2.jpg 22598
/static/js/jquery.lazyload.js 1627
/static/js/web_js.js 4460

4.再次对结果按照文件的总的下载大小进行排列

[root@moban ~]# awk '{print $7,$10}' /tmp/test.txt | sort | uniq -c | awk '{print $2,$1*$3}' | sort -k 2 -g --reverse
/?= 46232
/static/images/photos/2.jpg 22598
/static/js/web_js.js 4460
/static/flex/vedioLoading.swf 3583
/static/js/jquery.lazyload.js 1627

5.最后取出文件的首10行得出TOP10的结果

[root@moban ~]# awk '{print $7,$10}' /tmp/test.txt | sort | uniq -c | awk '{print $2,$3*$1}' | sort -k 2 -g --reverse | head
/?= 46232
/static/images/photos/2.jpg 22598
/static/js/web_js.js 4460
/static/flex/vedioLoading.swf 3583
/static/js/jquery.lazyload.js 1627


2) 假设有一个文件stu的内容如下所示:

[root@moban ~]# cat stu
stu10309
b7beb58f08
stu10310
12bd8b93fb
stu10311
d53396e575
stu10312
f3ca1841d2
stu10313
8cb55400f7
stu10314
c21fe97b45
stu10315
a283f28774
stu10316
7c14c28bfd
stu10317
42950407bf
stu10318
a3569b702d
stu10319
106bf29bf3
stu10320
601016ac7b
stu10321
36ef4738b2
stu10322
770645c2be
stu10323
e5ac87763d
stu10324
d05e8929c0
stu10325
544c8cc7e0
stu10326
b884398b68
stu10327
686ee8c05a

现在需要将文件转换为"stuXXXXXX = XXXXXXX"的形式存储为文件stu_pass

方法一:使用paste的-s参数,同时使用d指定分隔符

[root@moban ~]# paste -sd "=\n" stu     ========>这里使用了paste的特殊用法,轮流用"="和"\n"作为分割符号
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

方法二:直接使用paste -d参数实现

[root@moban ~]# paste -d"=" - - < stu
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

方法三:使用xargs+sed

[root@moban ~]# xargs -n 2 < stu | sed "s# #=#g"
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

方法四:只使用sed

[root@moban ~]# sed "N;s#\n#=#g" stu
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

方法五:使用AWK

[root@moban ~]# awk '{tmp=$0;getline;print tmp"="$0}' stu
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

[root@moban tmp]# awk '{getline tmp;print $0"="tmp}' ~/stu
stu10309=b7beb58f08
stu10310=12bd8b93fb
stu10311=d53396e575
stu10312=f3ca1841d2
stu10313=8cb55400f7
stu10314=c21fe97b45
stu10315=a283f28774
stu10316=7c14c28bfd
stu10317=42950407bf
stu10318=a3569b702d
stu10319=106bf29bf3
stu10320=601016ac7b
stu10321=36ef4738b2
stu10322=770645c2be
stu10323=e5ac87763d
stu10324=d05e8929c0
stu10325=544c8cc7e0
stu10326=b884398b68
stu10327=686ee8c05a

3)模拟inode占满造成磁盘空间不足

[root@moban ~]# dd if=/dev/zero of=/tmp/100k bs=1k count=100           ===>使用dd命令创建一个100K磁盘

100+0 records in

100+0 records out

102400 bytes (102 kB) copied, 0.000450046 s, 228 MB/s

 

[root@moban ~]# mkfs.ext4 /tmp/100k                                =====>使用mkfs.ext4格式化磁盘

mke2fs 1.41.12 (17-May-2010)

/tmp/100k is not a block special device.

Proceed anyway? (y,n) y

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

16 inodes, 100 blocks

5 blocks (5.00%) reserved for the super user

First data block=1

1 block group

8192 blocks per group, 8192 fragments per group

16 inodes per group

 

Writing inode tables: done                           

 

Filesystem too small for a journal

Writing superblocks and filesystem accounting information: done

 

This filesystem will be automatically checked every 34 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@moban ~]# mkdir -p /app/logs

[root@moban ~]# mount -o loop /tmp/100k  /app/logs/                                 ======>挂载格式化后的磁盘

[root@moban ~]# df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda3       18759984 2065836  15734508  12% /

tmpfs             502068       0    502068   0% /dev/shm

/dev/sda1         194241   33777    150224  19% /boot

/tmp/100k             93      14        74  16% /app/logs

[root@moban ~]# df -i

Filesystem      Inodes IUsed   IFree IUse% Mounted on

/dev/sda3      1201872 83633 1118239    7% /

tmpfs           125517     1  125516    1% /dev/shm

/dev/sda1        51200    38   51162    1% /boot

/tmp/100k           16    11       5   69% /app/logs

[root@moban ~]#

[root@moban ~]# for i in  `seq 1 6`; do touch /app/logs/$i.txt;done

touch: cannot touch `/app/logs/6.txt': No space left on device

[root@moban ~]#

[root@moban ~]#

[root@moban ~]#

[root@moban ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        18G  2.0G   16G  12% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   33M  147M  19% /boot

/tmp/100k        93K   14K   74K  16% /app/logs

[root@moban ~]# df -i

Filesystem      Inodes IUsed   IFree IUse% Mounted on

/dev/sda3      1201872 83633 1118239    7% /

tmpfs           125517     1  125516    1% /dev/shm

/dev/sda1        51200    38   51162    1% /boot

/tmp/100k           16    16       0  100% /app/logs

3)显示/etc/inittab中以#开头,且后面跟了一个或多个空白字符,而后又跟了任意非空白字符的行

[root@moban ~]# sed -n "/^#\s\+\.*/p" /etc/inittab
# inittab is only used by upstart for the default runlevel.
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# System initialization is started by /etc/init/rcS.conf
# Individual runlevels are started by /etc/init/rc.conf
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#

4)将/etc/passwd 的内容列出并打印行号,同时,将2-5行删除显示

[root@moban ~]# cat -n /etc/passwd | sed "2,5d"
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11 operator:x:11:0:operator:/root:/sbin/nologin
12 games:x:12:100:games:/usr/games:/sbin/nologin
13 gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
14 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
15 nobody:x:99:99:Nobody:/:/sbin/nologin
16 dbus:x:81:81:System message bus:/:/sbin/nologin
17 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
18 abrt:x:173:173::/etc/abrt:/sbin/nologin
19 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
20 ntp:x:38:38::/etc/ntp:/sbin/nologin
21 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
22 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
23 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
24 oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
25 tcpdump:x:72:72::/:/sbin/nologin
26 duanzl:x:500:500::/home/duanzl:/bin/bash
27 oldboy:x:501:501::/home/oldboy:/bin/bash
28 lab:x:502:502::/home/lab:/bin/bash
29 lab1:x:503:503::/lab1:/bin/bash

5)磁盘报错 “no space left on device”,但是df -h 查看磁盘空间没满,请问为什么?

当系统出现“no space left on device”的时候,我们首先使用df -h查看磁盘的空间(block)是否占满了。

[root@moban tmp]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 2.0G 15G 12% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 33M 147M 19% /boot
/tmp/100k 93K 14K 74K 16% /tmp/data

然后我们发现磁盘block并没有没占满,这个时候我们需要使用df -hi查看系统的inode使用情况

[root@moban tmp]# df -hi
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 1.2M 82K 1.1M 7% /
tmpfs 123K 1 123K 1% /dev/shm
/dev/sda1 50K 38 50K 1% /boot
/tmp/100k 16 16 0 100% /tmp/data

这个时候我们发现了inode被占满了,这个时候我们需要排查系统目录下那个目录下的小文件比较多

posted on 2018-01-22 11:03  吃胖了的包子  阅读(38)  评论(0)    收藏  举报