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
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律