Rename Files in Linux

Using the mv Command:

  1. Open a terminal window.
  2. Navigate to the directory containing the file you want to rename: Type cd /path/to/directory and press Enter. Replace /path/to/directory with the actual path to the directory.
  3. Use the mv command to rename the file: Type mv old_filename.txt new_filename.txt and press Enter. Replace old_filename.txt with the current name of the file and new_filename.txt with the desired new name.

Example: To rename a file named document.docx to updated_document.docx, you would type:

mv document.docx updated_document.docx

Renaming Multiple Files:

  1. Use wildcards to rename multiple files with a pattern: Type mv *.jpg *.jpeg and press Enter to rename all .jpg files to .jpeg files.

  2. Combine mv with a loop for more complex renaming: Type for f in *.txt; do mv “<span class=”math-inline”>f” “</span>{f%.txt}_new.txt”; done and press Enter to rename all .txt files by adding “_new” before the .txt extension.

Using the rename Command (if available):



  1. Install the rename command if necessary: Type sudo apt install rename (for Debian/Ubuntu) or sudo yum install rename (for Fedora/CentOS) and press Enter.

  2. Use the rename command for advanced renaming: Type rename ‘s/old_pattern/new_pattern/’ *.ext and press Enter. Replace old_pattern with the pattern to match in the old filenames, new_pattern with the replacement pattern for the new filenames, and *.ext with the file extension of the files you want to rename.

Example: To remove “_old” from all .txt filenames, you would type: rename ‘s/_old//’ *.txt

Additional Tips:

  • Use the i option with mv for interactive renaming (prompts before overwriting): mv -i old_filename new_filename
  • Use mv -v to see detailed information about the renaming process: mv -v old_filename new_filename
  • Always double-check the file extensions to avoid accidental changes.
  • Consider using a file manager for GUI-based renaming if you prefer a visual approach.

Leave a Reply