Linux下有很多命令,但是这里只讲解常用的一些,以及这些命令在开发软件时好用的一面。

1. ls命令

[xbh@bogon test]$ ls -lR
.:
total 8
drwxrwxr-x 2 xbh xbh 4096 Nov 29 21:44 1
drwxrwxr-x 2 xbh xbh 4096 Nov 29 21:44 2

./1:
total 0
-rw-rw-r-- 1 xbh xbh 0 Nov 29 21:44 1.txt

./2:
total 0
-rw-rw-r-- 1 xbh xbh 0 Nov 29 21:44 2.txt

展示 当前目录及子目录下的文件详细信息。

 

2. find 命令

[xbh@bogon test]$ find . -name "*.txt" -exec ls -l {} \;
-rw-rw-r-- 1 xbh xbh 0 Nov 29 21:44 ./2/2.txt
-rw-rw-r-- 1 xbh xbh 0 Nov 29 21:44 ./1/1.txt

找出当前目录下以txt结尾的文件, 并列出它的详细信息。

> find .   #列出当前目录的文件以及子目录下的相关信息。

 

3. grep 命令

  -F 参数: 强制通配符等失效,该参数应该熟记。

[xbh@bogon 1]$ grep 'www.*.com' 1.txt
www.baidu.com
www.google.com
www.*.com
[xbh@bogon 1]$ grep -F 'www.*.com' 1.txt
www.*.com

-A 参数: 关键字所在行的after 几行也同步显示;

[xbh@bogon 1]$ grep -A 1 'ed' 1.txt
ed
www.baidu.com
[xbh@bogon 1]$ cat 1.txt
a
b
c
d
ed
www.baidu.com
www.google.com
www.*.com

-B 参数: 关键字所在行的Before 几行也同步显示;

[xbh@bogon 1]$ grep -B 1 'ed' 1.txt
d
ed
[xbh@bogon 1]$ cat 1.txt
a
b
c
d
ed
www.baidu.com
www.google.com
www.*.com

-w 参数: 只匹配whole word 

[xbh@bogon 1]$ grep 'd' 1.txt
d
ed
www.baidu.com

[xbh@bogon 1]$ grep -w 'd' 1.txt
d

-c 参数: 统计关键字出现次数, 与-w 的查询结果配合使用。

[xbh@bogon 1]$ grep -c 'd' 1.txt
3                 
[xbh@bogon 1]$ grep -wc 'd' 1.txt
1

-n 参数: 展示关键字所在行

[xbh@bogon 1]$ grep -n 'd' 1.txt
4:d
5:ed
6:www.baidu.com

 

4. free 命令

[xbh@bogon 1]$ free -m     ##以M为单位展示系统内存当前情况
        total             used   free  shared buffers cached
Mem: 503              492   10    0         131     186
-/+ buffers/cache:  175   328
Swap: 996            0       996
[xbh@bogon 1]$ expr 131 + 186
317
[xbh@bogon 1]$ expr 492 - 317
175

其中第二行的参数计算方式为:

175 = 492 - (131 + 196)  表示系统真正已经被占用的内存

 328 = 10 + (131 + 186) 表示系统可用内存,包括缓冲区buffer和缓存cache

 

5. lsof 命令

-i 参数: 查看指定端口的进程信息

[root@bogon xbh]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 2276 root 3u IPv6 7164 TCP *:ssh (LISTEN)

在Redhat上只有root用户有权限执行,Suse上普通用户即可。

-p参数: 根据进程号查询指定进程加载的文件,这个常用于在linux查找进程是否加载了正确的.so 文件

[root@bogon ~]# ps -fu xbh | grep test
xbh 25341 25261 0 22:22 pts/2 00:00:00 /bin/sh ./test.sh
[root@bogon ~]# lsof -p 25341
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
test.sh 25341 xbh cwd DIR 8,2 4096 2042485 /home/xbh/test
test.sh 25341 xbh rtd DIR 8,3 4096 2 /
test.sh 25341 xbh txt REG 8,3 735004 1849620 /bin/bash
test.sh 25341 xbh mem REG 8,3 125736 2335717 /lib/ld-2.5.so
test.sh 25341 xbh mem REG 8,3 1611564 2336488 /lib/libc-2.5.so
test.sh 25341 xbh mem REG 8,3 16428 2336489 /lib/libdl-2.5.so
...............
test.sh 25341 xbh 255r REG 8,2 56 2042498 /home/xbh/test/test.sh
[root@bogon ~]# cat /home/xbh/test/test.sh
#!/bin/sh

while true
do
echo "ssss"
sleep 3
done

 

6. awk 命令

讲linux命令不提到awk,sed就说明还是在练手阶段,还没有入门。

-F 参数:指定分隔符,field 的编号从1开始。

[xbh@bogon 1]$ cat /etc/passwd | tail -n 1
vbird1:x:502:502::/home/vbird1:/bin/bash
[xbh@bogon 1]$ cat /etc/passwd | tail -n 1 | awk -F':' '{print "Username:" $1 " Shell:" $7}'
Username:vbird1 Shell:/bin/bash

 

7. sed 命令

-n 参数:不展示打印信息,

[xbh@bogon 1]$ cat 1.txt
www.baidu.com
www.google.com
www.*.com
[xbh@bogon 1]$ sed -n 's#baidu#chinese#p' 1.txt

www.chinese.com

其中,p参数是打印当前匹配信息,s标示是替换

-i参数: 将修改持久化到文件中

[xbh@bogon 1]$ sed -i 's#baidu#chinese#' 1.txt
[xbh@bogon 1]$ cat 1.txt
www.chinese.com
www.google.com
www.*.com

 

8.ipcs 命令

-m 参数: 只展示共享内存相关的信息。

[xbh@bogon 1]$ ipcs -m

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 xbh 600 393216 2 dest
0x00000000 98305 xbh 600 393216 2 dest
0x00000000 131074 xbh 600 393216 2 dest
0x00000000 163843 xbh 600 393216 2 dest
0x00000000 196612 xbh 600 393216 2 dest
0x00000000 229381 xbh 600 393216 2 dest
0x00000000 262150 xbh 600 393216 2 dest
0x00000000 294919 xbh 600 393216 2 dest
0x00000000 327688 xbh 600 393216 2 dest
0x00000000 360457 xbh 600 393216 2 dest
0x00000000 393226 xbh 600 393216 2 dest

 

-p 参数: 展示共享内存相关进程信息

[xbh@bogon 1]$ ipcs -mp

------ Shared Memory Creator/Last-op --------
shmid owner cpid lpid
65536 xbh 2770 21373
98305 xbh 2772 2822
131074 xbh 2772 2822
163843 xbh 2803 2599
196612 xbh 2805 2599
229381 xbh 2863 2599
262150 xbh 2875 2599

cpid为创建共享内存的进程号,lpid 为last pid, 即最近attach到共享内存上的进程号。

 

9. ipcrm 命令

  -m 参数:根据shmid强制删除共享内存。

[xbh@bogon 1]$ ipcs -m

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 xbh 600 393216 2 dest
0x00000000 98305 xbh 600 393216 2 dest
0x00000000 131074 xbh 600 393216 2 dest
0x00000000 163843 xbh 600 393216 2 dest
0x00000000 196612 xbh 600 393216 2 dest
0x00000000 229381 xbh 600 393216 2 dest
0x00000000 262150 xbh 600 393216 2 dest
0x00000000 294919 xbh 600 393216 2 dest
0x00000000 327688 xbh 600 393216 2 dest
0x00000000 360457 xbh 600 393216 2 dest
0x00000000 393226 xbh 600 393216 2 dest

key为0x00000, 说明这块共享内存已经被释放,但是因为系统上还有attach到这块共享内存的进程未退出,所以未释放。等所有attach的进程均退出后,共享内存即被释放。

nattch 即为attach的进程数。

 

[xbh@bogon 1]$ ipcrm -m 65536

 

10. mount命令

> mount -t cifs -o username=administrator,password=1 //192.168.1.107/Users /root/sne

格式为: mount -t cifs -o usernmae=DomainName/userName,password=*****   windows_dir linux_dir

mount 有时候报alloc memory failed, 这个是因为挂载的windows目录下文件太大,windows7无法处理的原因,需要修改win7配置,这里不做描述。

 

11. umount 命令

umount 失败的第一种情形有其他终端在占用要卸载的目录

[root@bogon xbh]# umount /root/sne
unmount error 16 = Device or resource busy
Refer to the umount.cifs(8) manual page (man 8 umount.cifs)
unmount error 16 = Device or resource busy
Refer to the umount.cifs(8) manual page (man 8 umount.cifs)
[root@bogon xbh]# fuser /root/sne
/root/sne: 5936c
[root@bogon xbh]# ps -ef | grep 5936
root 5936 5933 0 12:44 pts/1 00:00:00 -bash

[root@bogon xbh]# kill -9 5936

 

使用fuser命令查看卸载目录的占用者进程号,再使用ps命令得到对应的进程名称,确认可以直接删除后使用kill命令杀死进程。

第二种卸载失败的情形:

[root@bogon xbh]# umount /root/sne
unmount error 16 = Device or resource busy
Refer to the umount.cifs(8) manual page (man 8 umount.cifs)
unmount error 16 = Device or resource busy
Refer to the umount.cifs(8) manual page (man 8 umount.cifs)
[root@bogon xbh]# fuser /root/sne
[root@bogon xbh]# pwd
/root/sne/xbh

[root@bogon xbh]# cd
[root@bogon ~]# pwd
/root

[root@bogon ~]# umount /root/sne
[root@bogon ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 9.5G 3.3G 5.8G 37% /
/dev/sda2 9.5G 156M 8.9G 2% /home
/dev/sda1 99M 12M 83M 12% /boot
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc 2.8G 2.8G 0 100% /media/RHEL_5.4 i386 DVD

继续卸载仍然报错,使用fuser没有看到占用进程,再使用pwd命令看到执行命令时的目录在卸载目录下。

cd到不是卸载目录的目录,再次执行umount命令成功,使用df -h 查看确认卸载成功。

 

posted on 2015-11-29 23:04  hui2702  阅读(497)  评论(0编辑  收藏  举报