Site icon LinuxCommands.site

rename file linux – use rename cmd or mv cmd or awk cmd

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-*

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

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

$ mv TEST-1 test-1

options

$ 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)"")}'

Exit mobile version