Renaming a file on the command line. Batch renaming files in Windows

If you have a bunch of files that you want to rename, but don't want to deal with each one individually? Windows provides many ways to do this.

You can easily rename one or more files to Windows Explorer(Explorer), but you can also do this using the command line or PowerShell.

Let's look at each option and how it works.

Rename multiple files in Windows Explorer.

Windows Explorer (known as File Explorer in Windows 10) is surprisingly powerful. You probably know how to rename a single file, but let's start with the basics since there are advanced techniques available.

If you're using a mouse, you have at least three ways to select a file name and rename it.

We can do the following:

⦁ Click to select a file and then click the " Rename" in the main menu.
⦁ Click to select a file and then click the name of the selected file.
⦁ Right-click on the file and then select " Rename " in the context menu.

And if you prefer a keyboard then you can simply use the arrow keys (or start typing the file name) to select the file and then press the key F2 to select a file name. Once you have highlighted the name of the selected file, you can immediately enter your name.

When you finish entering the file name, click Enter(or just click somewhere else) to save the new name.

Here's where things get interesting: it's when you press the key Tab to automatically select the next file name in a folder, so you can immediately start typing a new name for it. Keep pressing Tab and enter the names this way and you can easily rename all the files in the folder.

If you have a bunch of files in the same folder and those files don't need different names then Windows provides an easy way to rename those files in batch.

Start by selecting several files while holding down the key Ctrl+A to select multiple files at once, or select files using one of the button commands Rename on the menu home, command in the context menu, or just click F2. You will see that all files remain selected.

Enter a new file name, and then press Enter or click anywhere else in this window. All selected files will be renamed using the name you just entered, and added with a number in brackets so that there are numerical differences.

Renaming multiple files from the command line

If you need more options you can use the rename or ren command in the Command Prompt window on one or more files.

The command accepts wildcard characters such as * and ?

to reconcile multiple files, which can be useful if you only want to rename a specific set of files in a folder. The fastest way to open a Command Prompt window in the desired location is to first open the folder in File Explorer. On the menu " File " select item " Open command line " select item "".

", and then select "

To rename a single file, you can use the following command syntax:

ren "current_filename.ext" "new_filename.ext"
These quotes are required if your filenames contain spaces. If there are no spaces then you don't need quotes.

So, for example, to rename files from “WordFile (1).docx” to “file (01).docx” we use the following command:

ren "wordfile (1).docx" "my word file (01).docx" Because the ren

The command can also be used to change the file extension, you can also use it to change the extensions of multiple files at the same time. Let's say for example you had a selection of .txt files that you wanted turned into .html files. You can use the following command along with the asterisk * wildcard pattern:

ren *.txt *.html

PowerShell Renaming multiple files using PowerShell

offers even more flexibility for renaming files in a command line environment. With PowerShell, you can use the output of a single command known as " commandlet

" in PowerShell terminals such as on Linux and other UNIX-like systems. The two important commands you need are Dir, which lists the files in the current directory, and Rename-Item, which renames the item (the file, in this case). The fastest way to open a Command Prompt window in the desired location is to first open the folder in File Explorer. On the menu " File The fastest way to open a PowerShell window in the desired location is to first open the folder in. On the menu " Open command line Open Windows PowerShell."

Open Windows Powershell

First, let's look at renaming a single file. To do this you need to use the following syntax:

rename-item "current_filename.ext" "new_filename.ext"

So, for example, to rename a file from “wordfile.docx” to “My Word File.docx” we use the following commandlet:

It's quite simple. But the real capability in PowerShell comes from the ability to syntax commandlets together and some of the conditional output supported by rename-itemcommandlet.
Let's say, for example, we had a bunch of files named "WordFile(1).docx", "WordFile(2).docx", and so on.

Let's say we wanted to replace the space in these file names with an underscore, so that there were no spaces in the file names. We could use the following commandlet:

dir | rename-item -NewName ($_.name -replace " ","_")

And now, our files look the way we want.

AdvancedRenamer application

There is another renaming tool called AdvancedRenamer. It also provides a huge number of renaming methods. The tool does the job perfectly and has a much more user-friendly interface and supports creating advanced batch jobs so you can combine multiple renaming methods and apply them to a large number of files . You can also save the created rename method for later use.

In the example below, I have created a rename method using the following syntax:

Word File_ ___()

This command tells AdvancedRenamer to name all my files in "Word File" and add the creation date in GMD format (separating each word with an underscore). It also adds the file extension number in parentheses and is separated from each other by an additional underscore.

And as you can see, my files have been renamed exactly the way I want.

Are there other ways to rename files in Windows? Don't forget to leave us a comment and let us know about it.

Changes the name of a file or set of files.

Syntax

rename[disk: ][path] file_name_1 file_name_2

Because the[disk: ][path] file_name_1 file_name_2

Options

[disk: ][path] filename1 Specifies the location and name of the file or set of files to rename. file_name_2 Specifies a new file name. If wildcards (* and ?) are used, then file_name_2 specifies new names for files. When renaming files, you cannot specify a new drive or path. /? Display help on the command line.

Notes

  • Renaming files

    All files matching a given file name can be renamed. Team rename cannot be used to rename files on different drives or to move them to a different directory.

  • Using wildcards when renaming

    Wildcards (* and ?) can be used in parameters specifying names. If they are used in the parameter file_name_2, then the characters replaced by wildcards will be the same as in the parameter file_name_1.

  • The rename command will not work if file_name_2 already exists.

    If the file name specified by the parameter file_name_2, already exists, command rename will display the following message:

    Duplicate file name or file not found

Examples

Let's say that you need to change the name extensions of all files in the current directory with the extension .txt; for example, you need to replace the extension .txt with .doc. To make these changes, enter:

To rename the file or directory Chap10 to Part10, enter.

Renaming a linux file is a very simple operation, but for Linux newbies, this task can be challenging. There are also several nuances and features that it is advisable for experienced users to know, for example, mass renaming. Everything is done very simply in the graphical interface, but the terminal gives real flexibility.

In this article we will look at how to rename a file in Linux using the terminal, we will look at features such as bulk batch renaming of files, as well as regular expressions.

How to rename a file in Linux using mv

Linux has a wonderful standard utility called mv that is designed to move files. But at its core, moving is the same as renaming a linux file if done in the same folder. Let's first look at the syntax of this command:

$ mv options source file destination file

Now let's look at the main utility options that you may need:

  • -f- replace the file if it already exists;
  • -i- ask if existing files need to be replaced;
  • -n- do not replace existing files;
  • -u- replace the file only if it has been changed;
  • -v

To rename a linux file, just call the utility without additional options. Simply passing it the name of the desired file and the new name:

As you can see, the file has been renamed. You can also use the full path to the file or move it to another folder:

mv /home/sergiy/test/newfile /home/sergiy/test/file1

Please note that you must have write permissions to the folder in which you are going to rename the files. If the folder is owned by another user, you may need to run the program via sudo. But in this case, it is better to run with the -i option, so as not to accidentally delete anything.

Renaming Linux files using rename

There is another command in Linux that allows you to rename a file. This is rename. It is specifically designed for this task, so it supports things like bulk renaming of Linux files and the use of regular expressions. The utility syntax is also more complicated:

$ rename options "s/old_name/new_name" files

$ rename options old_name new_name files

The old name is a regular expression or part of the name that needs to be changed, the new name indicates what needs to be replaced. Files are those that need to be processed, wildcard characters such as * or ? can be used to select files.

Let's look at the utility options:

  • -v- display a list of processed files;
  • -n- test mode, in fact, no actions will be performed;
  • -f- forcefully overwrite existing files;

For example, let's rename all htm files from the current folder to .html:

rename "s\.htm/\.html/" *.htm

Or for images:

rename "s\.jpeg/\.jpg/"*

The asterisk symbol means that linux file renaming will be done for all files in the folder. Additional modifiers can be used in regular expressions:

  • g(Global) - apply to all found occurrences;
  • i(Case Sensitive) - ignore case.

Modifiers are placed at the end of the regular expression, before the closing quote. Before using such a construction, it is advisable to check it to make sure that you have not made any mistakes; this is where the -n option comes to the rescue. Let's replace all occurrences of DSC with photo in the names of our photos:

rename -n "s/DSC/photo/gi" *.jpeg

DSC, DsC and even dsc will be processed, all variants. Since the -n option was used, the utility will only display the names of the images that will be changed.

You can use not only regular replacement, but also full-fledged regular expressions to batch rename linux files, for example, convert all names to lowercase:

rename "y/A-Z/a-z/"*.jpeg

From this example we see that even if such a file already exists, it will not be overwritten by default. Don't forget to use the -n option to avoid accidentally damaging anything.

Renaming files in pyRenamer

If you don't like using the terminal, but need to bulk rename Linux files, then you'll love the pyrenamer utility. This is a graphical program and all actions here are performed in a few mouse clicks. You can install it from the official repositories:

sudo apt install pyrenamer

In the program window, you can see the file system tree, the central part of the window where the files that will be changed are displayed, as well as a panel for specifying renaming parameters.

You can remove or add characters, change case, and automatically remove spaces and underscores. The program has tips to make it even easier:

Experienced users will love pyRenamer's ability to rename media files from their metadata. In addition, you can rename one file if necessary. This utility fully implements mv and remove functionality in a GUI.

conclusions

In this article we looked at how to rename a file in the Linux console. Of course, there are other ways, for example, writing a script, or using file managers. How do you perform complex renaming operations? Write in the comments!

To rename one or more files, as well as directories using the command line, use the commands RENAME and MOVE.

The RENAME command has the following syntax: RENAME [drive:][path] filename1 filename2. In this case, “file_name1” defines the current name of the file (directory), and “file_name2” defines the name of the file (directory) that it will receive after renaming. For example, let’s create a file “text.txt” on drive “C”, then the command to rename this file to a file “file.txt” will look like this: rename text.txt file.txt

The RENAME command can rename not only file names, but also their extensions. For example, let's take the file "file.txt" that we obtained earlier by renaming the file "text.txt". Let's change the extension of this file from “txt” to “doc”: rename file.txt file.doc

Naturally, the RENAME command can change the file name and its extension at the same time. For example, let's rename the file “file.doc” to the file “text.txt”: rename file.doc text.txt

The RENAME command allows wildcards. Wildcards can be used in file names. For example, let’s create another file “format.txt” on drive “C” in addition to the existing file “text.txt”. Then, to rename (change the extension) both files, use the command: rename *.txt *.doc

If wildcards are used only in the filename2 parameter, the resulting file name will be the same as the source file name. For example, the command rename format.doc *.txt will change the file name "format.doc" to "format.txt"

If the file specified by the "file_name2" parameter already exists, then the RENAME command will not be executed. For example, let's try changing the file "text.doc" to "format.txt": rename text.doc format.txt

When renaming a directory, the “file_name1” parameter determines the current directory name, and the “file_name2” parameter determines the resulting directory name. For example, let’s create a folder “11” on drive “C”, then the command to rename this folder to folder “22” will look like this: rename 11 22

To reinforce this material, you can experiment with wildcards. For example, take the following command: rename *.* file.html . It seems pointless, because... all files in the current directory are renamed into one file "file.html". However, this command works fine. The fact is that all files are renamed one by one. As a result, the first (alphabetical) file will be renamed normally, and when you try to rename the 2nd file, the message “a file with the same name already exists or not found” will appear.

When you use the RENAME command for the filename2 parameter, you cannot specify a different drive or directory. To do this, use the MOVE command.
The RENAME command is similar to REN. Both commands perform similar actions and come from the English. "rename" - rename.

Previous article: DEL command to delete files.
Table of contents: Command line.
Next article: MOVE Team.