Rename file in linux has two ways: linux rename command and linux mv command.
linux rename command
rename command – renames multiple files.
Ubuntu is used as the test system, it does not have the remanae command installed by default.
sudo apt install rename
syntax
rename 's/oldName/newName/' files
rename [options] 's/oldName/newName/' files
example
Replace “-” in the file name with “_”
$ rename 's/\-/_/' test-*
![](https://www.linuxcommands.site/wp-content/uploads/2019/11/image-46-1024x220.png)
Replace “-” in the file name with “_” in all files starting with “test-“.
The * in test – * is a wildcard, indicating any multiple characters, which means any file starting with “test-“.
Remarks: The letter "s" stands for substitution and is part of the regular expression.
In addition to regular substitution, you can also use the “y” parameter for letter case conversion.
$ rename 'y/[a-z]/[A-Z]/' test-*
options
- -v, –verbose
Verbose: print names of files successfully renamed. - -0, –null
Use \0 as record separator when reading from STDIN. - -n, –nono
No action: print names of files to be renamed, but don’t rename. - -f, –force
Over write: allow existing files to be over-written.
example
$ rename -v 's/_/\-/' test_*
linux mv command
mv command – move (rename) files or directory.
When the mv command is used to rename a file, it only supports the operation of a single file. But with the help of awk command, we can modify file names in batches.
syntax
mv [options] oldName newName
mv [options] oldDirectory newDirectory
example
- rename file linux
$ mv TEST-1 test-1
- rename folder linux
options
- -f, –force
do not prompt before overwriting - -v, –verbose
explain what is being done
$ mv -v TEST-2 test-2
linux awk command
use awk command and mv command batch rename
➜ ls | grep test | awk '{system("mv "$0" "substr($0,0,10)"")}'
Debian (and Ubuntu as derivative) have `rename` in the util-linux package under the name `rename.ul`. The package you get when installing `rename` is a perl program, but not the basic utility.
OK, I see. Thank you!