简体   繁体   中英

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.

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. 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

 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:

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):

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:

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. You have only provided the semi-colon that ends the chmod command.

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函数使其更具可读性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM