HOW TO RENAME FILES ON THE UBUNTU TERMINAL

How to Rename Files on the Ubuntu Terminal

How to Rename Files on the Ubuntu Terminal

Blog Article


How to Rename Files on the Ubuntu Terminal


Ubuntu, a popular Linux distribution, offers a powerful command-line interface (CLI) that allows users to perform a wide range of tasks efficiently. One common task is renaming files, which can be accomplished using various commands in the terminal. This article will guide you through the process of renaming files on the Ubuntu terminal, providing you with the necessary commands and examples to get started.

Using the mv Command


The most straightforward and commonly used method to rename files in Ubuntu is by using the mv (move) command. The mv command can be used to both move and rename files. The basic syntax for renaming a file using mv is:
mv old_filename new_filename

Example


Suppose you have a file named document.txt and you want to rename it to report.txt. You would use the following command:
mv document.txt report.txt

This command will rename document.txt to report.txt. If a file named report.txt already exists in the directory, the mv command will overwrite it without prompting. To avoid accidental overwriting, you can use the -i (interactive) option:
mv -i document.txt report.txt

The -i option will prompt you to confirm the overwrite if report.txt already exists.

Using the rename Command


For more complex renaming tasks, such as renaming multiple files based on a pattern, the rename command is very useful. The rename command is not installed by default on Ubuntu, so you may need to install it first:
sudo apt-get install rename

Basic Syntax


The basic syntax for the rename command is:
rename 's/old_pattern/new_pattern/' files

Example


Suppose you have multiple files with the .txt extension and you want to change the extension to .md:
rename 's/.txt$/.md/' *.txt

This command will rename all files in the current directory that end with .txt to end with .md.

Using Perl Regular Expressions


The rename command supports Perl regular expressions, which provide powerful pattern matching capabilities. For example, if you want to add a prefix to all files in a directory:
rename 's/^/prefix_/' *

This command will add the prefix prefix_ to the beginning of all files in the current directory.

Batch Renaming with for Loops


For more advanced batch renaming tasks, you can use a for loop in combination with the mv command. This method is useful when you need to perform multiple operations on a set of files.

Example


Suppose you have a directory of images named image1.jpg, image2.jpg, and so on, and you want to add a prefix vacation_ to each file:
for file in image*.jpg; do
mv "$file" "vacation_$file"
done

This loop will rename each file in the directory by adding the prefix vacation_.

Conclusion


Renaming files in the Ubuntu terminal can be a simple or complex task, depending on your needs. The mv command is ideal for single file renames, while the rename command and for loops offer more flexibility for batch renaming tasks. By mastering these commands, you can efficiently manage your files and directories from the command line.

For more detailed information and additional examples, you can refer to the Rename Files on Terminal in Ubuntu guide.

Happy renaming!

Report this page