Linux Rename Files And Folders With Rename Command With Examples – POFTUT

Linux Rename Files And Folders With Rename Command With Examples


I have some files and directories and I want to rename those. How can I rename them? or are there any alternative methods? Linux provides different tools and commands in order to rename files and directories. In this tutorial, we will examine mv and  rename commands.

Syntax

The basic and most used method to rename files is using the mv command. Below you can find the mv command syntax.

mv OPTION OLD_FILENAME NEW_FILENAME
  • OPTION is optional and can be used for different options like verbose mode, warn before the move, etc.
  • OLD_FILENAME is the current file or folder name which will be changed to NEW_FILENAME.
  • NEW_FILENAME is the new file or folder name which will be set.

Linux rename file is easy. As we see the syntax is easy for simple rename operation with mv . In this example, we will rename the file named live into dead.

$ mv live dead

Rename Files With Full Path

We can use the full path while renaming files. In this example, we will rename the file named dead into live .

$ mv /home/ismail/dead /home/ismail/live

List Renamed Files

After renaming we may want to see files by listing them. We will use ls to list files.

$ ls
List Renamed Files
List Renamed Files

Ask Before Overwrite Existing File

While moving new-file-name may already exist. In this situation mv command by default overwrites. To prevent overwrite we can provide -i option to ask before overwriting.

$ mv -i live dead

Verbose Mode

While renaming we can output verbosely to during rename operation.

$ mv -v live dead
Verbose
Verbose

Rename Command

There is a command named rename which whole purpose is rename files and directories. Rename command has the following syntax.

rename 'S/OLDNAME/NEWNAME/' FILES
  • OLDNAME is matched  oldname
  • NEWNAME will be set according to OLDNAME
  • FILES those files we want to look OLDNAME
LEARN MORE  Windows Ren Command Tutorial with Examples To Rename Files and Folders

In this example, we will change those files who have nix in their named into x .

$ rename 's/est/x/' *

Specify File and Folder Names

We can also specify the files and directories we want to rename. In this example, we will rename files and directories those ends with .txt

$ rename 's/est/x/' *.txt

Leave a Comment