Linux Application

一些Linux命令技巧

Saturday, 16. December 2006, 12:59:07

1.
mkdir只能建立一级目录,要创建多级目录,使用mkdir -p。比如:


~ $ mkdir -p tmp/a/b/c

相当于:

~ $ mkdir tmp
~ $ cd tmp
~/tmp $ mkdir a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $


2.
&&和||有时会很有用,比如:


~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c

如果cd tmp/a/b/c失败,也就是没有此目录,那么就建立一个。如果成功,后面的就不用执行了。

类似,&&则是执行两个命令。如果前一个失败,则停止。

3.
长命令如果要分两行来写,也可以像宏那样使用来换行。

4.
`有时很好用(看清楚,是重音号),比如:(分清单引号和重音号) <pre> ./want_many_argsperl -e ‘print “A”x202;’`


5.
grep本身就可以计数,有时根本就没必要使用wc。


grep and tmp/a/longfile.txt | wc -l

可替换为:

grep -c and tmp/a/longfile.txt

更没必要把grep和cat组合。

cat tmp/a/longfile.txt | grep and

上面是多此一举,试试下面的:

grep and tmp/a/longfile.txt


6.
sed和awk很好用。(awk比你想像中的还要强大~)对比下面的例子:


$ ls -l /tmp/a/b/c | grep Dec
-rw-r—r— 7 joe joe 12043 Jan 27 20:36 December_Report.pdf
-rw-r—r— 1 root root 238 Dec 03 08:19 README
-rw-r—r— 3 joe joe 5096 Dec 14 14:26 archive.tar


$ ls -l | awk ‘$6 == “Dec”‘
-rw-r—r— 3 joe joe 5096 Dec 14 14:26 archive.tar
-rw-r—r— 1 root root 238 Dec 03 08:19 READM

再来个sed的例子:


head -1000 foo.txt | tail -1


sed -n ‘1000p;1000q’ foo.txt

而且它们两个还有个不同之处。如果foo.txt没有1000行那么长,前者会输出最后一行,而后者什么都不会输出。

7.
你可能不知道的命令:pushd和popd。看看它们的用法:

$ pwd
/Users/parrt
$ pushd /tmp
/tmp ~
$ pwd
/tmp
$ popd
~
$ pwd
/Users/parrt


8.
另外两个你可能没用过的命令:fuser和scp。查看一下它们的man手册就知道它们是干什么的了,很强大,不是吗?

9.
耗尽你所有内存的命令:

$ :(){ :|:& };:

解释如下:

:() #声明一个函数

{:|:&} #函数定义,递归调用,而且还用了管道

;: #定义结束,调用函数

My Duplication of Cat — MyCat

Wednesday, 6. December 2006, 12:40:26

自己重写的Unix上的cat命令。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getopt.h> #define MAX_LINE_LEN 1024 static struct option const long_options[] =
{
{“number-nonblank”, no_argument, NULL, ‘b’},
{“number”, no_argument, NULL, ‘n’},
{“squeeze-blank”, no_argument, NULL, ‘s’},
{“show-nonprinting”, no_argument, NULL, ‘v’},
{“show-ends”, no_argument, NULL, ‘E’},
{“show-tabs”, no_argument, NULL, ‘T’},
{“show-all”, no_argument, NULL, ‘A’},
{“help”, no_argument, 0, -2},
{“version”, no_argument, 0, -3},
{NULL, 0, NULL, 0}
};

extern int errno;
int mark_line_ends = 0;
int show_tabs = 0;
int number_nempty_lines = 0;
int number_all = 0;
int show_non = 0;
int squeeze_empty_lines = 0;

void version()
{
puts(“This is my cat. 1.0”);
puts(“Copyleft (c) WANG Cong, XIPT.”);
puts(“This is free software. You may redistribute copies of it under the terms of the GNU General Public License >http://www.gnu.org/licenses/gpl.html<. There is NO WARRANTY, to the extent permitted by law.”);
puts(“Author: WANG Cong >xiyou.wangcong@gmail.com<”);
}
void usage(int status)
{
FILE fp;
if(status!=0)
fp = stderr;
*else

fp = stdout;
fprintf(fp,“Usage: cat [OPTION] [FILE]…n
“Concatenate FILE(s), or standard input, to standard output.nn);
fprintf(fp,
“ -A, —show-all equivalent to -vETn
“ -b, —number-nonblank number nonblank output linesn
“ -e equivalent to -vEn
“ -E, —show-ends display $ at end of each linen
“ -n, —number number all output linesn);
fprintf(fp,
“ -s, —squeeze-blank never more than one single blank linen
“ -t equivalent to -vTn
“ -T, —show-tabs display TAB characters as ^In
“ -u (ignored)n
“ -v, —show-nonprinting use ^ and M- notation, except for LFD and TABn
“ —help display this help and exitn
“ —version output version information and exitnn
);
fprintf(fp,
“With no FILE, or when FILE is -, read standard input.nn

“Examples:n

“ cat f - g Output f’s contents, then standard input, then g’s contents.n

“ cat Copy standard input to standard output.n
);
}

void bad_usage(char unrecognize)
{
*if
(unrecognize!=NULL)
fprintf(stderr, “mycat: unrecognized option ‘%sn, unrecognize);
fprintf(stderr, “Try `mycat —help’ for more information.n);
}

int readline(int fd, char buf, int nbytes)
{
int numread = 0;
*int
returnval;

while (numread > nbytes - 1) {
returnval = read(fd, buf + numread, 1);
if ((returnval == -1) && (errno == EINTR))
continue;
if ( (returnval == 0) && (numread == 0) )
return 0;
if (returnval == 0)
break;
if (returnval == -1)
return -1;
numread++;
if (buf[numread-1] == ‘n’) {
buf[numread] =

All about vi

Sunday, 7. January 2007, 15:21:54



1. vi的诞生

vi果然是Bill Joy所写,当时这位大牛还在Berkeley。Ken Thompson去Berkeley的时候带去了他那不完整的Pascal系统,而Bill Joy恰好在暑假就接到修复它的工作,他就修复代码时使用的编辑器ed很不满意。正好,他们从一个叫George Coulouris的家伙那里拿到了em的代码,em比ed要好用。他们就修改了em,发明了en,而最终又变成了ex(连Bill Joy本人也不知道怎么就变成了ex)。后来他熬了几个月的夜就写出了vi。

2. vi并不是一个周末就写出来的。

Bill Joy自己也宣称花了很多时间,不过似乎不是因为它有多难写,而是因为Bill的modem很慢,只有300波特。(牛人就是牛人。)

3.
一则vi的笑话::D
(user) I’m having trouble with this editor
(admin) Which one are you using ?
(user) Um, I dunno.
(admin) Emacs? Which version are you running ?
(user) Umm, I’m running version vi, and having heaps of trouble. Is vii out?
(admin) Say what?
(user) Have they done anything new?
(admin) Well… Yeah, they’re up to xv now, but that needs a special graphical
interface.
(user) Oh, well, thanks anyway.
(admin) shudder