`

linux 101 hacks 阅读笔记

阅读更多
20101009 日 晚上 7点 开始阅读linux 101 hacks 这本书 作笔记如下,与各位菜鸟共勉。

linux 下的find 命令 小总结..谢谢大家赏光!


通用格式:find pathname -options [-print -exec -ok]
例子:
find / -name filename 再根目录里面搜索文件名为filename的文件
find /etc -name *s*在目录里面搜索带有s的文件
find /etc -name *S 在目录里面搜索以s结尾的文件
find /etc -name s*在目录里面搜索以s开头的文件
find / -amin -10在系统中搜索最后10分钟访问的文件
find / -atime -2查找在系统中最后48小时访问的文件
find / -empty 查找在系统中为空的文件或者是文件夹
find / -group groupname 查找在系统中属于groupname的文件
find / -mmin -5查找在系统中最后5分钟修改过的文件
find / -mtime -1查找在系统中最后24小时修改过的文件
find /-nouser查找在系统中属于费用户的文件
find / -user username 查找在系统中属于username的文件
find / -ctime -1查找在系统中最后24小时被改变状态的文件
find / -fstype type查找在系统中文件类型为?的文件
find / -user user1name -or -user user2name查找在系统中属于user1name或着属于user2name的文件
find / -user user1name -and -user2name在系统中查找既属于user1name又属于user2name用户的文件.

一、 find 命令格式


1、find命令的一般形式为;

find pathname -options [-print -exec -ok ...]


2、find命令的参数;

pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。


3、 find命令选项

-name
按照文件名查找文件。
-perm
按照文件权限来查找文件。
-prune
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
-user
按照文件属主来查找文件。
-group
按照文件所属的组来查找文件。
-mtime -n +n
按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
-nogroup
查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2
查找更改时间比文件 file1新但比文件file2旧的文件。
-type
查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

另外,下面三个的区别:
  -amin n
  查找系统中最后N分钟访问的文件
  -atime n
  查找系统中最后n*24小时访问的文件
  -cmin n
  查找系统中最后N分钟被改变文件状态的文件
  -ctime n
  查找系统中最后n*24小时被改变文件状态的文件
   -mmin n
  查找系统中最后N分钟被改变文件数据的文件
  -mtime n
  查找系统中最后n*24小时被改变文件数据的文件


4、使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

# find . -type f -exec ls -l { } \;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

$ find logs -type f -mtime +5 -exec rm { } \;

记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。

$ find . -name "*.conf" -mtime +5 -ok rm { } \;
< rm ... ./conf/httpd.conf > ? n

按y键删除文件,按n键不删除。

任何形式的命令都可以在 -exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

# find /etc -name "passwd*" -exec grep "sam" { } \;
sam:x:501:501::/usr/sam:/bin/bash 




2010年10月24日 linux学习记录

      grep 'pattern' file :在文件里搜索字符串或和正则表达式匹配的字符串
      file somefile :取得文件somefile的文件类型
      uniq :只输出文件中不一样的行


20101031 学习笔记
Linux统计文件中单词出现的次数
文件名称:list.查找单词名称:test.操作命令:(1)more list | grep -o test | wc -l.(2)cat list | grep -o test | wc -l.(3) grep -o test list | wc -l

例子:more filename | grep -0 【word】 | wc -l

linux下统计文件数目

Tuesday, 14. November 2006, 09:11:47

linux
第一种方法:
ls -l|grep "^-"|wc -l
ls -l 长列表输出该目录下文件信息(注意这里的文件,不同于一般的文件,可能是目录、链接、设备文件等)。如果ls -lR|grep "^-"|wc-l则可以连子目录下的文件一起统计。
grep ^- 这里将长列表输出信息过滤一部分,只保留一般文件,如果只保留目录就是 ^d
wc -l 统计输出信息的行数,因为已经过滤得只剩一般文件了,所以统计结果就是一般文件信息的行数,又由于一行信息对应一个文件,所以也就是文件的个数。

第二种方法:

find ./ -type f|wc -l 由于默认find会去子目录查找,如果只想查找当前目录的文件用find ./ -maxdepth 1 -type f|wc -l即可。

需要说明的是第二种方法会比第一种方法快很多,尤其是也统计子目录时。




2010-12-12 Linux下的Memcache安装
     最近在研究怎么让Discuz!去应用Memcache去做一些事情,记录下Memcache安装的过程。

Linux下Memcache服务器端的安装
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 。
下载:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.3。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.3.tar.gz

用wget指令直接下载这两个东西.下载回源文件后。
1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr;然后make;然后make install;
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:

1.分别把memcached和libevent下载回来,放到 /tmp 目录下:
# cd /tmp
# wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz

2.先安装libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install

3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还不错,都安装上了。

4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,

5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug

安装Memcache的PHP扩展
1.在http://pecl.php.net/package/memcache 选择相应想要下载的memcache版本。
2.安装PHP的memcache扩展

tar vxzf memcache-2.2.1.tgz
cd memcache-2.2.1
/usr/local/php/bin/phpize
./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config –with-zlib-dir
make
make install

3.上述安装完后会有类似这样的提示:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/

4.把php.ini中的extension_dir = “./”修改为

extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”

5.添加一行来载入memcache扩展:extension=memcache.so

memcached的基本设置:
1.启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

# kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。

3.重启apache,service httpd restart

Memcache环境测试:
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始领略Memcache的魅力把!
< ?php
$mem = new Memcache;
$mem->connect(”127.0.0.1″, 11211);
$mem->set(’key’, ‘This is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>


2012-10-09 linux 学习
tracert -d




  • 大小: 74 KB
分享到:
评论

相关推荐

    Linux 101 Hacks(中文版).pdf 高清下载

    Linux 101 Hacks(中文版).pdf 高清下载

    Linux 101 Hacks 中英版

    Linux 101 Hacks中英版 If you are a developer, or system administrator, or database administrator, or IT manager, or just someone who spends a significant amount of time on UNIX / Linux, you should ...

    Linux101 Hacks 2rd

    Linux101 Hacks 2rd Linux101 Hacks 2rd Linux101 Hacks 2rd

    linux-101-hacks

    linux-101-hacks,学习 linux 的一些 tips,一些小技巧

    linux-101-hacks.pdf

    linux-101-hacks.pdf

    Linux 101 Hacks英语版手册

    Linux 101 Hacks学习手册,英语版,真心不错,可以提高你的外语能力

    Linux 101 Hacks

    《Linux 101 Hacks》是哈工大 IBM 技术俱乐部 08 级新生暑假培训中学习 Linux 基本系统管理知识的参考文献,该书中的很多技巧对于初学者提高系统管理的工作效率很有帮助。考虑到很多初学者因为个人程度或者习惯的...

    Linux 101 hacks (2nd)

    Geekstuff唯一一本在官网免费下载的101 hacks系列,可以说是为其他的101 hacks的经典之一。本书中的很多技巧对于初学者提高系统管理的工作效率很有帮助。光看看目录,就感觉非常实用! 此为第二版。 -------- 资源...

    Linux 101 Hacks (英文原版)

    There are total of 101 hacks in this book that will help you build a strong foundation in Linux. All the hacks in this book are explained with appropriate Linux command examples that are easy to ...

    Linux Server Hacks

    Linux_Server_Hacks_Volume_Two_Tips_Tools_for_Connecting_Monitoring_and_Troubleshooting

    Vim 101 hacks

    此电子书为vim 101 hacks,不是vim hacks也不是linux 101 hacks,千辛万苦才找到的。PS,本书为英文版的。

    Vim-101-hacks、Sed-and-Awk-101-Hacks、Linux-101-hacks 英文版(高清)PDF

    Vim-101-hacks、Sed-and-Awk-101-Hacks、Linux-101-hacks 英文版(高清)PDF

    linuxserverhacks2-13.pdf

    linuxserverhacks2-13.pdf

    Linux_101_Hacks_CN.pdf

    常用Linux命令详解cd、日期操作、SSH、归档、系统管理任务、Apachectl和Httpd实例、Bash脚本、系统监控和性能以及一些技巧,该书中的很多技巧对于初学者提高系统管理的工作效率很有帮助。

    Linux_101_Hacks_CN

    《Linux 101 Hacks》是一本关于 Linux 使用技巧的免费电子书籍。 其中最为实用的,大概 4(基本命令)、7(归档打包)、9(系统配置)、10(Apache服务配置)和12(系统控制)几章。虽说都是一些命令的使用,但是要...

    Linux_101_Hacks中文译本.

    学习Linux Ubuntu入门的好资源 好好利用 完全免费

    Linux.101.Hacks

    Linux.101.Hacks linux linux hacks

Global site tag (gtag.js) - Google Analytics