commands.page Website Logo

  • Home
  • Categories
  • Search

Rename Files on Terminal in Ubuntu

This is an article about how to rename files and directories using the terminal in Ubuntu. In this guide, we’ll explore various methods for renaming files and directories, from basic commands like mv (move) to more advanced techniques involving regular expressions. Read this article to find out how you can streamline your file management tasks by mastering these command-line tools.

Introduction

The Linux Terminal is a powerful tool that allows users to perform complex operations with just a few keystrokes. One of the most common tasks on any operating system is renaming files and directories, which in Ubuntu can be easily done using the command line. Whether you need to rename hundreds of files at once or simply want a quick way to change file names without opening graphical user interfaces (GUIs), knowing how to use terminal commands for this purpose is highly beneficial.

In this article, we will cover everything from basic renaming techniques to advanced batch renaming methods. We’ll also look into some useful tips and tricks that can make your workflow more efficient when managing files on Ubuntu’s command line interface.

Basic Renaming with mv Command

The mv (move) command is one of the most fundamental commands for renaming files in Unix-like operating systems, including Ubuntu. By default, it moves files or directories from one place to another but also serves as a simple way to rename files and directories by changing their names within the same directory.

Syntax

To use mv to rename a file:

mv old_filename new_filename

For renaming a directory:

mv old_directory_name new_directory_name

Example

Suppose you have a file named report.txt, and you want to rename it to summary.txt. You can do this by executing the following command in your terminal:

mv report.txt summary.txt

Similarly, if you need to change the name of a directory from photos to images, simply type:

mv photos images

Renaming Multiple Files

Often, users find themselves needing to rename several files at once. While renaming each file individually using mv would be time-consuming and impractical, you can use shell globbing (pattern matching) along with the mv command to handle multiple renamings efficiently.

Syntax

To rename a set of files with similar patterns:

mv pattern_old* new_pattern*

Example

Let’s assume that you have several files in your directory named as follows: image01.jpg, image02.jpg, and so forth, but now you want to change them all to a naming format like photo_01.jpg. You can perform this action by using the following command:

mv image*.jpg photo_*.jpg

This will match all files that start with “image” followed by any sequence of numbers and end with “.jpg”, renaming them to start with “photo_” instead.

Batch Renaming Using Find Command

The find command is an extremely versatile tool for searching through file systems. In combination with the mv command, it can be used to rename files based on specific criteria such as modification time, file type, or content patterns.

Syntax

To use find in conjunction with mv:

find directory -type f -name pattern_old* -exec mv {} new_pattern* \;

Example

Imagine you have a large folder named backups containing many files that were generated daily. The filenames follow the pattern backup_YYYY-MM-DD.sql, and now you need to update them to include a version number, such as v1_backup_YYYY-MM-DD.sql. Here’s how you can achieve this:

find backups -type f -name "backup_*" -exec mv {} v1_\{\} \;

In the above command:

  • -type f specifies that we’re looking for files (not directories).
  • v1_{} \; tells mv to rename each file found by adding v1_ at the beginning.

Using Sed Command for Complex Renaming

When dealing with complex renaming requirements, such as replacing parts of filenames based on regex patterns, the sed command can be very useful. The sed utility allows you to perform text transformations in files and streams, making it ideal for tasks like updating file names.

Syntax

To use sed within an mv pipeline:

find directory -type f -name pattern* | while read i; do mv "$i" "$(echo $i | sed 's/old_pattern/new_pattern/')"; done

Example

Suppose you have a set of files named like project_2019Q4.zip, and you want to change them all so that the year is followed by “Report”, resulting in names such as project_Report2019Q4.zip.

find . -type f -name 'project_*' | while read i; do mv "$i" "$(echo $i | sed 's/_\(.*\)_\(..\)\(.\{2\}\)/_\1Report\3/')"; done

Here, sed is used to match the old pattern and replace it with a new one. The \( and \) notation groups parts of the pattern so that they can be referenced later using \1, \2, etc.

Renaming Files by Extension

Sometimes you may need to rename files based on their extensions, such as converting .txt files into .md. This is straightforward with mv, but if you want more control over how filenames are generated (like adding a datestamp), you can combine commands like date and find.

Syntax

find directory -type f -name "*.ext1" -exec mv {} {}.new_ext \;

Example

Consider having multiple .docx files that need to be renamed to .pdf. Using the mv command alongside find, you can accomplish this task as follows:

find Documents -type f -name "*.docx" -exec mv {} {.}.pdf \;

This will convert every .docx file in your Documents directory into a corresponding .pdf.

Conclusion

Mastering the art of renaming files and directories via terminal commands is an essential skill for efficient file management under Ubuntu. Whether you’re dealing with individual files or entire sets, understanding how to use commands like mv, find, and even leveraging tools such as sed can drastically improve your productivity.

In this article, we’ve covered a range of scenarios from simple renaming tasks to more complex batch renamings. Armed with these techniques, you should now feel confident in tackling any file or directory renaming challenge that comes your way on the Ubuntu terminal.

Remember, practice makes perfect; experiment with different commands and patterns to fully understand their capabilities and limitations. Happy coding!

Last Modified: 26/11/2015 - 23:12:38