Technology and General Blog

This tutorial will focus on a brief way to use Bash to rename information from a specific extension to a further. We will use a bash loop, find, rename, and the mv command for this a single.

Approach 1: Bash loop

The most widespread way to alter file extensions recursively in a directory is to use a bash for loop. We can prompt the consumer to enter the target listing, aged extension, and the new extension to rename working with a bash script.

Phase 1: Assemble the script
Permit us get started assembling the script. The very first section we will need is to get the concentrate on directory. For this, we can bash go through as:

echo “Enter the target listing “
study concentrate on_dir
cd $goal_dir
echo “Enter the file extension to lookup without having a dot”
examine aged_ext
echo “Enter the new file extension to rename to without the need of a dot”
go through new_ext
echo $goal_dir, $outdated_ext, $new_ext

The script over will check with the consumer for the directory to approach and then cd into the established directory.

Future, we get the outdated extension without the need of the dot (.) lastly, we get the new extension to rename the documents.

Now enable us get to processing the information. For this we can implement a straightforward rule that recursively searches the files as:

for file in *.$old_ext
    mv -v $file $file%.$aged_ext.$new_ext
carried out

The for loop earlier mentioned will search the passed directory for all data files with the previous extension and rename them to the new extension.

To get verbose, we use the mv command with -v. You can suppress this output by changing the -v flag with –

Move 2: Operate the script
Now, permit us place the script to the check. The remaining script is under:

echo “Enter the goal directory “
study concentrate on_dir
cd $goal_dir
echo “Enter the file extension to research devoid of a dot”
read aged_ext
echo “Enter the new file extension to rename to with no a dot”
go through new_ext
echo $concentrate on_dir, $old_ext, $new_ext
for file in *.$old_ext
    mv -v $file $file%.$old_ext.$new_ext

In this examination, we will use the /var/log directory and rename all the .log data files to .bak. Below are the contents of the listing in advance of working the script.

$ ls l /var/log/ | grep .log

Now, let us operate the script.

$ chmod +x
$ sudo ./extensions

The screenshot over shows the script processing the information and renaming all the files with .log to .bak.

Since this is an interactive script, it comes in useful when you do not want to challenging code the extension.

The following is the contents of the /var/log listing just after the script.

$ ls -l /var/log/ | grep .bak

To revert the alterations, change the old extension to .bak and the new extension as .log

Technique 2: Rename command

If you do not experience like operating with a script, you can use the rename device to change the file extensions recursively.

To set up rename, use the command:

$ sudo apt-get set up rename -y

After mounted, you can use the rename command as:

# adjust to the focus on listing
cd /var/log/
# alter extension
sudo rename ‘s/.log/.bak/’ *.log

To revert the changes, modify the .bak to .log and vice versa.

$ sudo rename ‘s/.bak/.log/’ *.bak

Process 3: MMV command

You can also use the mmv command that lets you to shift numerous data files at the same time.  Install mmv with the command:

$ sudo apt-get install mmv

To rename data files with mmv command:

$ cd /var/log/
mmv “*.csv” “#1.xls”

The #1 moves the files to the latest listing. When you run the command, it will rename all .log files to the specified extension.

Summing Up

This short article talked about various techniques you can recursively rename file extensions in a certain directory. Nevertheless, it is great to note that you can carry out procedures other than these mentioned in this tutorial.

Thank you for looking at, and recall to share!

Leave a Reply

Your email address will not be published. Required fields are marked *