查找并批量编辑 git 个文件夹
find and mass edit git folders
我尝试那样做
me="$(whoami)";
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $me:1000 '{}' -R && cd '{}' & echo '{}' & echo $(pwd) & git config --global --add safe.directory $(pwd) & git config core.filemode false; " \;
我得到
user: grek
vendor/Mea/
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/MementoBundle
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/InvoiceBundle
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/AccountancyBundle
所以 $(pwd) - 总是在 /mnt/data1/PhpstormProjects/vista_bralion_docker
所以 git 命令不起作用,为什么?
更新 1:
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $me:1000 '{}' -R && cd '{}' & echo '{}' & echo $(pwd) & git config --global --add safe.directory $(pwd) & git config core.filemode false; " \;
和以前一样的情况
你需要转义$(pwd)
它和使用&&
组合命令。
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $:1000 '{}' -R && cd '{}' && echo '{}' && echo $(pwd) && git config --global --add safe.directory $(pwd) && git config core.filemode false; " \;
这应该有效。我已经在本地进行了这样的测试:
$ find . -maxdepth 1 -exec bash -c "cd '{}' && echo $(pwd)" \;
/home/bayou/tmp
/home/bayou/tmp/lala
/home/bayou/tmp/framebuffer-vncserver
用 -execdir
试试这个:
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -execdir bash -c "chmod 777 '{}' -R && chown $:1000 '{}' -R && cd '{}' && echo '{}' && echo $(pwd) && git config --global --add safe.directory $(pwd) && git config core.filemode false; " \;
来自 find
的文档:
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The
expression must be terminated by
a semicolon (“;”). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a
control operator. If the string
“{}” appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be
executed from the directory from
which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.
注意加粗的“实用程序将从执行查找的目录中执行”。如果要从下级目录执行,使用-execdir
.
我尝试那样做
me="$(whoami)";
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $me:1000 '{}' -R && cd '{}' & echo '{}' & echo $(pwd) & git config --global --add safe.directory $(pwd) & git config core.filemode false; " \;
我得到
user: grek
vendor/Mea/
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/MementoBundle
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/InvoiceBundle
/mnt/data1/PhpstormProjects/vista_bralion_docker
vendor/Mea/AccountancyBundle
所以 $(pwd) - 总是在 /mnt/data1/PhpstormProjects/vista_bralion_docker 所以 git 命令不起作用,为什么?
更新 1:
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $me:1000 '{}' -R && cd '{}' & echo '{}' & echo $(pwd) & git config --global --add safe.directory $(pwd) & git config core.filemode false; " \;
和以前一样的情况
你需要转义$(pwd)
它和使用&&
组合命令。
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -exec bash -c "chmod 777 '{}' -R && chown $:1000 '{}' -R && cd '{}' && echo '{}' && echo $(pwd) && git config --global --add safe.directory $(pwd) && git config core.filemode false; " \;
这应该有效。我已经在本地进行了这样的测试:
$ find . -maxdepth 1 -exec bash -c "cd '{}' && echo $(pwd)" \;
/home/bayou/tmp
/home/bayou/tmp/lala
/home/bayou/tmp/framebuffer-vncserver
用 -execdir
试试这个:
sudo find vendor/Mea/ -maxdepth 1 -type d \( ! -name . \) -execdir bash -c "chmod 777 '{}' -R && chown $:1000 '{}' -R && cd '{}' && echo '{}' && echo $(pwd) && git config --global --add safe.directory $(pwd) && git config core.filemode false; " \;
来自 find
的文档:
-exec utility [argument ...] ; True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (“;”). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string “{}” appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.
注意加粗的“实用程序将从执行查找的目录中执行”。如果要从下级目录执行,使用-execdir
.