简体   繁体   English

将find命令作为别名

[英]Putting a find command as an alias

I'm trying to create aliases for the following commands which recursively convert all file permissions in the current directory to 644, and another one which will convert all directories to 755. 我正在尝试为以下命令创建别名,这些命令将当前目录中的所有文件权限递归转换为644,另一命令将所有目录转换为755。

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

However, when I run this I get: 但是,当我运行它时,我得到:

find: paths must precede expression

These find commands work fine in the shell by themselves. 这些find命令本身在shell中可以正常工作。 Is there something special you have to do in order to run commands as aliases? 为了将命令作为别名运行,您需要做些特别的事情吗?

Thank you! 谢谢!

You need a few more semi colons to separate the actual find commands (as opposed to terminating them), ie 您还需要一些半冒号来分隔实际的查找命令(而不是终止它们),即

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

You can bullet proof your alias to only search your presstheme dir if it exists (allowing for the case of moving your aliases to other machines) by making each command after cd conditionally execute, ie 您可以通过在cd有条件地执行每个命令后,用别名来证明您的别名仅在您的presstheme目录存在时进行搜索(允许将别名移至其他计算机),例如

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

I hope this helps. 我希望这有帮助。

You need extra semi-colons to separate the two find commands from their surroundings: 您需要额外的分号来将两个find命令与其周围的环境分开:

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

You could benefit from using a sub-shell; 您可以从使用子外壳中受益。 then you don't need the final cd (which takes you home, not back to where you came from): 那么您就不需要最终的cd (将您带回家,而不是回到您的来源):

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

And, since I started using shells before there were aliases, I'd make this into a legible script in my bin directory: 而且,由于我是在没有别名之前开始使用shell的,所以我将其放入bin目录中的清晰脚本中:

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

and probably I'd parameterize it, too: 可能我也将其参数化:

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

Then I could specify a different directory if I wanted to, but it would default to the 'normal' one. 然后,我可以根据需要指定其他目录,但默认为“普通”目录。

You're missing the semi-colon that ends the first find command. 您缺少以第一个find命令结尾的分号。 You have only provided the semi-colon that ends the chmod command. 您仅提供了以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