简体   繁体   English

遍历目录并删除文件

[英]looping through directories and remove files

I want to Loop through the homefolders and validate, if the path /home/*/.ssh/authorized_keys exists.如果路径 /home/*/.ssh/authorized_keys 存在,我想遍历主文件夹并验证。 If it exists, I want to remove the file .../.ssh/disabled_authorized_keys for the user, that has already the authorized_keys file in his homefolder.如果它存在,我想删除用户的文件 .../.ssh/disabled_authorized_keys ,该文件已经在他的主文件夹中包含了 authorized_keys 文件。

I have created this script so far, but it will remove all disabled_authorized_keys and not just for the users who have the authorized_keys.到目前为止,我已经创建了这个脚本,但它会删除所有 disabled_authorized_keys,而不仅仅是拥有authorized_keys 的用户。

 for i in /home/*/.ssh/authorized_keys;
 do ii=`echo $i|awk -F '/' '{print $3}'` ;
 rm /home/$ii/.ssh/disabled_authorized_keys;
 done

Thank you in advance.先感谢您。 (I'm an absolute beginner in bash scripting!) (我绝对是 bash 脚本的初学者!)

Try this:尝试这个:

for path in /home/*/.ssh/authorized_keys; do
    rm "$(dirname "$path")"/disabled_authorized_keys
done

It will grumble on stderr if disabled_authorized_keys does not exists, but will not "crash".如果disabled_authorized_keys不存在,它会在 stderr 上抱怨,但不会“崩溃”。

More about dirname .更多关于dirname

for dir in /home/*/.ssh; do
    [[ -e $dir/authorized_keys ]] && rm "$dir/disabled_authorized_keys"
done

[[ -e file ]] checks if file exists. [[ -e file ]]检查file存在。 Read && as "if-then".&&读作“if-then”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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