Browse > Home / Archive by category 'Linux'

| 订阅RSS

[Linux秘笈28].显示用户连接时间

三月 21st, 2009 | No Comments | Posted in Linux

Ac command will display the statistics about the user’s connect time.
Connect time for the current logged in user
With the option –d, it will break down the output for the individual days. In
this example, I’ve been logged in to the system for more than 6 hours today.
On Dec 1st, I was logged in for about 1 hour.

   $ ac –d
   Dec 1      total             1.08
   Dec 2      total             0.99
   Dec 3      total             3.39
   Dec 4      total             4.50
   Today      total             6.10

Connect time for all the users
To display connect time for all the users use –p as shown below. Please note
that this indicates the cumulative connect time for the individual users.

   $ ac -p
              john                                            3.64
              madison                                      0.06
              sanjay                                          88.17
              nisha                                           105.92
             ramesh                                        111.42
             total 309.21

Connect time for a specific user
To get a connect time report for a specific user, execute the following:

  $ ac -d sanjay
  Jul 2      total          12.85
  Aug 25     total            5.05
  Sep 3      total            1.03
  Sep 4      total            5.37
  Dec 24     total            8.15
  Dec 29     total            1.42
  Today      total            2.95
Tags: , ,

[命令集3.3]FreeBSD下的启动引导

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

如果新内核不能引导,要引导一个旧内核,停止启动倒计时,做如下动作:

 # unload
 # load kernel.old
 # boot
Tags: , , ,

[linux秘笈27]Diff命令

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

diff命令,通常用来比较两个文件的差别,命令的输出比较隐晦,不能直接阅读。使用方法

   Syntax: diff [options] file1 file2

具体的使用方法

jcai@jcai-notebook:~$ diff --help
Usage: diff [OPTION]... FILES
Compare files line by line.
 
  -i  --ignore-case  Ignore case differences in file contents.
  --ignore-file-name-case  Ignore case when comparing file names.
  --no-ignore-file-name-case  Consider case when comparing file names.
  -E  --ignore-tab-expansion  Ignore changes due to tab expansion.
  -b  --ignore-space-change  Ignore changes in the amount of white space.
  -w  --ignore-all-space  Ignore all white space.
  -B  --ignore-blank-lines  Ignore changes whose lines are all blank.
  -I RE  --ignore-matching-lines=RE  Ignore changes whose lines all match RE.
  --strip-trailing-cr  Strip trailing carriage return on input.
  -a  --text  Treat all files as text.
 
  -c  -C NUM  --context[=NUM]  Output NUM (default 3) lines of copied context.
  -u  -U NUM  --unified[=NUM]  Output NUM (default 3) lines of unified context.
    --label LABEL  Use LABEL instead of file name.
    -p  --show-c-function  Show which C function each change is in.
    -F RE  --show-function-line=RE  Show the most recent line matching RE.
  -q  --brief  Output only whether files differ.
  -e  --ed  Output an ed script.
  --normal  Output a normal diff.
  -n  --rcs  Output an RCS format diff.
  -y  --side-by-side  Output in two columns.
    -W NUM  --width=NUM  Output at most NUM (default 130) print columns.
    --left-column  Output only the left column of common lines.
    --suppress-common-lines  Do not output common lines.
  -D NAME  --ifdef=NAME  Output merged file to show `#ifdef NAME' diffs.
  --GTYPE-group-format=GFMT  Similar, but format GTYPE input groups with GFMT.
  --line-format=LFMT  Similar, but format all input lines with LFMT.
  --LTYPE-line-format=LFMT  Similar, but format LTYPE input lines with LFMT.
    LTYPE is `old', `new', or `unchanged'.  GTYPE is LTYPE or `changed'.
    GFMT may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
    LFMT may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Either GFMT or LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
 
  -l  --paginate  Pass the output through `pr' to paginate it.
  -t  --expand-tabs  Expand tabs to spaces in output.
  -T  --initial-tab  Make tabs line up by prepending a tab.
 
  -r  --recursive  Recursively compare any subdirectories found.
  -N  --new-file  Treat absent files as empty.
  --unidirectional-new-file  Treat absent first files as empty.
  -s  --report-identical-files  Report when two files are the same.
  -x PAT  --exclude=PAT  Exclude files that match PAT.
  -X FILE  --exclude-from=FILE  Exclude files that match any pattern in FILE.
  -S FILE  --starting-file=FILE  Start with FILE when comparing directories.
  --from-file=FILE1  Compare FILE1 to all operands.  FILE1 can be a directory.
  --to-file=FILE2  Compare all operands to FILE2.  FILE2 can be a directory.
 
  --horizon-lines=NUM  Keep NUM lines of the common prefix and suffix.
  -d  --minimal  Try hard to find a smaller set of changes.
  --speed-large-files  Assume large files and many scattered small changes.
 
  -v  --version  Output version info.
  --help  Output this help.
 
FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or `FILE... DIR'.
If --from-file or --to-file is given, there are no restrictions on FILES.
If a FILE is `-', read standard input.

当用我的新文件去和老文件比较那些发生了修改?
当执行比较的时候,在diff命令中使用选项 -w 能够忽略掉空格,
查看diff输出时候的注意是:

  1. ‘—’ 线上面的是第一个文件发生的变化
  2. ‘—’线向下的第二个文件发生的变化
  3. < 符号表示的内容,属于第一个文件,>符号表示的内容属于第二个文件
jcai@jcai-notebook:~/tmp$ cat test.txt
我们
你们现在还好吗?比较文本
jcai@jcai-notebook:~/tmp$ cat test2.txt
他们
我们
你们现在还好吗?
jcai@jcai-notebook:~/tmp$ diff test.txt test2.txt
0a1
> 他们
2c3
< 你们现在还好吗?比较文本
---
> 你们现在还好吗?
Tags: , , ,

[命令集3.2]Linux下和磁盘相关的命令

三月 19th, 2009 | No Comments | Posted in Linux
# diskinfo -v /dev/ad2 # 显示磁盘信息(扇区/大小) (FreeBSD)
# hdparm -I /dev/sda   # 显示 IDE/ATA 磁盘信息 (Linux)
# fdisk /dev/ad2       # 显示和修改磁盘分区表
# smartctl -a /dev/ad2 # 显示磁盘检测信息
Tags: , ,

[linux秘笈26]Stat命令

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

Stat命令被用来查看单个文件或者文件系统的状态以及属性。
显示某一个文件或者文件系统的状态

jcai@jcai-notebook:~$ stat /etc/mysql/my.cnf 
  File: `/etc/mysql/my.cnf'
  Size: 3978      	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 449073      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-03-18 21:31:03.000000000 +0800
Modify: 2009-03-04 18:08:03.000000000 +0800
Change: 2009-03-04 18:08:03.000000000 +0800
jcai@jcai-notebook:~$ stat /home/jcai
  File: `/home/jcai'
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: fe00h/65024d	Inode: 2187265     Links: 66
Access: (0755/drwxr-xr-x)  Uid: ( 1000/    jcai)   Gid: ( 1000/    jcai)
Access: 2009-03-19 08:14:47.000000000 +0800
Modify: 2009-03-19 09:22:12.000000000 +0800
Change: 2009-03-19 09:22:12.000000000 +0800

使用 -f 选项来战士文件系统的状态

jcai@jcai-notebook:~$ stat -f /
  File: "/"
    ID: f346283e2da86295 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 4845177    Free: 3879940    Available: 3635752
Inodes: Total: 1224000    Free: 1082670
Tags: , , ,

[命令集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秘笈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: , ,