我正在编写一个shell脚本,它可以重命名文件夹中的所有文件,
我们需要每 30 秒搜索一次特定模式的文件。
需要选择以下格式的文件,
core.3467
core.1234
core.acde
并且它们需要更新如下,
c.o.r.e.3467
c.o.r.e.1234
c.o.r.e.acde
一旦更新,必须发送一封警报电子邮件,表明文件 core.3467 已更改为 core3467
这是我到目前为止所写的,但重命名命令似乎不起作用,
#!/bin/bash
#go the designated directory
cd "<dir_name>"
mail="[email protected]"
#writing all the files in the specified format
ls core* > current.txt
a='cat current.txt'
#renaming the file
rename "s/core/c.o.r.e."*
#writing updated file names
ls c.o.r.e* > updated.txt
b='cat updated.txt'
#sending alert email
mail -s "Files $a changed to $b" $mail
答案1
#!/bin/bash
[email protected]
for file in $(ls /my/directory/name/core*)
do
newname=$(echo ${file}|sed -e "1,1s/core/c.o.r.e/")
mv ${file} ${newname}
echo "File Renamed..."|mail -s "File ${file} renamed to ${newname} ${email_address}
done
这应该做...
答案2
感谢 MelBuslan 出色的 scriptlet,让我对其进行调整以解决一封电子邮件的请求:
#!/bin/bash
[email protected]
if test -n "$(find /my/directory/name -maxdepth 1 -name 'core*' -print -quit)" ; then
for file in $(ls /my/directory/name/core*)
do
newname=$(echo ${file}|sed -e "1,1s/core/c.o.r.e/")
mv ${file} ${newname}
echo "File ${file} renamed to ${newname}" >> updated.txt
done
cat updated.txt | mail -s "Files renamed" ${email_address}
rm updated.txt
fi