for循环 shell批量修改文件名,创建文件

1.根据txt的文本 批量创建文件夹和创建文件

[root@localhost tmp]# cat name.txt 
Jacquelyn 杰奎琳
Leilani 莉兰妮
Fiona 菲奥娜
Kelly 凯利
Larissa 兰瑞莎
Karen 卡伦
Irene 艾琳
Caiden 凯登
....

#脚本编写
#创建文件夹

#!/bin/bash
for dir in `cat name.txt | awk '{print $1}'`
do
        mkdir /tmp/$dir
done
[root@localhost tmp]# sh -x mkdir.sh  #sh -x 可以查看脚本的执行过程
++ cat name.txt
++ awk '{print $1}'
+ for dir in '`cat name.txt | awk '\''{print $1}'\''`'
+ mkdir /tmp/Jacquelyn
+ for dir in '`cat name.txt | awk '\''{print $1}'\''`'
+ mkdir /tmp/Leilani
+ for dir in '`cat name.txt | awk '\''{print $1}'\''`'
+ mkdir /tmp/Fiona
.....

root@localhost tmp]# ll
总用量 8
drwxr-xr-x 2 root root   6 6月  18 15:21 Aidan
drwxr-xr-x 2 root root   6 6月  18 15:21 Albert
drwxr-xr-x 2 root root   6 6月  18 15:21 Alfredo
drwxr-xr-x 2 root root   6 6月  18 15:21 Caiden
drwxr-xr-x 2 root root   6 6月  18 15:21 Chelsea
drwxr-xr-x 2 root root   6 6月  18 15:21 Destiny
.....
#创建文件

#!/bin/bash
for file in `cat name.txt | awk '{print $2}'`
do
    touch /tmp/${file}-student.txt
done
[root@localhost tmp]# sh -x touch.sh 
++ cat name.txt
++ awk '{print $2}'
+ for file in '`cat name.txt | awk '\''{print $2}'\''`'
+ touch /tmp/杰奎琳-student.txt
+ for file in '`cat name.txt | awk '\''{print $2}'\''`'
+ touch /tmp/莉兰妮-student.txt
+ for file in '`cat name.txt | awk '\''{print $2}'\''`'
+ touch /tmp/菲奥娜-student.txt
+ for file in '`cat name.txt | awk '\''{print $2}'\''`'
+ touch /tmp/凯利-student.txt
+ for file in '`cat name.txt | awk '\''{print $2}'\''`'
+ touch /tmp/兰瑞莎-student.txt
.....

[root@localhost tmp]# ll
总用量 12
-rw-r--r-- 1 root root   0 6月  18 15:47 阿尔弗雷多-student.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾伯特-student.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾登-student.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾琳-student.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 黛丝缇妮-student.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 菲奥娜-student.txt

2.批量修改文件名

将上面所有的student修改为teacher

#脚本实现将所有的student替换为teacher
#!/bin/bash
for file in `ls *s*.txt`
do
	mv $file ${file/student/teacher}
done
[root@localhost tmp]# sh -x change-name.sh 
++ ls 阿尔弗雷多-student.txt 艾伯特-student.txt 艾登-student.txt 艾琳-student.txt 黛丝缇妮-student.txt 菲奥娜-student.txt 哈丽特-student.txt....
+ for file in '`ls *s*.txt`'
+ mv 阿尔弗雷多-student.txt 阿尔弗雷多-teacher.txt
+ for file in '`ls *s*.txt`'
+ mv 艾伯特-student.txt 艾伯特-teacher.txt
+ for file in '`ls *s*.txt`'
+ mv 艾登-student.txt 艾登-teacher.txt
+ for file in '`ls *s*.txt`'
+ mv 艾琳-student.txt 艾琳-teacher.txt
+ for file in '`ls *s*.txt`'
+ mv 黛丝缇妮-student.txt 黛丝缇妮-teacher.txt
.....

[root@localhost tmp]# ll
总用量 16
-rw-r--r-- 1 root root   0 6月  18 15:47 阿尔弗雷多-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾伯特-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾登-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 艾琳-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 黛丝缇妮-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 菲奥娜-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 哈丽特-teacher.txt
-rw-r--r-- 1 root root   0 6月  18 15:47 杰奎琳-teacher.txt
.....

修改文件后缀名

#修改后缀txt为docx
#!/bin/bash
for ed in `ls *.txt`
do
	mv ${ed} ${ed/.txt/.docx}
done
[root@localhost tmp]# sh -x houzui.sh 
++ ls name.txt 阿尔弗雷多-teacher.txt 艾伯特-teacher.txt 艾登-teacher.txt 艾琳-teacher.txt 黛丝缇妮-teacher.txt 菲奥娜-teacher.txt 哈丽特-teacher.txt.....
+ for ed in '`ls *.txt`'
+ mv name.txt name.docx
+ for ed in '`ls *.txt`'
+ mv 阿尔弗雷多-teacher.txt 阿尔弗雷多-teacher.docx
+ for ed in '`ls *.txt`'
+ mv 艾伯特-teacher.txt 艾伯特-teacher.docx
+ for ed in '`ls *.txt`'
+ mv 艾登-teacher.txt 艾登-teacher.docx
+ for ed in '`ls *.txt`'
+ mv 艾琳-teacher.txt 艾琳-teacher.docx
+ for ed in '`ls *.txt`'
+ mv 黛丝缇妮-teacher.txt 黛丝缇妮-teacher.docx
...

[root@localhost tmp]# ll
总用量 20
-rw-r--r-- 1 root root   0 6月  18 15:47 阿尔弗雷多-teacher.docx
-rw-r--r-- 1 root root   0 6月  18 15:47 艾伯特-teacher.docx
-rw-r--r-- 1 root root   0 6月  18 15:47 艾登-teacher.docx
-rw-r--r-- 1 root root   0 6月  18 15:47 艾琳-teacher.docx
-rw-r--r-- 1 root root   0 6月  18 15:47 黛丝缇妮-teacher.docx
...

#!/bin/bash
#([0-9]{1,3}\.){3}[0-9]{1,3}
filename=/tmp/sshtest/ssh-deny.txt
ip=$(cat secure.txt | grep Failed | awk '{print $11}' | sort | uniq -c | sort -n | awk '$1>2{print $2}')

for i in `echo $ip`
    do
        cat $filename | grep $i >/dev/null
        if [ $? -ne 0 ];
        then
        sed -i "/no/a $i is deny" $filename
        else
            echo "已经拉黑了!"
            exit
        fi
done
#!/bin/bash
#file=/var/log/secure
file=/tmp/666/secure
destfile=/etc/hosts.deny

#ipaddr=$(tail -100 ${file} | grep "Failed password for invalid" | awk '{print $13}' | sort | uniq -c | sort -n | awk '$1>20{print $2}')
ipaddr=$(cat ${file} | grep "^May" | grep "Failed password for invalid" | awk '{print $13}' | sort | uniq -c | sort -n | awk '$1>20{print $2}')

for i in `echo "${ipaddr}"`
do
	cat ${destfile} | grep ${i} >/dev/null
	if [[ $? -ne 0 ]]; 
	then
		echo "sshd:${i}:deny" >> ${destfile}
	else
		echo "已经拉黑了!"
	fi
done

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值