How to rename multiple files at once?

QuestionsQuestions8 SkillsProDec, 19 2025
0119

In basic Linux, renaming multiple files at once is slightly different because the mv command usually handles only one source at a time when renaming.

Depending on what you want to achieve, here are the most common ways to do it:

1. Using a for loop (The Standard Way)

If you want to change the extension of all .txt files to .bak, you can use a simple loop in the terminal:

for file in *.txt; do
    mv "$file" "${file%.txt}.bak"
done
  • file in *.txt: Finds all text files.
  • ${file%.txt}.bak: This takes the filename, removes .txt, and adds .bak.

2. Using the rename command

In many Linux systems, there is a dedicated tool called rename. It uses "regular expressions" to change names.

To change all .log files to .txt:

rename 's/\.log$/\.txt/' *.log

(Note: If the command is not installed, you can install it with sudo apt update && sudo apt install rename)

3. Using Wildcards (For moving to a new folder)

If your goal is to "rename" paths (moving many files at once to a new location), you can use the wildcard *:

mv *.txt backup/

In this case, the filenames stay the same, but their "full name" (path) changes.

Short Summary:

  • To rename one file: Use mv.
  • To rename many files: Use a for loop or the rename command.

Which specific files are you trying to rename? Tell me the pattern (e.g., "add a prefix to all files"), and I can give you the exact command!

0 Comments

no data
Be the first to share your comment!