Skip navigation.

极湖

无不用其“极”

Posts tagged with "Linux"

部分删除log文件,保留最后n行的简单命令

,

为了删除不断增长的log文件,不过需要保留最后的部分,于是“发明”以下命令:

# tail -10000 procmaillog > procmaillog_tmp && mv procmaillog_tmp procmaillog

记录命令于此,以备用。

带压缩和排除条件的 tar 用法举例

,

tar:

tar -czf tarball.tar.gz --exclude=pattern fileslist

例):

tar -czf website.tar.gz --exclude=*.zip public_html

--exclude-from=file-of-patterns

例):

tar -cvf mytest.tar --exclude-from=testfile bin

tar -zcvf trial.tar.gz trial --exclude=cpon/log/* --exclude=cpon/syslog/* --exclude=cpon/cache/* --exclude=*2005*

tar -czf trial.tar ./* --no-anchored --exclude=./build/lib/*.a --exclude=*/OBJS/* --exclude=*/lib/solaris/*

untar:

tar -zxf website.tar.gz


想要实现更复杂的功能,请看帮助:

man tar

Linux下删除具有特殊名字的文件

,

不小心生成一个名为“--exclude”的文件,用如下命令:

rm --exclude
rm \-\-exclude
rm "--exclude"

都不能将其删除。

终于找到如下方法:

  假设Linux系统中有一个文件名叫“-ee”,如果我们想对它进行操作,例如要删除它,按照一般的删除方法在命令行中输入rm -ee命令,界面会提示我们是“无效选项”(invalid option),原来由于文件名的第一个字符为“-”,Linux把文件名当作选项了,我们可以使用“--”符号来解决这个问题,输入“rm -- -ee”命令便可顺利删除名为“-ee”的文件。如果是其他特殊字符的话可以在特殊字符前加一个“”符号,或者用双引号把整个文件名括起来。

于是用以下命令:

rm -- --exclude


成功删除文件。

[转载] xargs 命令的用法(英文)

, ,

原文地址 : http://www.unixreview.com/documents/s=8274/sam0306g/
作者 : Ed Schaefer

(July 2003 — see Web-exclusive update at end of article)

Many UNIX professionals think the xargs command, construct and execute argument lists, is only useful for processing long lists of files generated by the find command. While xargs dutifully serves this purpose, xargs has other uses. In this article, I describe xargs and the historical "Too many arguments" problem, and present eight xargs "one-liners":

  • Find the unique owners of all the files in a directory.
  • Echo each file to standard output as it deletes.
  • Duplicate the current directory structure to another directory.
  • Group the output of multiple UNIX commands on one line.
  • Display to standard output the contents of a file one word per line.
  • Prompt the user whether to remove each file individually.
  • Concatenate the contents of the files whose names are contained in file into another file.
  • Move all files from one directory to another directory, echoing each move to standard output as it happens.

Examining the "Too Many Arguments" Problem

In the early days of UNIX/xenix, it was easy to overflow the command-line buffer, causing a "Too many arguments" failure. Finding a large number of files and piping them to another command was enough to cause the failure. Executing the following command, from Unix Power Tools, first edition (O'Reilly & Associates):

pr -n 'find . -type f -mtime -1 -print'|lpr

will potentially overflow the command line given enough files. This command provides a list of all the files edited today to pr, and pipes pr's output to the printer. We can solve this problem with xargs:

find . -type f -mtime -1 -print|xargs pr -n |lp

With no options, xargs reads standard input, but only writes enough arguments to standard output as to not overflow the command-line buffer. Thus, if needed, xargs forces multiple executions of pr -n|lp.

While xargs controls overflowing the command-line buffer, the command xargs services may overflow. I've witnessed the following mv command fail -- not the command-line buffer -- with an argument list too long error:

find ./ -type f -print | xargs -i mv -f {} ./newdir

Limit the number of files sent to mv at a time by using the xargs -l option. (The xargs -i () syntax is explained later in the article). The following command sets a limit of 56 files at time, which mv receives:

find ./ -type f -print | xargs -l56 -i mv -f {} ./newdir

The modern UNIX OS seems to have solved the problem of the find command overflowing the command-line buffer. However, using the find -exec command is still troublesome. It's better to do this:

# remove all files with a txt extension

find . -type f -name "*.txt" -print|xargs rm

than this:

find . -type f -name "*.txt" -exec rm {} \; -print

Controlling the call to rm with xargs is more efficient than having the find command execute rm for each object found.

xargs One-Liners

The find-xargs command combination is a powerful tool. The following example finds the unique owners of all the files in the /bin directory:

# all on one line
find /bin -type f -follow | xargs ls -al | awk ' NF==9 { print $3 }'|sort -u

If /bin is a soft link, as it is with Solaris, the -follow option forces find to follow the link. The xargs command feeds the ls -al command, which pipes to awk. If the output of the ls -al command is 9 fields, print field 3 -- the file owner. Sorting the awk output and piping to the uniq command ensures unique owners.

You can use xargs options to build extremely powerful commands. Expanding the xargs/rm example, let's assume the requirement exists to echo each file to standard output as it deletes:

find . -type f -name "*.txt" | xargs -i ksh -c "echo deleting {}; rm {}"

The xargs -i option replaces instances of {} in a command (i.e., echo and rm are commands).

Conversely, instead of using the -i option with {}, the xargs -I option replaces instances of a string. The above command can be written as:

find . -type f -name "*.txt" | xargs -I {} ksh -c "echo deleting {}; rm {}"

The new, third edition of Unix Power Tools by Powers et al. provides an xargs "one-liner" that duplicates a directory tree. The following command creates in the usr/project directory, a copy of the current working directory structure:

find . -type d -print|sed 's@^@/usr/project/@'|xargs mkdir

The /usr/project directory must exist. When executing, note the error:

mkdir: Failed to make directory "/usr/project/"; File exists

which doesn't prevent the directory structure creation. Ignore it. To learn how the above command works, you can read more in Unix Power Tools, third edition, Chapter 9.17 (O'Reilly & Associates).

In addition to serving the find command, xargs can be a slave to other commands. Suppose the requirement is to group the output of UNIX commands on one line. Executing:

logname; date

displays the logname and date on two separate lines. Placing commands in parentheses and piping to xargs places the output of both commands on one line:

(logname; date)|xargs

Executing the following command places all the file names in the current directory on one line, and redirects to file "file.ls":

ls |xargs echo > file.ls

Use the xargs number of arguments option, -n, to display the contents of "file.ls" to standard output, one name per line:

cat file.ls|xargs -n1

# from Unix in a Nutshell
In the current directory, use the xargs -p option to prompt the user to remove each file individually:

ls|xargs -p -n1 rm

Without the -n option, the user is prompted to delete all the files in the current directory.

Concatenate the contents of all the files whose names are contained in file:

xargs cat < file > file.contents

into file.contents.

Move all files from directory $1 to directory $2, and use the xargs -t option to echo each move as it happens:

ls $1 | xargs -I {} -t mv $1/{} $2/{}

The xargs -I argument replaces each {} in the string with each object piped to xargs.

Conclusion

When should you use xargs? When the output of a command is the command-line options of another command, use xargs in conjunction with pipes. When the output of a command is the input of another command, use pipes.

References

Powers, Shelley, Peek, Jerry, et al. 2003. Unix Power Tools. Sebastopol, CA: O'Reilly & Associates.

Robbins, Arnold. 1999. Unix in a Nutshell. Sebastopol, CA: O'Reilly & Associates.

Ed Schaefer is a frequent contributor to Sys Admin. He is a software developer and DBA for Intel's Factory Integrated Information Systems, FIIS, in Aloha, Oregon. Ed also hosts the monthly Shell Corner column on UnixReview.com. He can be reached at: shellcorner@comcast.net.

July 2003 UPDATE from the author:

I've received very positive feedback on my xargs article. Other readers have shared constructive criticism concerning:

1. When using the duplicate directory tree "one-liner", reader Peter Ludemann suggests using the
mkdir -p option:

find . -type d -print|sed 's@^@/usr/project/@'|xargs mkdir -p

instead of :

find . -type d -print|sed 's@^@/usr/project/@'|xargs mkdir

mkdir's "-p" option creates parent directories as needed, and doesn't error out if one exists. Additionally, /usr/project does not have to exist.

2. Ludemann, in addition to reader Christer Jansson, commented that spaces in directory names renders the duplicate directory tree completely useless.

Although I'm unable to salvage the duplicate directory command, for those find and xargs versions that support -0 (probably GNU versions only), you might try experimenting with:

find ... -print0 | xargs -0 ...

Using Ludemann's email example, suppose your current directory structure contains:
foo
bar
foo bar

find . -type f -print | xargs -n 1 incorrectly produces:
foo
bar
foo
bar

while find . -type f -print0 | xargs -0 -n 1 delivers the correct results:
foo
bar
foo bar

According to the 7.1 Red Hat Linux man page for xargs and find, the -0 doesn't use the null terminator for file names disabling the special meaning of white space.

3. Reader Peter Simpkin asks the question, "Does the use of xargs only operate after the find command has completed?

find. -type f -name "*.txt" -print | xargs rm

If not, I was under the impression that the above was a bad idea as it is modifying the current directory that find is working from, or at least this is what people have told me, and, thus the results of find are then undefined."

My response is "no". Any Unix command that supports command-line arguments is an xargs candidate. The results of the find command are as valid as the output of the ls command:
# remove files ending with .txt in current directory

ls *.txt|xargs rm

If a command such as this is valid:

chmod 444 1.txt 2.txt 3.txt

then:

find . \( -name 1.txt -o -name 2.txt -o -name 3.txt \) -print|xargs chmod 444

is valid.

In closing, If I had the opportunity to rewrite "Using the Xargs Command", it would look somewhat different.


看过这篇文章后的一个应用:

ls -1 *200603*.log | xargs -i tar -zcvf {}.tar.gz {}

按天压缩3月份的log文件。

Linux/BSD 和 Solaris 之差异数例

, ,

常用命令:

Linux/BSD: ps -aux
Solaris: ps -ef

Linux/BSD: df
Solaris: df -k

Linux/BSD: ping
Solaris: ping -s

Linux/BSD: shutdown
Solaris: shutdown [-y] [-i] [-g]

Linux/BSD: top
Solaris: prstat

Linux/BSD: tcpdump
Solaris: snoop

Linux/BSD: fdisk
Solaris: format

Solaris下,为了照顾Linux用户的习惯,将一些命令放在 /usr/ucb 之下,用这些命令,能得和Linux相同的结果。

目录结构:

Linux下默认有 /usr/local,Solaris则默认无,Solaris下相应的路径是 /opt

Linux的log文件一般放在 /var/log 下,Solaris则根据log的种类分别放在 /var/log/var/adm 之下。

Linux下,root用户的主目录是 /root ,Solaris的root用户的主目录则是 /

Linux的磁盘设备名在 /dev/ 之下,如 /dev/sda1,Solaris的磁盘设备名在 /dev/dsk/ 之下,如 /dev/md/dsk/d10

推荐一款Linux下的射击游戏:chromium B.S.U

,

在 Ubuntu 下查找射击游戏,输入如下命令:

# apt-cache search shoot game

结果列出一大片,一个一个安装并试玩,发现了好几个好玩的游戏。

其中,推荐这款名叫 chromium B.S.U 的小游戏。游戏的声光效果都很好,比起我们熟悉的“雷电”,甚至有过之而无不及。

Ubuntu下,安装很简单,只需执行:

# apt-get install chromium chromium-data


游戏的操作简单得不能再简单,一只手控制鼠标即可。点击鼠标左键射击,移动鼠标,自己的飞船也跟着移动。

该游戏的规则和一般的射击游戏有所不同。游戏的目标,就是击落所有的飞行物,放过一个飞行物,自己就掉一条命。因此,如果不能用炮弹击落飞行物,就只能用自己的飞船去撞飞行物,还好,不是撞一次就爆炸。

对射击游戏有兴趣的,不妨尝试一下这款游戏:chromium B.S.U


这款游戏也有 Windows 的版本,不过,不是最新版, Linux 下的才是最新的版本。

补充:刚刚发现一个加分数的秘诀,那就是适当放过能“吃”的东西,比如,放过一个加武器的加3000,放过一个加健康值的加10000。

Linux下常用 CVS 命令整理

,

这些天又要在命令行下使用CVS,找到很久以前转贴在WebSamba上的一篇文章,稍作整理,重新贴在这个Opera风水宝地。

1. 登录 -- login

$ export CVSROOT=:pserver:username@the_server_name:/home/cvsroot

Note:
pserver是访问方式,如果服务器设置的是口令认证,则是 pserver。
username是 CVS服务器的用户名,可以根据你的设置修改;
the_server_name是CVS服务器的名称或者IP地址;
/home/cvsroot是你的CVS服务器的CVSROOT目录,根据你的CVS服务器设置做修改或者询问管理员.

登陆CVS服务器:

$ cvs login

这时候cvs会问你口令,请把你在CVS服务器上的口令敲进去:
Passwd: xxxxxxxx


2. 提交项目 -- import

cvs import [-options] repository vendortag releasetag...

Note:
该命令将当前目录下的所有文件(包括子目录)导入源代码储存库。
repository :项目名称,在CVS服务器上会创建以这个名字命名的仓库。
vendortag : 项目分支的总标记。(不常用)
releasetag :标识文件的输入层次的标记。 (一般用start)
使用import提交项目的时候,CVS会要求对项目进行说明。在默认状态下,CVS会弹出文本编辑器。用户也可以用-m “log_message”来输入说明信息。

例如

$cvs import -m "upload the first time as new module" judecvs v_0_0_1 start

提示:import 一般在第一次导入module时使用。后期修改文件后可直接使用commit命令提交修改的文件。

3. 从CVS导出项目 -- checkout

cvs checkout [options] modules...

此命令将源代码储存库中已有的项目导出到当前目录。
modules :项目名称

例如、从仓库中检索出judecvs项目的源文件:

$ cvs checkout judecvs


4. CVS 主要命令 -- update

更新当前工作目录中的文件

cvs update [-options] [files...]

此命令比较指定CVS源码库中的文件和当前目录下的文件,如果CVS源码库中有更高版本的源文件,则更新当前目录下的文件。此命令只有在checkout命令使用过后才能使用。

在执行update命令时,CVS并不是简单的将新版本覆盖当前文件,而是试图将新版本所做的修改添加到当前文件中去。如果发生冲突,CVS会以字符串“<<<<<<”和“>>>>>>”来表示冲突发生。这时候你可以修改文件,重新提交。

提示:如果你已经做过一次checkout了,那么不需要重新checkout,只需要进入cvstest项目的目录,更新一把就行了:

例如:

$ cd judecvs
$ cvs update


5. CVS 主要命令 -- status

如果你不想直接更新,只是想看看有没有更新的东西,那么:

$ cvs status

会给每个文件有一份状态报告,类似这样:
==================================================
File: client.c Status: Up-to-date
Working revision: 1.1.1.1 'Some Date'
Repository revision: 1.2 /home2/cvsroot/judecvs/client.c,v

这里最重要的就是 Status 栏,这里总共可能有四种状态:
Up-to-date:表明你要到的文件是最新的.
Locally Modified:表明你曾经修改过该文件,但还没有提交,你的版本比仓库里的新.
Needing Patch:表明有人已经修改过该文件并且已经提交了!你的版本比仓库里的旧.
Needs Merge:表明你曾经修改国该文件,但是别人也修改了这个文件,而且还提交给仓库了!


6. CVS 主要命令 -- commit

保存修改到CVS中

cvs commit [-lnR] [-m 'log_message' | -f file] [-r revision] [files...]
此命令将当前目录下的源代码与CVS中最新版本比较,并进行更新。
[-m ‘log_message‘ ] :输入修改说明。
[-r revision] :指定版本。
[files...] :指定修改文件。

$ cvs commit -m "add XXX function" client.c

系统会提示
CVS: ----------------------------------------------------------------------
CVS: Enter Log. Lines beginning with `CVS:' are removed automatically
CVS:
CVS: Committing in .
CVS:
CVS: Modified Files:
CVS: client.c
CVS: ----------------------------------------------------------------------


退出后,系统询问是否continue,输入c,则完成checkin
Log message unchanged or not specified
a)bort, c)ontinue, e)dit, !)reuse this message unchanged for remaining dirs
Action: (continue) c
Checking in client.c;
/home2/cvsroot/judecvs/client.c,v <-- client.c
new revision: 1.2; previous revision: 1.1
done


如果CVS上文件已经有其他人更新,也就是我当前工作的不是最新版本,系统提示commit失败,这时候需要先update,然后把整合文件再commit.
cvs server: Up-to-date check failed for `client.c'
cvs [server aborted]: correct above errors first!
cvs commit: saving log message in /tmp/cvsCEjA9N


提示:修改文件之前先update或者先查看文件状态,确认当前工作版本是最新版本。

7. 添加文件到项目中 -- add

cvs add [-k kflag] [-m 'message'] files...

此命令并不真正添加文件,只是将文件注册到项目中,要真正添加文件,还要使用commit命令。

例如:

$ cvs add -m "test add" testadd.c

提示:
cvs server: scheduling file `testadd.c' for addition on branch `v_0_0_2'
cvs server: use 'cvs commit' to add this file permanently

$ cvs commit

同 commit 过程一样,CVS将testadd.c添加到项目中

8. CVS 主要命令 -- remove

从项目中删除文件

cvs remove [-k kflag] [-m 'message'] files...

和add命令一样,此命令并不真正删除文件,只是将文件从项目中取消,要真正删除文件,还要使用commit命令。

e.g.

$ rm testadd.c
$ cvs rm testadd.c


系统提示
cvs server: scheduling `testadd.c' for removal
cvs server: use 'cvs commit' to remove this file permanently

$ cvs commit testadd.c

此时,CVS才将testadd.c从项目的最新版本中删除,但是如果它有以前的版本,以前版本依然存在。

Unix/Linux目录权限: rwx 之外的 s

,

以前一直不太明白Linux的目录权限当中的 s,今天终于明白了。

例:
名为 files 的目录,所有者为 user1,user1的所在组为 group1,group1对files有写的权限
执行 ls -la 结果如下:
drwxrwxr-x 4 user1 group1 4096 Aug 31 19:17 files

现在想把 group1 改成 apache,于是执行:
chown -R user1:apache files

执行 ls -la 结果如下:
drwxrwxr-x 4 user1 apache 4096 Aug 31 19:17 files

这样有个问题,用user1登陆,在 files 下生成新文件test
ls -a files/test 的结果:
drwxrwxr-x 4 user1 group1 4096 Aug 31 19:17 test

新文件的组还是 group1。

现在想做的是,用户在files下生成新文件的时候,文件的所在组还是用apache。这时候,·s· 派上用场了。

执行以下命令:
chmod -R g+s files

用user1登陆,在 files 下生成新文件 test2
ls -a files/test2 的结果:
drwxrwxr-x 4 user1 apache 4096 Aug 31 19:17 test2

OK了,这就是想要的结果。

如果对目录下的文件设置默认用户id,只需
chmod -R u+s files

Vim图解键盘指令

, ,

这个图很有用,把它打印了一份,还得留一个连接在这儿:
http://plog.longwin.com.tw/blog_images/vi-vim-cheat-sheet.png

Installed Ubuntu 6.06 on my laptop

,

在新买的DELL笔记本上安装了Ubuntu 6.06,安装方法选择硬盘方式,用Grub for DOS引导,安装完成并经过字体美化之后,可以说,很满意。

安装过程,出乎意料,比Windows XP的安装还顺利,驱动程序基本上都自动找到。而Windows XP却要单独安装。需要DELL提供的Resource CD。

还有一点让我感到惊异,在Ubuntu下,USB设备的即插即用,一点不比Windows XP差。

越来越喜欢Linux了!在服务器方面,本来就喜欢Linux,Windows服务器在自己心目中早已退居二线。从Ubuntu和SuSE等发行版的表现看来,桌面上Linux也开始崭露头角。也许,Linux在桌面上撼动Windows的霸主地位的日子,也为时不远了。

January 2010
S M T W T F S
December 2009February 2010
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30