linux重命名文件shell脚本
时间: 2024-06-25 20:01:17 浏览: 6
在Linux中,你可以使用shell脚本来批量重命名文件。这里有一个基本的bash脚本示例,它会将当前目录下的所有.txt文件按数字递增的方式重命名:
```bash
#!/bin/bash
# 获取当前目录中的所有.txt文件
files="./*.txt"
for file in $files; do
# 从文件名中提取原始名称和扩展名
old_name=$(basename "$file" .txt)
# 如果是第一个文件,直接保留原名
if [ ! -e "$old_name.new.txt" ]; then
mv "$file" "$old_name.new.txt"
else
# 对于后续文件,添加递增值
counter=1
while [ -e "$old_name.$counter.new.txt" ]; do
((counter++))
done
mv "$file" "$old_name.$counter.new.txt"
fi
done
#
相关问题
linux shell脚本编程游戏
Linux Shell脚本编程游戏是一种通过编写Shell脚本来解决问题和完成任务的游戏。这种游戏可以帮助玩家学习Shell脚本编程语言,并提高他们的技能和知识。在游戏中,玩家需要使用Shell脚本来完成各种任务,例如创建文件、复制文件、移动文件、重命名文件等等。这些任务需要玩家使用Shell脚本编写正确的命令和语法,以便成功完成任务。通过这种方式,玩家可以学习到Shell脚本编程的基础知识和技能,提高他们的编程能力。
shell linux 常用脚本
1. 批量创建文件夹
```bash
#!/bin/bash
for i in {1..10}
do
mkdir "folder$i"
done
```
2. 批量重命名文件
```bash
#!/bin/bash
for file in *.txt
do
mv "$file" "new_$file"
done
```
3. 批量压缩文件
```bash
#!/bin/bash
tar -cvzf archive.tar.gz *.txt
```
4. 批量解压缩文件
```bash
#!/bin/bash
tar -xvzf archive.tar.gz
```
5. 批量转换文件编码
```bash
#!/bin/bash
for file in *.txt
do
iconv -f GBK -t UTF-8 "$file" > "new_$file"
done
```
6. 批量查找文件
```bash
#!/bin/bash
find . -name "*.txt"
```
7. 批量修改文件权限
```bash
#!/bin/bash
chmod 777 *.txt
```
8. 批量删除文件
```bash
#!/bin/bash
rm *.txt
```
9. 批量复制文件
```bash
#!/bin/bash
cp *.txt /path/to/destination
```
10. 批量移动文件
```bash
#!/bin/bash
mv *.txt /path/to/destination
```
以上是一些常用的 shell linux 脚本,可以根据需要修改参数和文件路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)