簡體   English   中英

將find命令作為別名

[英]Putting a find command as an alias

我正在嘗試為以下命令創建別名,這些命令將當前目錄中的所有文件權限遞歸轉換為644,另一命令將所有目錄轉換為755。

alias fixpermissions='cd ~/public_html/wp-content/themes/presstheme; find . -type f -exec chmod 644 {} \; find . -type d -exec chmod 755 {} \; cd'

但是,當我運行它時,我得到:

find: paths must precede expression

這些find命令本身在shell中可以正常工作。 為了將命令作為別名運行,您需要做些特別的事情嗎?

謝謝!

您還需要一些半冒號來分隔實際的查找命令(而不是終止它們),即

 alias fixpermissions='cd ~/public_html/wp-content/themes/presstheme; find . -type f -exec chmod 644 {} \; ; find . -type d -exec chmod 755 {} \; ; cd'

您可以通過在cd有條件地執行每個命令后,用別名來證明您的別名僅在您的presstheme目錄存在時進行搜索(允許將別名移至其他計算機),例如

 alias fixpermissions='cd ~/public_html/wp-content/themes/presstheme && find . -type f -exec chmod 644 {} \; && find . -type d -exec chmod 755 {} \; && cd'

我希望這有幫助。

您需要額外的分號來將兩個find命令與其周圍的環境分開:

alias fixpermissions='cd ~/public_html/wp-content/themes/presstheme; find . -type f -exec chmod 644 {} \; ; find . -type d -exec chmod 755 {} \; ; cd'

您可以從使用子外殼中受益。 那么您就不需要最終的cd (將您帶回家,而不是回到您的來源):

alias fixpermissions='( cd ~/public_html/wp-content/themes/presstheme; find . -type f -exec chmod 644 {} \; ; find . -type d -exec chmod 755 {} \; )'

而且,由於我是在沒有別名之前開始使用shell的,所以我將其放入bin目錄中的清晰腳本中:

cd ~/public_html/wp-content/themes/presstheme
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

可能我也將其參數化:

cd ${1:-"~/public_html/wp-content/themes/presstheme"}
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

然后,我可以根據需要指定其他目錄,但默認為“普通”目錄。

您缺少以第一個find命令結尾的分號。 您僅提供了以chmod命令結尾的分號。

alias fixpermissions='find ~/public_html/wp-content/themes/presstheme -type f -exec chmod 644 {} \; ; find ~/public_html/wp-content/themes/presstheme -type d -exec chmod 755 {} \; cd'

我認為也許您應該使用Bash函數使其更具可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM