顯示具有 find 標籤的文章。 顯示所有文章
顯示具有 find 標籤的文章。 顯示所有文章

2012年5月17日 星期四

find .. then cd to it(them)

找到含 .git 的目錄,cd 過去,run git status..

ref: http://www.linuxquestions.org/questions/programming-9/shell-script-to-cd-into-the-result-of-find-592503/

#/bin/sh find . -type d -name '.git' | while read F; do D=$F/../ cd "$D" echo $PWD git status cd - > /dev/null echo --------------- done 找到的 path 含 .git,所以要cd 到 .git 上一層。
做完後,回到剛剛的目錄: cd - 不要把 cd - 的 command 印(echo) 出來,所以加上 > /dev/null

2011年5月3日 星期二

find - exclude some folder

是用指定 "path 中不包含某些字串" 完成的:
find . -type f -not -path "./.repo*"
-- 不要找./.repo 下的
如果有一堆,就 一直 -not -path ..



ref: http://blog.tcmacdonald.com/content/exclude-directories-bash-find

也可以用 -prune find . -type d -name Documentation -prune -o -type f -name '*.c' -- 修剪掉所有 Documentation 目錄

多個可以用: find . -type d \( -name Documentation -o -name tools -o -name scripts \) -prune -o -type f -name '*.c' -- 修剪掉 Documentation, tools 和 scripts 三個目錄。
有關 -prune

用 man find 來看, -prune 放在 -path 後面,代表 -path 'XXX' 的這個path 會被 exclude。
--- 但是 -prune 後面好像一定要加 -o
而且要放在前面...

2011年4月29日 星期五

find files contains ***

找內含XXX的檔案

找檔案: (列出所有檔案)
find . -type f -print

交給 grep : 先交給 xargs 把一行一行轉成 argument,再交給 grep 找 XXX
find . -print | xargs grep XXX




所以變化... 找所有 包含 XXX 的 .c 檔:
在找檔時加上選項:
find . -type f -name '*.c' -print | xargs grep XXX

兩種檔案:.c, .c :
每個 -name option 前面加上 -o
find . -type f -name '*.c' -o -name '*.h' -print | xargs grep XXX




ref :

find any files contains xx then touch it

原因: android build system 的 Makefile 好像沒有把 header 列入 depenedency check。
所以當修改一個 .h 檔,就要去找出所有 include 他的 source,作 touch。

這個動作可以分成:
  1. 找出所有包含 aa.h 的檔案
  2. 取出檔名
  3. touch 這些檔案

實例:

touch 所有 include system_properties.h 的 c source code.

找出所有含 system_properties.h 的 c 檔
find .-type f -name '*\.c' | xargs grep 'system_properties\.h
這樣會列出:
./system/core/init/init.c:#include <sys/system_properties.h>
./system/core/init/parser.c:#include <sys/_system_properties.h>
./system/core/init/property_service.c:#include <sys/_system_properties.h>
./build/tools/check_prereq/check_prereq.c:#include <sys/system_properties.h>

grep 的輸出包含 檔名跟 match location。
取出檔名:用 cut 把 ':' 後面都刪除
find . -type f -name '*\.c' | xargs grep 'system_properties\.h' | xargs cut -f1 -d ':'

結果就是:
./system/core/toolbox/watchprops.c
./system/core/toolbox/getprop.c
./system/core/libcutils/properties.c
./system/core/init/init.c
./system/core/init/parser.c
./system/core/init/property_service.c
./build/tools/check_prereq/check_prereq.c

這樣就可以餵給 touch了:
... 最後加上 | xargs touch