linux系统中批量对一类文件重命名

1、rename命令

[root@centos79 test]# ls
a.mp4  dup1.mp4  dup2.mp4  dup4.mp4
[root@centos79 test]# rename mp4 txt *.mp4
[root@centos79 test]# ls
a.txt  dup1.txt  dup2.txt  dup4.txt

 

2、利用循环语句

[root@centos79 test]# ls
a.txt  dup1.txt  dup2.txt  dup4.txt
[root@centos79 test]# n=1; for file in *.txt; do num=$(printf "%02d" $n); mv $file new$num.mp3; let n=n+1;done
[root@centos79 test]# ls
new01.mp3  new02.mp3  new03.mp3  new04.mp3

 

3、实现按照文件大小递增命名

复制代码
[root@centos79 test]# ls
a.txt  b.txt  c.txt  d.txt
[root@centos79 test]# ll -h
total 170M
-rw-r--r--. 1 root root 50M Jul  6 19:21 a.txt
-rw-r--r--. 1 root root 10M Jul  6 19:21 b.txt
-rw-r--r--. 1 root root 80M Jul  6 19:21 c.txt
-rw-r--r--. 1 root root 30M Jul  6 19:21 d.txt
[root@centos79 test]# a=$(ls -S | xargs du -sh | tac | cut -f 2)
[root@centos79 test]# echo $a
b.txt d.txt a.txt c.txt
[root@centos79 test]# n=1; for file in $a; do num=$(printf "%02d" $n); mv $file $num.txt; let n=n+1; done
[root@centos79 test]# ls
01.txt  02.txt  03.txt  04.txt
[root@centos79 test]# ll -h
total 170M
-rw-r--r--. 1 root root 10M Jul  6 19:21 01.txt
-rw-r--r--. 1 root root 30M Jul  6 19:21 02.txt
-rw-r--r--. 1 root root 50M Jul  6 19:21 03.txt
-rw-r--r--. 1 root root 80M Jul  6 19:21 04.txt
复制代码

 

posted @   小鲨鱼2018  阅读(226)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示