繁体   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