How to rename a file in Linux?

In general, renaming or managing files is one of the simplest tasks an operating system has to perform. Under Linux, this can be done via the terminal or via the GUI file manager.

In this article we will discuss three different ways to rename files in Linux – two using the terminal command and the third using the GUI.

If you are using the terminal, you can use either of the following two commands to rename files.

The mv command is designed to move files, but as a side effect, you can also use it to rename files. For large jobs, on the other hand, the Rename command is very useful because it is only meant to rename files, as you guessed.

Also read : How do I install PHP on Ubuntu?

How to rename files in Linux with the mv command?

The normal syntax of the mv command is as follows.

mv [options] destination/source file path

Although the source file may be one or more, the destination path must be a single file or directory. Thus, to rename a file, simply specify the name of the file as the source and then enter the new name in the destination path.

Look at this example.

mv file1.txt file_new.txt

The above command renames file1 to file_new.

The mv command can only rename one file at a time, but you can use the command in a bash script to rename multiple files at once.

for f in *.html ; do
mv — $f ${f%.html}.php

In the above example, the for loop goes through all files with the .html extension. The second line renames each file and changes the extension from .html to .php. The statement made only marks the end of the loop.

You can also use the mv command in combination with the find command to get the same result.

find. -depth -name *.html -exec sh -c ‘f={} ; mv — $f ${f%.html}.php’ ;

Here, the find command passes all files with the extension .html in the hot folder to the mv command, which in turn renames them to .php files.

Also read : How to install OpenCV on Ubuntu?

How to rename files in Linux using the rename command?

The rename command can rename multiple files at once, which is its biggest advantage over the mv command. However, some knowledge of regular expressions is necessary to use this command optimally.

Another thing you should know before using this command is that it is not part of the standard Linux distribution. To set the command, enter the following command.

  • Ubuntu/Debian – sudo apt rename installation
  • CentOS/Fedora – sudo yum install preconfiguration
  • Arch Linux – yay perl-rename ## or yogurt -S perl-rename
  • Manjaro – sudo pacman -Syu perl-rename

Now that the command is installed, here is the basic syntax for the same.

rename [options] files ‘s/oldName/newname/’.

To return to our previous example: If you want to rename some .html files to .php, you need to use the following command

rename ‘s/.html/.php/’ *.html

You can also use the -n option to print the file names before renaming them.

Rename -n ‘s/.html/.php/’ *.html

By default, existing files are not overwritten when renamed. If you rename a lot of files at once, you may have a lot of clutter in your folders. Use the -f option to allow the command to overwrite existing files.

Rename -f ‘s/.html/.php/’ *.html

In another example, if you wanted to replace spaces in the filename with underscores, you would do the following

rename y/ /_/ *

There is also the -B option. In this case, the list of renamed files is printed with their new names during the execution of the command.

You can even remove parts of the filename by omitting the search term in the regular expression.

rename ‘s/ext_/’ *.c

The above command removes ext_ from all files with that name in the current working directory.

To change specific parts of the name, simply list the part you want to change, followed by the new part.

rename ‘s/conf/parameter/’ *.c

The above command replaces the word conf in each filename with the parameter. Note that because *.cfiles is listed, this command only works for files with the extension .c.

Also read : How to delete a directory under Linux?

With the Linux GUI file manager, renaming is exactly what you would expect. Just right-click the file or folder, click Rename, and enter a new name. You can also use the F2 key combination.

However, you can use batch renaming programs like Metamorphose2 or pyRenamer to rename multiple files at once to make your life easier.

Also read : How to use the SCP command to transfer files under Linux?

The one who writes/cuts/films/owns all the technology, and when he’s not around, switches to virtual machine races. You can contact Yadullah at [email protected], or follow him on Instagram or Twitter.

frequently asked questions

What is the command to rename files in Linux?

Use mv to rename the file by typing mv, the space, the filename, the space, and the new name you want to give the file. Then press the Enter key. With ls, you can check if the file is renamed.

How do I rename a file?

Stal ‘ Rename files

How to rename a file in Unix

Renaming files to file Unix does not have a special command to rename files. Instead, the mv command is used to both change the file name and move the file to another directory.

how to rename a folder in linux,delete file linux,how to rename a file in unix,linux rename command examples,rename multiple files in linux,linux copy file,Feedback,Privacy settings,How Search works,move file linux,copy and rename file linux

You May Also Like