Browse > Home /

| 订阅RSS

Linux秘笈24. Uniq命令

三月 17th, 2009 | No Comments | Posted in Linux

Uniq command is mostly used in combination with sort command, as uniq
removes duplicates only from a sorted file. i.e In order for uniq to work, all
the duplicate entries should be in the adjacent lines. Following are some
common examples.
1. When you have an employee file with duplicate entries, you can do the
following to remove duplicates.

   $ sort namesd.txt | uniq
   $ sort –u namesd.txt

2. If you want to know how many lines are duplicates, do the following. The
first field in the following examples indicates how many duplicates where
found for that particular line. So, in this example the lines beginning with
Alex and Emma were found twice in the namesd.txt file.

   $ sort namesd.txt | uniq –c
             2  Alex Jason:200:Sales
             2  Emma Thomas:100:Marketing
             1  Madison Randy:300:Product Development
             1  Nisha Singh:500:Sales
             1  Sanjay Gupta:400:Support

3. The following displays only the entries that are duplicates.

   $ sort namesd.txt | uniqcd
             2 Alex Jason:200:Sales
             2 Emma Thomas:100:Marketing
Tags: , ,

Kill命令与信号

三月 16th, 2009 | No Comments | Posted in Linux

使用kill 或killall 终止或发送一个信号给进程。
# ping -i 60 cb.vu > ping.log &
[1] 4712
# kill -s TERM 4712 # 同 kill -15 4712
# killall -1 httpd # 发送 HUP 信号终止进程 httpd
# pkill -9 http # 发送 TERM 信号终止包含 http 的进程
# pkill -TERM -u www # 发送 TERM 信号终止 www 所有者进程
# fuser -k -TERM -m /home # 终止所有访问 /home 的进程(卸载该分区前)
下面是一些重要的信号:
1 HUP (挂起)
2 INT (中断)
3 QUIT (退出)
9 KILL (KILL 信号不能被捕捉,不能被忽略。)
15 TERM (软件终止信号)

Tags: , ,

Linux秘笈23.Sort命令

三月 5th, 2009 | No Comments | Posted in Linux

Sort命令能够对一个文本文件的行进行排序,下面的几个实用的样例展示怎么去对样例文本进行排序。样例文本的格式为:employee_name:employee_id:department_name.

$ cat names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales

以一个升序排列文本文件

$ sort names.txt
Alex Jason:200:Sales
Emma Thomas:100:Marketing
Madison Randy:300:Product Development
Nisha Singh:500:Sales
Sanjay Gupta:400:Support

以倒序排列文本文件

$ sort -r names.txt
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Madison Randy:300:Product Development
Emma Thomas:100:Marketing
Alex Jason:200:Sales

用第二字段排列一个以冒号分隔的文本文件

$ sort -t: -k 2 names.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales

以第三个字段排列一个以tab分隔的文本文件,并且禁止重复

$ sort -t: -u -k 3 names.txt
Emma Thomas:100:Marketing
adison Randy:300:Product Development
Alex Jason:200:Sales
Sanjay Gupta:400:Support

通过ip地址来排序/etc/hosts文件

$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.100.101 dev-db.thegeekstuff.com dev-db
192.168.100.102 prod-db.thegeekstuff.com prod-db
192.168.101.20 dev-web.thegeekstuff.com dev-web
192.168.101.21 prod-web.thegeekstuff.com prod-web

合并sort和其他命令

ps –ef | sort : Sort the output of process list
ls -al | sort +4n : List the files in the ascending order of the file-size. i.e sorted by 5th filed and displaying smallest files first.
ls -al | sort +4nr : List the files in the descending order of the file-size. i.e sorted by 5th filed and displaying largest files first.
Tags: , , ,

Linux秘笈22. Xargs命令

三月 2nd, 2009 | No Comments | Posted in Linux

xargs是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令,下面是一些如何有效使用xargs的实用例子。

1. 当你尝试用rm删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs去避免这个问题。

find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

2. 获得/etc/下所有*.conf结尾的文件列表,有几种不同的方法能得到相同的结果,下面的例子仅仅是示范怎么实用xargs,在这个例子中实用xargs将find命令的输出传递给ls -l

 
# find /etc -name "*.conf" | xargs ls –l

3. 假如你有一个文件包含了很多你希望下载的URL,你能够使用xargs下载所有链接。

 
# cat url-list.txt | xargs wget –c

4. 查找所有的jpg文件,并且压缩他。

 
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

5. 拷贝所有的图片文件到一个外部的硬盘驱动

 
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Tags: , ,

Linux秘笈21. 改变文件内容的大小写

二月 28th, 2009 | 1 Comment | Posted in Linux

把一个文件转换成大小

$ cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
$ tr a-z A-Z < employee.txt
100 JASON SMITH
200 JOHN DOE
300 SANJAY GUPTA
400 ASHOK SHARMA

转换一个文件成小写

$ cat department.txt
100 FINANCE
200 MARKETING
300 PRODUCT DEVELOPMENT
400 SALES
$ tr A-Z a-z < department.txt
100 finance
200 marketing
300 product development
400 sales
Tags: , ,

Linux秘笈20. Join命令

二月 27th, 2009 | No Comments | Posted in Linux

Join命令是用一个共同的字段为基础从两个文件中合并行。在下面的例子中,我们有两个文件,employee.txt和salary.txt,两个文件都有一个共同的employee-id字段,所以我们用join命令去合并。

$ cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
$ cat bonus.txt
100 $5,000
200 $500
300 $3,000
400 $1,250
$ join employee.txt bonus.txt
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000
400 Ashok Sharma $1,250
Tags: ,

Linux秘笈18.Find命令

二月 26th, 2009 | Comments Off | Posted in Linux

find是经常使用的命令,用来在UNIX文件系统中查询文件,让我们来回顾一些练习find命令的例子

Syntax: find [pathnames] [conditions]

怎样查询一些特殊字符的文件名呢?

下面的命令是在/etc目录下查询所有包含mail字符的文件。

# find /etc -name "*mail*"

怎样去查询超过一定大小的文件?

下面的命令将列出系统中所有超过100MB的文件。

# find / -type f -size +100M

怎样去查询在过去X天没有修改的文件?

下面的命令将罗列在当前目录下60天前修改过的文件:

# find . -mtime +60

怎样查询在过去X天修改过的文件?

下面的命令将在当期目录下,查询在过去2天修改过的文件:

# find . –mtime -2

怎样删除所有超过100MB且后缀为*.tar.gz的归档文件?

请小心执行下面的命令,有可能回删除一些你不想删除的文件,最好的习惯是在执行删除命令的时候,先用ls命令查看一下。

# find / -type f -name *.tar.gz -size +100M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +100M -exec rm -f {} \;

怎样去归档已经超过X天没有修改过的文件呢?

下面的命令将查询所有在/home/jsmith目录下60天都没修改过的文件,并且创建一个ddmmyyyy_archive.tar格式的压缩文件。

# find /home/jsmith -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`
Tags: ,

[linux秘笈14]grep命令

二月 25th, 2009 | No Comments | Posted in Linux

grep命令被用来搜索所有一些文本文件,它是拥有些参数,非常厉害的一个工具,
Syntax: grep [options] pattern [files]

我怎样能在一个文件中查找所有匹配关键词的行呢?
在下面的例子中,用grep在/etc/passwd中查找John文字且显示匹配的行:

# grep John /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash

参数-v,将显示所有没有匹配的行,在下面的例子中,它将显示在/etc/passwd文件中没有匹配John的记录。

# grep -v John /etc/passwd
jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash

在一个文件中有多少行匹配文字模式吗?
下面的例子,将显示在/etc/passwd文件中有多少行包含了John。

# grep -c John /etc/passwd
2

你也能用-cv参数来得到有多少行没有包含John.

# grep -cv John /etc/passwd
39

怎样在搜索文本的时候忽略大小写呢?
通过-i参数将忽略大小写。

# grep -i john /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash

我怎样搜索子目录下的的文本文件呢?
用参数-r能够实现这个目的,下面的例子,将在/home/users目录下搜索所有忽略大小写包含”John”的文件。
将以”文件名: 匹配的行”.你也能用-l参数,仅仅展示匹配的文件名。

# grep -ri john /home/users /home/users/subdir1/letter.txt:John, Thanks for your contribution.
/home/users/name_list.txt:John Smith
/home/users/name_list.txt:John Doe
# grep -ril john /root /home/users/subdir1/letter.txt
/home/users/name_list.txt
Tags: , ,

[命令集]操作系统运行级别相关

二月 22nd, 2009 | No Comments | Posted in Linux

操作系统的运行级别
Linux
一旦内核加载完成,内核会启动 init 进程,然后运行 rc6 脚本,之后运行所有属于其运行级别的命令脚本。这
些脚本都储存在 /etc/rc.d/rcN.d 中(N代表运行级别),并且都建立着到 /etc/init.d 子目录中命令脚本程序
的符号链接。
默认运行级别配置在 /etc/inittab 中。它通常为 3 或 5:

 # grep default: /etc/inittab
 id:3:initdefault:

可以使用 init 来改变当前运行级别。举个例子:

 # init 5                       # 进入运行级别 5

运行级别列表如下:
0 系统停止
1 进入单用户模式(也可以是 S)
2 没有 NFS 特性的多用户模式
3 完全多用户模式(正常操作模式)
4 未使用
5 类似于级别3,但提供 XWindow 系统登录环境
6 重新启动系统
使用 chkconfig 工具控制程序在一个运行级别启动和停止。

 # chkconfig --list             # 列出所有 init 脚本
 # chkconfig --list sshd        # 查看 sshd 在各个运行级别中的启动配置
 # chkconfig sshd --level 35 on # 对 sshd 在级别 3 和 5 下创建启动项
 # chkconfig sshd off           # 在所有的运行级别下禁用 sshd

Debian 和基于Debian 发行版像 Ubuntu 或 Knoppix 使用命令 update-rc.d 来管理运行级别脚本。默认启动为
2,3,4 和 5,停止为 0,1 和 6。

 # update-rc.d  sshd defaults          # 设置 sshd 为默认启动级别
 # update-rc.d  sshd start 20 2 3 4 5 . stop 20 0 1 6 . # 用显示参数
 # update-rc.d  -f sshd remove         # 在所有的运行级别下禁用 sshd
 # shutdown -h  now (或者 # poweroff)    # 关闭停止系统

FreeBSD
BSD 启动步骤不同于 SysV, 她没有运行级别。她的启动状态(单用户,有或没有 XWindow)被配置在 /etc/
ttys中。所有的系统脚本都位于 /etc/rc.d/中,第三方应用程序位于 /usr/local/etc/rc.d/中。service 的启
动顺序被配置在 /etc/rc.conf 和/etc/rc.conf.local中。默认行为可在 /etc/defaults/rc.conf 中进行配
置。 这些脚本至少响应 start|stop|status.

 # /etc/rc.d/sshd status
 sshd is running as pid 552.
 # shutdown now                        # 进入单用户模式
 # exit                                # 返回到多用户模式
 # shutdown -p now                     # 关闭停止系统
 # shutdown -r now                     # 重新启动系统

同样可以使用进程 init 进入下列状态级别。举个例子: # init 6 为重启。
0 停止系统并关闭电源 (信号 USR2)
1 进入单用户模式 (信号 TERM)
6 重新启动 (信号 INT)
c 阻止进一步登录 (信号 TSTP)
q 重新检查 ttys(5) 文件 (信号 HUP)

Tags: , , ,

Linux秘笈13. 用SSh登录到远程的机器

二月 20th, 2009 | No Comments | Posted in Linux

当你第一次登录到远程的机器时候,将展示远程机器的Key没有发现之类的信息,你能给一个”yes”,继续操作,例如:

localhost$ ssh -l jsmith remotehost.example.com
Host key not found from database.
Key fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub
Are you sure you want to continue connecting (yes/no)? Yes
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700
jsmith@remotehost.example.com password:
remotehost.example.com$

当你第二次登录的时候,将不再进行提示了,例如:

localhost$ ssh -l jsmith remotehost.example.com
jsmith@remotehost.example.com password:
remotehost.example.com$

有的时候当远程的机器重新安装,或者重新生成SSH的KEY时候,你又会得到一些错误的信息,例如:

localhost$ ssh -l jsmith remotehost.example.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the host key has just been changed.
Please contact your system administrator.
Add correct host key to “/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub”
to get rid of this message.
Received server key’s fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.
Agent forwarding is disabled to avoid attacks by corrupted servers.
Are you sure you want to continue connecting (yes/no)? yes
Do you want to change the host key on disk (yes/no)? yes
Agent forwarding re-enabled.
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:17:31 -0700
jsmith @remotehost.example.com’s password:
remotehost$
Tags: ,