1 grep 实例:
grep -参数 "匹配目的内容" 要匹配文件目标
举例
1.1 grep 查找某个文件夹下所有文件中的字符
用grep 命令查找一个文件夹下,所有的编译选项含-diretfb的文件,比如makefile
grep -r "-diretfb" ./
fgrep -r directfb ./ > yourmake.log
1.2 grep 查找某个文件内的字符
从文件内容查找匹配指定字符串的行:
$ grep "被查找的字符串" 文件名
从文件内容查找与正则表达式匹配的行:
$ grep –e “正则表达式” 文件名
查找时不区分大小写:
$ grep –i "被查找的字符串" 文件名
查找匹配的行数:
$ grep -c "被查找的字符串" 文件名
从文件内容查找不匹配指定字符串的行:
$ grep –v "被查找的字符串" 文件名
1.3 grep 和 find 结合使用,可以查找特定文件中含特定字符的文件并打印报错,这个功能比windows的强大的多
1.3.1 从根目录开始查找所有的文件中含有的某个字符串的文件 和find连用
从根目录开始查找所有扩展名为.xml的文本文件,找到的结果用xargs分解,然后从找到的文件中找出包含”username”的行
find / -type f -name "*.xml" | xargs grep "username"
结果往往很大,直接存档到文件再查看 > find_usename_log.txt 加到上面的命令后面
the following is the often used comand that used to search a project C files for a demand key words "play_open",and all the result include the error result would list in the txt file
find ./ -type f -name "*.c" | xargs grep "player_open" > find_player_open.txt 2>&1
find ./ -type f -name "*.*" | xargs grep "GOODMAN" > find_GOODMAN.txt 2>&1
为了便于理解,有关于xargs的描述: 如下:http://blog.csdn.net/zhangfn2011/article/details/6776925
1.3.2 查找项目中所有的编译后产生的config.log中各种Lib的编译工具gcc的版本是否一致:
find . -type f -name "*config.log" | xargs grep "gcc version"
2 擦写
一个文件,往往用于嵌入式中擦写flash设备
1. 产生一个全0xFF, 0xA5, 0xAA 的文件
tr ‘\000‘ ‘\377‘ < /dev/zero| dd f=file_0xFF.bin bs=2k count=1 // 用到的比较多
tr ‘\000‘ ‘\245‘ < /dev/zero| dd f=file_0xA5.bin bs=2k count=1
tr ‘\000‘ ‘\252‘ < /dev/zero| dd f=file_0xAA.bin bs=2k count=1
3. Find 的组合操作举例
3.0 find ./ -type d -name ‘.svn*‘ | xargs rm -rf
找到并删除 名字里面带 svn的folder...
命令对找到文件执行多个操作
find -name abc.txt -exec touch {} \; -exec ls -l {} \; -exec cat {} \;
3.1 Find 命令找两种以上的文件
find . \( -name "makefile" -o -name "*.patch" -o -name "*.txt" \)
3.2 find 和grep连用
hsy75:查找根目录或者当前文件夹下,文件后缀为filetype的文件名为filename的文件
find / | grep "filename\.filetype$"
举例:
要查找根目录下directfb.a的动态库
find / | grep "directfb\.a$"
得到结果:
apps/directfb/DirectFB-1.4.3/src/.libs/libdirectfb.a
apps/usr/lib/libdirectfb.a
/opt/lib/libdirectfb.a
/debug/lib/libdirectfb.a
/usr/lib/libdirectfb.a
3.3 一些从man page里面的复杂的find 的例子,介绍的非常详细
------------------------------------------------
#从根目录查找当天变化的文件
find $HOME -mtime 0
Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since
each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a
modification in the past which is less than 24 hours ago.
------------------------------------------------
cd /source-dir
find . -name .snapshot -prune -o \( \! -name *~ -print0 \)|
cpio -pmd0 /dest-dir
This command copies the contents of /source-dir to /dest-dir, but omits files and directories named .snapshot (and anything in them). It also
omits files or directories whose name ends in ~, but not their contents. The construct -prune -o \( ... -print0 \) is quite common. The idea here
is that the expression before -prune matches things which are to be pruned. However, the -prune action itself returns true, so the following -o
ensures that the right hand side is evaluated only for those directories which didn‘t get pruned (the contents of the pruned directories are not
even visited, so their contents are irrelevant). The expression on the right hand side of the -o is in parentheses only for clarity. It empha‐
sises that the -print0 action takes place only for things that didn‘t have -prune applied to them. Because the default `and‘ condition between
tests binds more tightly than -o, this is the default anyway, but the parentheses help to show what is going on.
----------------------------------------------------
find repo/ -exec test -d {}/.svn -o -d {}/.git -o -d {}/CVS ; \
-print -prune
Given the following directory of projects and their associated SCM administrative directories, perform. an efficient search for the projects‘ roots:
repo/project1/CVS
repo/gnu/project2/.svn
repo/gnu/project3/.svn
repo/gnu/project3/src/.svn
repo/project4/.git
In this example, -prune prevents unnecessary descent into directories that have already been discovered (for example we do not search project3/src
because we already found project3/.svn), but ensures sibling directories (project2 and project3) are found.
4. echo 代替键盘输入
echo -e “\n\n\n” 三个回车
从 AC_MGMT 中摘录的echo 代替键盘输入的 片段
18 useradd $AC_ID -g $GRP_ID -m && \
19 echo -e "$AC_ID\n$AC_ID\n"|passwd $AC_ID && \
20 echo "Create Successfully."
21 echo "Enabling Samba For $AC_ID..."
22 echo -e "$AC_ID\n$AC_ID\n$AC_ID\n"|smbpasswd -s -a $AC_ID && \
23 smbpasswd -e $AC_ID
24 echo "Samba Account for $AC_ID Done."
hsy75案:echo -e 是shell command 处理经常用到的方法,用来自动输入用户的输入,上文,根据用户输入变量AC_ID自动创立了一个samba的用户
当然,变量AC_ID应该要用户自己输入的
5. 利用变量内嵌执行
找到所有ko并copy
cp $(find *.ko) to_dir
6. 暂停运行程序和恢复
CTRL-Z 暂停运行程序
jobs 查看当前任务列表
fg num 前台运行任务列表中 num 任务
bg num 后台运行任务列表中 num 任务
7. 无终端运行程序
a. nohup <command>
b. 利用括号() 使进程成为 init(pid=1) 的子进程
(ping 192.168.0.1 &)
可用 ps -ef 看到 进程PID 和父进程PPID
8:
将tar 和find 结合,选定目录下指定的文件类型进行打包解压:
tar命令用语对文件进行归档以及恢复归档文件,
"tar xzvf"命令用于释放<恢复>".tar.gz"格式压缩的归档文件;
"tar xvf"命令用于释放<恢复>".tar"格式压缩的归档文件;
"tar xjvf"命令用于释放<恢复>".tar.b2z"格式压缩的归档文件;
tar xzvf +软件包名称 |find . -type f -name "*.cpp "
打包 : tar cvf + 打包后文件名 + 需打包文件夹名
tar -cvf target_file.tar target_folder/
或者有选择的压缩
tar cvjf file-cpp.tar.bz2 | find . -type f -name "*.cpp"
或者
find . -type f -name "*.cpp" | xargs tar zcvpf
backup.tar.gz
9 给文件夹打补丁:
1 进入你需要补丁的文件夹:
[email protected]:~/directfb_4.1/DirectFB-1.4.3$
2 执行补丁程序:
patch -p1 < ../DirectFB-1.4.3.patch
10 chmod
要求就是:
1、将当前目录中的所有“子目录”的权限设置为755;
2、将当前目录中的所有“文件”的权限设置为644。
解决方法:
chmod 644 -R *
chmod 755 `find -type d`
也可以用:
用find彻底些
find /path -type f -exec chmod 644 {} /;
find /path -type d -exec chmod 755 {} /;
将*.jpg文件名中的09v9改为0919
[email protected]:~/public_web_sSmO9OUVY1/files/image$ rename ’s/09v9/0919/’ *.jpg
ref:
1
http://ss64.com/bash/tar.html
2
http://topic.csdn.net/u/20071112/18/5630fa3e-d0cf-43ed-bc88-fb048ef1e482.html
3
http://zh.wikipedia.org/wiki/Xargs
ref:
http://www.eetop.cn/blog/?uid-51552-action-viewspace-itemid-26822
ref:
http://www.linuxso.com/command/
2012-12-21 add a find example for list vary result in search a key words in a project files
2013-04-10 add more good command
2014-12-24 porting from eetop
By Franklin :