Bash script copy rename multiple files

In this tutorial, I am written a small program with “Bash script copy rename multiple files” on Linux. Linux the essential for DevOps Roles.

The syntax for the loop

for file in $(ls)
       cp $file ${file/<Pattern>/<Replacement>}; 

For example, I will copy and rename 2 files “foo2017-2.txt and foo2017-3.txt” into “foo2018-2.txt and foo2018-3.txt”  in “folder” folder.

[huupv@huupv devopsroles]$ pwd
[huupv@huupv devopsroles]$ ls folder/
file.txt foo2017-1.txt foo2017-2.txt foo2017-3.txt
[huupv@huupv devopsroles]$

My Bash script copy rename multiple files

#Only 2 copies files from the bottom and change 2017 to 2018

for file in $(ls $FOLDER/ | tail -n2)
   cp ${FOLDER}/${file} ${FOLDER}/${file/2017/2018};

Execute bash script

[huupv@huupv devopsroles]$ chmod +x
[huupv@huupv devopsroles]$ ./ /home/huupv/devopsroles/folder

The result, after you run the bash script

[huupv@huupv devopsroles]$ ls /home/huupv/devopsroles/folder/
file.txt foo2017-1.txt foo2017-2.txt foo2017-3.txt foo2018-2.txt foo2018-3.txt
[huupv@huupv devopsroles]$

The screen output terminal

Bash script copy rename multiple files


Thought the article, How to use “Bash script copy rename multiple files” as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.