今天在跑腳本的時候看到如下報錯:
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
然後就上網查了一下,結果搜索到一篇,大概是這樣說的:多文件的查找的時候需要增加單引號
而我之前一直都是用的雙引號,也沒有遇到問題,這次卻報錯了~
ssh $ip "cd $path;find . -name'access.$month*.log' -print -exec zip -r access.$month.zip
{} \;"
修改成單引號後,木有再見報錯~
例子說明:
# 進入tmp目錄新建4個文本文件
# cd /tmp
# touch {1,2,3,4}.txt
# find . -name *.txt
find: paths must precede expression: 2.txt
出現這個提示是因爲星號被展開爲當前目錄下所有的文件,這樣的匹配當然會出錯。看這個就知道了:
# echo *
1.txt 2.txt 3.txt 4.txt
# echo '*'
*
# echo \*
*
想要星號不被展開就需要加上括號或者反斜槓轉義,知道了這些我們就知道該怎麼find了
# find . -name '*.txt'
find . -name '*.txt'
./4.txt
./2.txt
./3.txt
./1.txt
#或者使用反斜槓
find . -name \*.txt
./4.txt
./2.txt
./3.txt
./1.txt