linux - 如何根据文件是否存在递归执行命令

标签 linux bash shell

我需要检查文件 new.htaccess 是否存在于多个目录中(递归)并执行:

rm .htaccess
rename new.htaccess .htaccess
rm .htpasswd

我认为 for 循环可能有效,但我不确定语法(我是 for 循环的菜鸟)。

我认为它应该看起来像这样,但正如我所说,我是一个菜鸟,并没有让它正常工作:

for -r new.htaccess in * do {
rm .htaccess
rename new.htaccess .htaccess
rm .htpasswd }

我在 Godaddy 4GH 共享主机上使用 Red hat linux 和 BASH v3.2.25。

感谢您的帮助!

最佳答案

有很多方法可以做到这一点,这是另一种简单的方法:

find . -name new.htaccess -execdir sh -c '
    mv new.htaccess .htaccess
    rm .htpasswd
  ' \;

关于linux - 如何根据文件是否存在递归执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25070680/

相关文章:

php - 如何在数千个代码页中查找字符串?

linux - Pathos:在 Linux 上强制生成

bash - bash 重定向中的意外行为

linux - 2 个字符串的所有排列和组合的列表

linux - umask 会影响消息队列吗?

linux - 仅将 docker 镜像绑定(bind)到特定的 MAC 地址

bash - 从列剪切到行尾

bash - 使用 sed 转义字符串变量中的符号

linux - 如何在 Linux shell 脚本中更改文件扩展名?

linux - 使用 shell 从文件中删除 {^D ctrl+d } 垃圾