一、脚本要求 1.所有操作在/python下 2.批量创建12个以py后缀结尾的文件,文件名中必须包含_hcip,文件名除了_hcip固定字符串外,文件名还包含8个小写随机的字符。 3.创建的12个文件后缀名改为大写的
1.所有操作在/python下
2.批量创建12个以py后缀结尾的文件,文件名中必须包含_hcip,文件名除了_hcip固定字符串外,文件名还包含8个小写随机的字符。
3.创建的12个文件后缀名改为大写的PY,固定字符串改为_hcie
#!/bin/bash
##########################################################
#File Name:create_file.sh
#Version:V1.0
#Aurhor:
#Emali:
#Created Time:2021-06-15 02:56:59
#Description: create files & modify files
##########################################################
if [ -d /Python ];then
cd /python
else
mkdir /python
cd /python
fi
for i in {1..12}
do
file_name=$(echo $RANDOM |md5sum |cut -c 1-8)
touch ${file_name}_hcip.py
done
for files in $(ls /python)
do
file1=$(echo $files |cut -d '_' -f1)
mv $files ${file1}_hcie.PY
done
[root@control python]# /scripts/create_file.sh
[root@control python]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 15 04:26 4272db00_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 51989a58_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 75075364_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 7a272319_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 7d76af2f_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 977e73D6_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 9e4386a0_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 a35d5337_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 b9cb6458_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 b9d3349e_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 c3e589a2_hcie.PY
-rw-r--r-- 1 root root 0 Jun 15 04:26 e4f82be8_hcie.PY
用脚本创建10个文件,以jpeg作为后缀,代码如下:
#!/bin/sh
for ((i=0;i<=10;i++))
do
touch ${i}.jpeg
done
执行完上面的脚本后会看见在当前目录下生成10个文件,文件名的构成为name.suffix
为了批量更改当前目录下的文件,我们需要遍历当前目录,获得需要更改文件的name,然后与新的文件后缀进行拼接,形成新的文件名name.newsuffix。
代码如下:
#!/bin/sh
oldsuffix="jpeg"
newsuffix="jpg"
dir=$(eval pwd)
for file in $(ls $dir | grep .${oldsuffix})
do
name=$(ls ${file} | cut -d. -f1)
mv $file ${name}.${newsuffix}
done
echo "change jpeg to jpg successd!"
到此这篇关于shell脚本之文件批量创建与修改的文章就介绍到这了,更多相关Shell脚本文件批量创建修改内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
--结束END--
本文标题: Shell脚本之文件批量创建与修改的简单方法
本文链接: https://www.lsjlt.com/news/21341.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0