This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9352|Reply: 1

[linux] How to rename files in batches in Linux

[Copy link]
Posted on 2/9/2022 3:57:49 PM | | |
1. Use the rename command

SYNOPSIS
rename from to file...
from indicates the characters that need to be replaced or processed, such as the file extension, the file name.

to indicates the result after processing from.

file represents the target file.


[root@liujianfan tmp]# ls
hello_10_2016-03-17.log  hello_4_2016-03-17.log  hello_8_2016-03-17.log
hello_1_2016-03-17.log   hello_5_2016-03-17.log  hello_9_2016-03-17.log
hello_2_2016-03-17.log   hello_6_2016-03-17.log
hello_3_2016-03-17.log   hello_7_2016-03-17.log
Use rename to change .log to .jpg

[root@liujianfan tmp]# rename ".log" ".jpg" *
[root@liujianfan tmp]# ls
hello_10_2016-03-17.jpg  hello_4_2016-03-17.jpg  hello_8_2016-03-17.jpg
hello_1_2016-03-17.jpg   hello_5_2016-03-17.jpg  hello_9_2016-03-17.jpg
hello_2_2016-03-17.jpg   hello_6_2016-03-17.jpg
hello_3_2016-03-17.jpg   hello_7_2016-03-17.jpg
2. Use sed:

[root@liujianfan tmp]# ls|sed -nr "s#(^.*[0-9].) (.*)#mv & \1log#gp"
mv hello_10_2016-03-17.jpg hello_10_2016-03-17.log
mv hello_1_2016-03-17.jpg hello_1_2016-03-17.log
mv hello_2_2016-03-17.jpg hello_2_2016-03-17.log
mv hello_3_2016-03-17.jpg hello_3_2016-03-17.log
mv hello_4_2016-03-17.jpg hello_4_2016-03-17.log
mv hello_5_2016-03-17.jpg hello_5_2016-03-17.log
mv hello_6_2016-03-17.jpg hello_6_2016-03-17.log
mv hello_7_2016-03-17.jpg hello_7_2016-03-17.log
mv hello_8_2016-03-17.jpg hello_8_2016-03-17.log
mv hello_9_2016-03-17.jpg hello_9_2016-03-17.log
Finally, pipe the bash process:

[root@liujianfan tmp]# ls|sed -nr "s#(^.*[0-9].) (.*)#mv & \1log#gp"|bash
[root@liujianfan tmp]# ls
hello_10_2016-03-17.log  hello_4_2016-03-17.log  hello_8_2016-03-17.log
hello_1_2016-03-17.log   hello_5_2016-03-17.log  hello_9_2016-03-17.log
hello_2_2016-03-17.log   hello_6_2016-03-17.log
hello_3_2016-03-17.log   hello_7_2016-03-17.log
3. Use for and then replace the string

[root@liujianfan tmp]# vim rename.sh
#!/bin/bash
#This script is use to rename files
for name in `ls *.log`;
do
echo "mv $name ${name/.log/.txt}"
done
Finally via bash:

[root@liujianfan tmp]# ./rename.sh |bash
[root@liujianfan tmp]# ls
hello_10_2016-03-17.txt  hello_4_2016-03-17.txt  hello_8_2016-03-17.txt
hello_1_2016-03-17.txt   hello_5_2016-03-17.txt  hello_9_2016-03-17.txt
hello_2_2016-03-17.txt   hello_6_2016-03-17.txt  rename.sh
hello_3_2016-03-17.txt   hello_7_2016-03-17.txt
Original:The hyperlink login is visible.




Previous:There are three ways that Java Maven projects reference local jar packages
Next:Java JDBC's rewriteBatchedStatements, allowPublicKeyRetrieval...
Posted on 2/13/2022 4:32:38 PM |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com