linux如何批量重命名多个文件

时间:2019-04-29 03:18:25  来源:igfitidea点击:

如何批量将dir1, dir2, dir3重命名为dir001, dir002, dir003。

要重命名多个文件夹,可以使用来自MariaDB/MySQL或Linux实用程序包的rename命令。
或者使用bash的for循环。
或者使用mmv命令

Linux使用rename命令重命名多个文件夹

语法

rename expression replacement file

示例

[root@lautturicentos8 tmp]# touch file{1..4}.tar.gz
[root@lautturicentos8 tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 22 05:03 file1.tar.gz
-rw-r--r--. 1 root root 0 Apr 22 05:03 file2.tar.gz
-rw-r--r--. 1 root root 0 Apr 22 05:03 file3.tar.gz
-rw-r--r--. 1 root root 0 Apr 22 05:03 file4.tar.gz
[root@lautturicentos8 tmp]# rename -v .tar.gz .tar *.tar.gz
'file1.tar.gz' -> 'file1.tar'
'file2.tar.gz' -> 'file2.tar'
'file3.tar.gz' -> 'file3.tar'
'file4.tar.gz' -> 'file4.tar'
[root@lautturicentos8 tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 22 05:03 file1.tar
-rw-r--r--. 1 root root 0 Apr 22 05:03 file2.tar
-rw-r--r--. 1 root root 0 Apr 22 05:03 file3.tar
-rw-r--r--. 1 root root 0 Apr 22 05:03 file4.tar

重命名目录

[root@lautturicentos8 tmp]# mkdir dir{1..5}
[root@lautturicentos8 tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir1
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir2
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir3
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir4
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir5
[root@lautturicentos8 tmp]# rename -v dir dir00 dir?
'dir1' -> 'dir001'
'dir2' -> 'dir002'
'dir3' -> 'dir003'
'dir4' -> 'dir004'
'dir5' -> 'dir005'
[root@lautturicentos8 tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir001
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir002
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir003
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir004
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir005

使用bash for循环重命名多个文件夹

去掉目录名中的空格

for i in *
do 
  mv -v "$i" "${i// /}"
done

将目录名中的00去掉:

for i in *
do 
  mv -v "$i" "${i//00/}"
done
[root@lautturicentos8 tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir001
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir002
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir003
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir004
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir005
[root@lautturicentos8 tmp]# 
[root@lautturicentos8 tmp]# for i in *
> do 
>   mv -v "$i" "${i//00/}"
> done
renamed 'dir001' -> 'dir1'
renamed 'dir002' -> 'dir2'
renamed 'dir003' -> 'dir3'
renamed 'dir004' -> 'dir4'
renamed 'dir005' -> 'dir5'
[root@lautturicentos8 tmp]# 
[root@lautturicentos8 tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir1
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir2
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir3
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir4
drwxr-xr-x. 2 root root 6 Apr 22 05:05 dir5

将.tar扩展名改成.zip

for i in *.tar
do 
  mv -v -- "$i" "${i%.tar}.zip"
done
[root@lautturicentos8 tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 22 05:12 file1.tar
-rw-r--r--. 1 root root 0 Apr 22 05:12 file2.tar
-rw-r--r--. 1 root root 0 Apr 22 05:12 file3.tar
-rw-r--r--. 1 root root 0 Apr 22 05:12 file4.tar
-rw-r--r--. 1 root root 0 Apr 22 05:12 file5.tar
[root@lautturicentos8 tmp]# for i in *.tar
> do 
>   mv -v -- "$i" "${i%.tar}.zip"
> done
renamed 'file1.tar' -> 'file1.zip'
renamed 'file2.tar' -> 'file2.zip'
renamed 'file3.tar' -> 'file3.zip'
renamed 'file4.tar' -> 'file4.zip'
renamed 'file5.tar' -> 'file5.zip'
[root@lautturicentos8 tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 22 05:12 file1.zip
-rw-r--r--. 1 root root 0 Apr 22 05:12 file2.zip
-rw-r--r--. 1 root root 0 Apr 22 05:12 file3.zip
-rw-r--r--. 1 root root 0 Apr 22 05:12 file4.zip
-rw-r--r--. 1 root root 0 Apr 22 05:12 file5.zip
[root@lautturicentos8 tmp]# 

在Linux中使用mmv重命名多个文件

安装mmv命令

### CentOS/RHEL
sudo yum install mmv

### Ubuntu/Debian
sudo apt install mmv

### Fedora 
sudo dnf install mmv

mmv语法

mmv from to
mmv [options] from to

mmv示例

### 所有文件名大写
mmv "dir*" "#u1"

### 所有文件名小写
mmv "dir*" "#l1"

### 将.PY扩展名的文件改成.Pl
mmv '*.PY' '#1.pl'