Fundamentals 5 min read

Rename Files Faster in Linux: Bash Function & Shortcut Tricks

This guide shows how to avoid typing a filename twice when renaming files on Linux by adding a custom mv function to ~/.bashrc, using brace expansion with mv/cp, and leveraging shell shortcuts like Ctrl+W and Ctrl+Y for rapid input.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Rename Files Faster in Linux: Bash Function & Shortcut Tricks

Problem

Renaming a file with mv normally requires typing the full source and destination names, e.g. mv file1.txt file2.txt. For long filenames this duplication is tedious.

Method 1: Bash function that prompts for the new name

Edit your ~/.bashrc (or the appropriate shell startup file) and append the following function:

# Bash function to rename a file without re‑typing the old name
function mv() {
  # If the call does not have exactly one existing argument, fall back to the real mv
  if [ "$#" -ne 1 ] || [ ! -e "$1" ]; then
    command mv "$@"
    return
  fi
  # Prompt the user with the current name, allowing in‑place editing
  read -ei "$1" newfilename
  # Perform the rename with verbose output
  command mv -v -- "$1" "$newfilename"
}

After saving, reload the file: source ~/.bashrc Now you can rename a file by typing only the original name: mv file1.txt The shell will display the current name, let you edit it directly, and execute the rename when you press Enter . The function falls back to the real mv for any usage that does not match the single‑argument pattern (e.g., moving multiple files, using options, etc.).

Method 2: Brace expansion for simple pattern changes

If only a part of the filename changes, Bash brace expansion can generate the source and destination in a single command.

# Rename file1.txt to file2.txt
mv file{1,2}.txt

The same syntax works with cp to keep the original and create a copy:

# Copy file1.txt to file2.txt
cp file{1,2}.txt

This technique is limited to patterns that can be expressed with a single pair of braces.

Method 3: Shell line‑editing shortcuts

Most interactive shells support the following shortcuts while editing a command line:

Ctrl+W – cuts (deletes) the word immediately before the cursor and stores it in the kill ring.

Ctrl+Y – yanks (pastes) the most recently killed text at the cursor position.

When renaming, you can type the first filename, press Ctrl+W to delete it, type the second filename, and then press Ctrl+Y to paste the first name back, avoiding re‑typing the whole string.

Shell shortcut demonstration
Shell shortcut demonstration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

productivitycommand-lineFile Renaming
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.