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-*
rename file linux

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_*
rename file linux

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 file linux

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

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *