![](/img/trans.png)
[英]how to create a linux shell alias to give to g++ compiler the path of the file to compile?
[英]Linux shell: How to give parameter to alias of find command?
例如,我希望查找所有名為“ Makefile”的文件的當前目錄(不包括./bin),作為參數和grep“ library”,如下所示:
find . ! -path "./build" -name "*Makefile"|xargs grep library
我不必每次都鍵入所有這些內容,我只想使用2個參數制作一個“別名”,例如:
myfind "*Makefile" "library"
myfind的find參數是“ find”命令中的“ name”,第二個參數是“ grep”命令,我也希望星號“ *”可以作為參數的一部分傳遞,而不進行解析。
如何寫這樣的別名?
使用功能:
myfind() { find . ! -path "./build" -name "$1"|xargs grep "$2"; }
您不能使用別名來做到這一點。 您可以編寫一個小腳本,例如
#!/bin/bash
find . ! -path "./build" -name "$1"|xargs grep $2
將其另存為myfind
,位於路徑上的某個位置,並確保刪除別名。
您使用了標記linux,所以我假設您正在使用GNU grep。 GNU grep(至少是grep (GNU grep) 2.25
)支持以下選項,這使您的工作非常輕松。 讓我從grep --help
引用:
-r, --recursive like --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
因此,您的任務可以通過以下方式完成:
grep -r --include=Makefile --exclude-dir=build library
當然,您可以創建一個shell腳本或函數來進一步簡化它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.