
Renaming files and directories is a routine but essential task when managing Linux systems. Whether you're organizing documents, restructuring a project, or automating workflows, Linux provides powerful command-line tools to simplify the process.
This article explains multiple ways to rename files and directories using the mv and rename commands. You’ll also learn how to perform bulk renaming with shell loops and apply best practices to avoid overwriting files or introducing naming errors.
Before you begin, you need to:
Linux offers multiple ways to rename files and directories using command-line tools. The most commonly used methods include:
mv: Renames or moves files and directories.rename: Renames multiple files using Perl-compatible expressions.Each method serves different use cases, which the following sections cover in detail.
Renaming files in Linux helps organize content, improve clarity, and ensure consistent naming across your projects.
old_filename: The current name of the file.new_filename: The new name to assign.-i: Prompt before overwrite.-n: Prevent overwriting an existing file.Rename document.txt to report.txt.
Verify the result.
Output.
Prompt before overwriting.
Prevent overwriting with -n.
mv overwrites files silently unless you use -i or -n.
If the filename includes spaces, wrap it in double quotes.
This article uses the Perl-based rename utility (also called prename), which is default on Debian-based distributions like Ubuntu. On other distributions, such as CentOS or Fedora, rename may refer to a different C-based implementation with different syntax.
's/old/new/': Perl-style substitution pattern.files_to_rename: One or more files that match the pattern.Rename .txt files to .md.
Convert all filenames to lowercase.
To preview changes before renaming, use the -n flag with rename for a dry run.
Or use a loop with echo to simulate:
*.txt: All files ending in .txt.mv -- "$f": Rename each file individually."${f%.txt}.pdf": Change the extension from .txt to .pdf.Batch rename all .txt files to .pdf.
Add the -n flag to prevent overwriting files if they already exist.
Renaming directories helps improve project organization, enforce naming conventions, and maintain a clean directory structure. This section explores multiple methods to rename directories in Linux.
old_directory: The current name of the directory.new_directory: The new name to assign.-T: Treat the destination as a regular file or directory, not a target location.Rename the projects directory to work.
If work already exists and you want to overwrite it directly rather than move projects inside work/, use the -T flag.
's/old/new/': Perl-style substitution pattern.*/: Targets all directories in the current path.Replace old with new in all directory names.
*/: Matches all directories in the current path.${dir%/}_backup: Appends _backup to the directory name after removing the trailing slash.Append _backup to all directories.
Always preview loop-based renaming with echo before running destructive changes.
In this article, you learned how to rename files and directories in Linux using various command-line tools. You used the mv command for simple renames, the rename command for pattern-based bulk operations, and Bash loops for custom renaming logic. These techniques help you maintain a clean file structure, automate repetitive tasks, and manage large directories efficiently. For additional options and examples, explore the official man pages for mv and rename.
0 Comments
Be the first to comment and share your perspective with the community.