[英]bash script to search server for specific /users/ and then delete
我是bash腳本的新手,並且了解一些基礎知識。 我需要幫助來創建一個可以運行的bash腳本,它將在一個服務器(或多個服務器)中搜索特定/home/users
的列表,然后如果從列表中找到/user
,則使用sudo rm -rf /user
刪除該用戶的目錄sudo rm -rf /user
。 如果找不到列出的用戶,則不執行任何操作。 該腳本應適合在可能有或沒有任何用戶的多台服務器上運行。 任何幫助,將不勝感激。
我發布了我一直在想的東西。 我將從創建一個名為userList的文件開始,該文件包含我要刪除的所有主目錄,每行一個:/ Users / user1 / Users / user30
#!/usr/bin/bash
do
if [[ -d $dir ]]
then
rm -R $dir
echo “Directory $dir found and deleted.”
else
echo “Directory $dir not found.”
fi
done < userList
您接近:
#!/bin/bash
while read dir; do # <-- there's a change here
if [[ -d $dir ]]
then
sudo rm -rf $dir # <-- and here
echo “Directory $dir found and deleted.”
else
echo “Directory $dir not found.”
fi
done < userList
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.