How to rename files in bulk with commands

Mondo Technology Updated on 2024-03-07

I believe that everyone, whether it is a programmer or a white-collar worker in other industries, will encounter a need when using a computer to work, that is, to rename a large number of files with a certain number of regular names.

For programmers, in a Linux environment, we can use some command-line tools to accomplish this.

For example, there are log files as follows:

root@linux:~/test# ls test-*

test-10.log test-1.log test-2.log test-3.log test-4.log test-5.log test-6.log test-7.log test-8.log test-9.log

We need to change the suffix of these files from log to txt, then we can use itrenameCommand:

rename -v 's/\.log$/\.txt/' test*.log

Here's how it works:

The -v parameter is used to print the name of the file that was successfully renamed, which is actually the process record of the output processing, which can be used to verify whether the renaming process meets the requirements. 's/\.log$/\.txt/'is a perl regular expression, which means that a string is filled with .The part at the end of the log is replaced with .txt。

Or it can be usedmvcommands in conjunction with a for loop

for name in `ls test*.log`; do mv $name $.txt ; done

Here's what the command looks like:

In the loop body, we use the $ string treatment, which means that the same as the . is deleted from the end of the namelog matches the smallest part and returns the remainder; After that, add .txt suffix. By doing this, we can change the filename suffix from .log totxt。Finally, we use the mv command to actually rename the file.

If we come across these files like this:

root@linux:~/test# ls test-*

test-10-0eb22520 test-2-09e36680 test-4-0b189764 test-6-0c4bd416 test-8-0d7ebb96

test-1-0949b5c6 test-3-0a7d3e36 test-5-0bb2609c test-7-0ce5454c test-9-0e189874

We need to get rid of the last dash - and the characters after it (in this case, 8 characters), then we need to use itsedCombined with the for loop

for file in `ls test-*`do

newname=$(echo $file | sed 's/.\$//')

mv $file $newname

done

Here's what the command looks like:

For those who use Windows to work, you can also use the command tool to rename in batches. For example, there are many jpeg files in the test directory under the C drive

The filename suffix needs to be renamed to jpg, and we can open the cmd window,

cd \

cd test

ren *.jpeg *.jpg

The effect after execution is as follows:

In the cmd window, we can use the command help ren or ren ?to see usage tips.

Reference: Related Reading: What does 2>&1 mean in shell.

How to write a bash script to restart a process if it freezes.

How to pass parameters to an interactive script in a non-interactive manner.

Related Pages

    How to use common FTP commands

    FTP File Transfer Protocol is a protocol used to transfer files between computers.When using FTP,commonly used commands include login,upload,and delet...

    Common commands of Ruijie switches

    enable to enter privileged mode.exit returns to the previous mode of operation end returns to privileged mode copy running config startup config to sa...

    How to use dental floss?

    We brush our teeth very carefully every day,what is the focus of brushing?Oral hygiene is all about removing plaque thoroughly.However,brushing alone ...

    How ArcGIS assigns values to fields in bulk

    Batch assignment of values to fields is a very common operation in ArcGIS,especially when it comes to data processing and analysis.Batch assignment si...

    How do I sign up for multiple Google accounts in bulk?

    Email addresses are essential for accounts,but they can be misused to send spam.That s why Google uses advanced algorithms to detect suspicious regist...