您的位置:首页 > 运维架构 > Shell

常用shell命令操作

2017-08-23 10:58 567 查看
1.判断2个目录同时存在再执行shell命令

#!/bin/bash
TestPath="/volume01/edit_high/"
TestPath1="/volume01_bak/edit_high/"
if [ -d "$TestPath" ]&&[ -d "$TestPath1" ]; then
echo "Ok";
rsync -a --delete /volume01/* /volume01_bak/
fi

2.在多个目录中查找超过7天的文件

#!/bin/bash
delfile()
{
find $i  -type f -mtime +7 -print 
}
Dirlist="/VMS_transfer/cztvcloud/mixiu_new/complete /VMS_transfer/cztvcloud/shixian_new/complete /VMS_transfer/cztvcloud/channel_new
/complete"
for i in $Dirlist;do
        [ ! -z  $i ] && delfile;
Done
3.从文本中读取IP地址,并修改密码,前提服务器免密码登陆。

 

 cat iplist
192.168.122.3
192.168.122.4
192.168.122.5

# cat chpass.sh 
#!/usr/bin/env bash
while read line
do
     echo "------------- $line --------------"
     ssh $line -n "echo '123456' | passwd --stdin root"
     if [ $? -eq 0 ];then
         echo "$line sucess."
     else
         echo "$line failed."
     fi
done < iplist

另外方法:for i in `cat user.txt`; do useradd $i; done 
 

4.除/home/dmtsai目录外,/home和/etc目录的所有东西都打包? 

tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc 

tar -cvf /root/backup/`date+%y%m%d`_etc /etc   带时间的备份文件

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: