Renaming a file in the same Linux directory avoids the complexity of moving it to a different location. If you are wondering how to rename a file in linux in same directory, the answer is simpler than you might think. Linux offers multiple ways to do this, from basic commands to advanced batch renaming tools. This guide covers everything you need, with clear steps and practical examples.
You do not need to be a command-line expert. Whether you are a beginner or a seasoned user, renaming files in the same folder is a routine task. Let us break it down step by step.
How To Rename A File In Linux In Same Directory
The most common method uses the mv command. Yes, the same command you use to move files. In Linux, renaming is essentially moving a file to a new name within the same directory. The syntax is straightforward:
mv old_filename new_filename
For example, to rename report.txt to summary.txt in the current directory, you type:
mv report.txt summary.txt
That is it. The file stays in the same folder but gets a new name. No extra flags or options needed. This works for any file type—text files, images, scripts, or archives.
Using The Mv Command With Full Paths
If you are not already in the directory containing the file, you can specify the full path. For instance:
mv /home/user/documents/report.txt /home/user/documents/summary.txt
This renames the file while keeping it in the documents folder. The key point: the destination path must be the same as the source path, except for the filename.
Renaming Multiple Files With Mv
The mv command handles one file at a time. To rename multiple files in the same directory, you need a loop or a tool like rename. For example, using a bash loop to add a prefix:
for file in *.txt; do mv "$file" "backup_$file"; done
This renames all .txt files in the current directory by adding backup_ to the beginning. The files remain in the same directory.
Using The Rename Command For Batch Operations
For more complex renaming, the rename command is powerful. It uses Perl expressions to transform filenames. The basic syntax is:
rename 's/old_pattern/new_pattern/' files
For example, to change all .htm files to .html in the current directory:
rename 's/\.htm$/\.html/' *.htm
This renames every file ending in .htm to end with .html, all in the same directory. The command does not move files; it only changes names.
Installing The Rename Command
Not all Linux distributions include rename by default. On Debian-based systems like Ubuntu, install it with:
sudo apt install rename
On Red Hat-based systems like Fedora, use:
sudo dnf install prename
Check your distribution’s package manager. Once installed, the command works as described.
Practical Examples With Rename
- Convert filenames to lowercase:
rename 'y/A-Z/a-z/' * - Replace spaces with underscores:
rename 's/ /_/g' * - Add a date prefix:
rename 's/^/2025_/' *.log
All these operations keep files in the same directory. The rename command is ideal for bulk renaming without moving data.
Renaming Files With Graphical File Managers
If you prefer a visual interface, Linux desktop environments offer easy renaming. In GNOME Files (Nautilus), right-click a file and select “Rename.” Type the new name and press Enter. The file stays in the same folder.
In KDE Dolphin, the process is similar. Right-click, choose “Rename,” or press F2. For multiple files, select them all, right-click, and choose “Rename.” You can then apply patterns like adding numbers or replacing text.
Graphical tools are great for beginners. They avoid command-line errors and provide immediate visual feedback. However, for automation or server work, the command line is faster.
Using Thunar Bulk Rename
Xfce’s Thunar file manager includes a bulk rename tool. Select multiple files, right-click, and choose “Rename.” You can:
- Insert numbers sequentially
- Replace text
- Change case
- Remove or add characters
All changes happen in the same directory. This tool is intuitive and does not require memorizing commands.
Renaming Files With The Cp Command
You can also rename a file by copying it to a new name and deleting the original. For example:
cp oldfile.txt newfile.txt && rm oldfile.txt
This creates a copy with the new name, then removes the old one. The result is the same as renaming. However, this method is less efficient because it duplicates data. Use it only if you need a backup copy temporarily.
For large files, mv is much faster because it only changes the directory entry, not the file content.
Handling File Extensions And Case Changes
Renaming often involves changing extensions or fixing case. For a single file, mv works fine:
mv image.JPG image.jpg
For multiple files, use rename or a loop. For example, to change all .JPG to .jpg:
rename 's/\.JPG$/\.jpg/' *.JPG
This keeps every file in the same directory. The extension change is just a rename, not a conversion.
Renaming Hidden Files
Hidden files in Linux start with a dot (e.g., .bashrc). To rename a hidden file, include the dot in the name:
mv .oldconfig .newconfig
The file remains hidden in the same directory. Be careful not to accidentally rename system configuration files.
Common Mistakes And How To Avoid Them
One frequent error is overwriting an existing file. If you run mv file1.txt file2.txt and file2.txt already exists, it will be replaced without warning. To avoid this, use the -i (interactive) flag:
mv -i old.txt new.txt
This prompts you before overwriting. Alternatively, use -n to never overwrite:
mv -n old.txt new.txt
Another mistake is forgetting to specify the full path when not in the directory. Always double-check your current directory with pwd.
Renaming Files With Special Characters
Filenames with spaces, parentheses, or symbols require quoting. For example:
mv "my file.txt" "my_renamed_file.txt"
Use double quotes or escape characters with backslashes. This ensures the command sees the filename as a single argument.
Using The Mmv Command For Advanced Moves
The mmv command (mass move) is another option. It is not installed by default on most systems. Install it via your package manager. It allows pattern-based renaming:
mmv "*.htm" "#1.html"
This renames all .htm files to .html, keeping them in the same directory. The #1 represents the base name. mmv is useful for complex patterns but less common than rename.
Renaming Files In Scripts And Automation
When writing shell scripts, use mv inside loops. For example, to rename files based on a list:
while IFS= read -r oldname; do
newname="${oldname%.txt}.bak"
mv "$oldname" "$newname"
done < filelist.txt
This reads filenames from filelist.txt and renames them from .txt to .bak in the same directory. Always test scripts on copies first.
Using Find With Exec For Renaming
The find command can locate files and rename them. For instance, to rename all .log files in subdirectories:
find . -type f -name "*.log" -exec mv {} {}.old \;
This adds .old to each log file's name, keeping it in its original directory. The {} is replaced by the filename.
Renaming Files With Python Or Perl
For programmatic control, use Python's os.rename() or Perl's rename function. A simple Python script:
import os
os.rename('old.txt', 'new.txt')
This works in the same directory. For bulk renaming, iterate over a list of files. This approach is useful when you need complex logic, like renaming based on file content.
Understanding Inodes And Renaming
When you rename a file in the same directory, the inode number remains the same. The file's data does not move. Only the directory entry changes. This is why renaming is instantaneous, even for large files. You can verify with ls -i to see the inode before and after.
This also means that hard links to the file remain valid. If you have multiple hard links, renaming one does not affect the others. The file content stays accessible under the new name.
Renaming Directories In The Same Parent
Renaming a directory works the same way as renaming a file:
mv old_folder new_folder
The directory stays in its parent location. All contents inside remain unchanged. This is useful for reorganizing projects without moving files.
Using Tab Completion To Avoid Typos
When typing filenames, use the Tab key for autocompletion. This reduces errors, especially with long or complex names. For example, type mv rep and press Tab to complete report.txt. Then type the new name.
This tip saves time and prevents accidental overwrites. It works in most Linux terminals.
Renaming Files With Wildcards
Wildcards like * and ? can match multiple files. However, mv does not support renaming multiple files with a single wildcard command directly. You need a loop or rename. For example, this does NOT work:
mv *.txt *.bak # Wrong
Instead, use:
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
This renames all .txt files to .bak in the same directory.
Undoing A Rename
If you rename a file and immediately realize the mistake, rename it back:
mv newname oldname
There is no undo command in Linux. To avoid permanent errors, consider using version control or testing with dry-run tools. Some file managers have a trash feature, but command-line renames are immediate.
Using Git For Safety
If you are working in a Git repository, you can rename files with git mv:
git mv old.txt new.txt
This stages the rename for commit. Git tracks the change, allowing you to revert if needed. The file stays in the same directory within the repository.
Renaming Files With Case Sensitivity
Linux filenames are case-sensitive. File.txt and file.txt are different. To change case, use mv or rename. Be careful: on some filesystems, renaming to the same name with different case may fail or behave unexpectedly. For example:
mv File.txt file.txt # Works on ext4
This renames the file to lowercase. The file remains in the same directory.
Using The Basename And Dirname Commands
In scripts, basename and dirname help extract parts of a path. For renaming in the same directory, you might use:
oldname="/path/to/file.txt"
dirname "$oldname" # Returns /path/to
basename "$oldname" .txt # Returns file
Then construct the new name:
newname="$(dirname "$oldname")/$(basename "$oldname" .txt).bak"
This keeps the file in the same directory while changing the extension.
Renaming Files With Timestamps
To add a timestamp to a filename, use the date command:
mv log.txt "log_$(date +%Y%m%d).txt"
This creates log_20250321.txt in the same directory. The file is not moved, only renamed with a date stamp.
Common Use Cases For Same-Directory Renaming
- Fixing typos in filenames
- Standardizing naming conventions
- Adding version numbers
- Changing file extensions after conversion
- Organizing files by date or project
Each of these tasks can be done with mv or rename without moving files to other folders.
Performance Considerations
Renaming a file in the same directory is a metadata operation. It does not read or write the file content. Therefore, it is extremely fast, even for multi-gigabyte files. The only limitation is the filesystem's speed in updating directory entries.
For thousands of files, batch renaming with rename or a loop is efficient. Avoid renaming files one by one manually if you have many.
Renaming Files On Remote Systems
If you are working on a remote server via SSH, the same commands apply. Use mv or rename as if you were local. The file stays in the same directory on the remote machine.
For FTP or cloud storage, use the client's rename function. Most support renaming without moving.
FAQ: How To Rename A File In Linux In Same Directory
What is the simplest command to rename a file in the same directory?
The simplest command is mv oldname newname. It renames the file without moving it to a different folder.
Can I rename multiple files at once in the same directory?
Yes, use the rename command or a bash loop. For example, rename 's/\.txt$/\.bak/' *.txt renames all text files.
Does renaming a file change its location?
No, renaming with mv in the same directory keeps the file in its original folder. Only the name changes.
What happens if the new filename already exists?
The existing file is overwritten without warning unless you use the -i (interactive) or -n (no overwrite) flag.