A2oz

Which command can be used to rename files in Linux?

Published in Linux commands 2 mins read

The mv command is used to rename files in Linux.

How to use the mv command:

The basic syntax for renaming a file using the mv command is:

mv [old_filename] [new_filename]

For example, to rename a file named "old_file.txt" to "new_file.txt", you would use the following command:

mv old_file.txt new_file.txt

Practical Examples:

Here are some practical examples of using the mv command to rename files:

  • Renaming a file with spaces:
      mv "My File.txt" "My_File.txt"
  • Renaming multiple files:
      mv *.jpg *.png

    This command will rename all files with the ".jpg" extension to ".png".

  • Moving and renaming a file:
      mv old_file.txt /home/user/new_directory/new_file.txt

    This command will move the file "old_file.txt" to the directory "/home/user/new_directory" and rename it to "new_file.txt".

Key Points:

  • The mv command can be used to rename both files and directories.
  • If the new filename already exists, the original file will be overwritten.
  • Use caution when using wildcard characters (e.g., *) as they can affect multiple files.

Related Articles