Browse > Home /

| 订阅RSS

[命令集3.1]linux文件的权限

三月 18th, 2009 | No Comments | Posted in Hacks, Linux

用 chmod 和 chown 更改访问权限和所有权。对于所有用户的默认掩码(umask)可以在 /etc/profile (Linux)
或 /etc/login.conf (FreeBSD) 中修改。其默认掩码(umask)通常为 022。掩码可以和777做减法,从而得到755
的权限。
1 –x 执行 # Mode 764 = 执行/读/写 | 读/写 | 读
2 -w- 写 # |—所有者|—用户组|—其他用户|
4 r– 读
ugo=a u=所有者, g=用户组, o=其他用户, a=所有用户

# chmod [OPTION] MODE[,MODE] FILE    # MODE 可以是 [ugoa]*([-+=]([rwxXst]))
# chmod 640 /var/log/maillog         # 更改 maillog 访问权限为 -rw-r-----
# chmod u=rw,g=r,o= /var/log/maillog # 同上
# chmod -R o-r /home/*               # 递归去除所有其他用户的可读权限
                                       在可执行位设置 SUID (知道你在干什么!13)
# chmod u+s /path/to/prog            #
# find / -perm -u+s -print           # 查找所有设置过 SUID 位的程序
# chown user:group /path/to/file     # 改变文件的所有者和文件关联的组
# chgrp group /path/to/file          # 改变文件关联的组
# chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files
# chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories
Tags: , , ,

Linux秘笈25.Linux的Cut命令

三月 18th, 2009 | 2 Comments | Posted in Hacks, 软件架构

Cut command can be used to display only specific columns from a text file or
other command outputs.
Following are some of the examples.
Display the 1st field (employee name) from a colon delimited file

  $ cut -d: -f 1 names.txt
  Emma Thomas
  Alex Jason
  Madison Randy
  Sanjay Gupta
  Nisha Singh

Display 1st and 3rd field from a colon delimited file

  $ cut -d: -f 1,3 names.txt
  Emma Thomas:Marketing
  Alex Jason:Sales
  Madison Randy:Product Development
  Sanjay Gupta:Support
  Nisha Singh:Sales

Display only the first 8 characters of every line in a file

  $ cut -c 1-8 names.txt
  Emma Tho
  Alex Jas
  Madison
  Sanjay G
  Nisha Si
Tags: , , ,

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: , ,

[命令集]对前后台的进程进行调出和调入

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

前台/后台
当一个进程在shell 中已运行,可以使用[Ctrl]-[Z] (^Z), bg 和fg 来调入调出前后台10。举个例子:启动
2 个进程,调入后台。使用jobs 列出后台列表,然后再调入一个进程到前台。

# ping cb.vu > ping.log
^Z                # ping 使用 [Ctrl]-[Z] 来暂停(停止)
# bg # 调入后台继续运行
# jobs -l         # 后台进程列表
[1] - 36232 Running ping cb.vu > ping.log
[2] + 36233 Suspended (tty output) top
# fg %2           # 让进程 2 返回到前台运行

使用nohup 开启一个持续运行的进程直到 shell 被关闭(避免挂断)。
# nohup ping -i 60 > ping.log &

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: , , ,