Rename all the files in one directory with one command is possible. Adding Suffix and Prefix to all files and directories are also possible. In this post we will see how Add Prefix-Suffix To All Existing Files in Linux Directory.
Table of Contents
Many times admin has to do backup of the files. For some testing purpose admin has to rename the files so that it can be easily identified which files have been changed. To rename or add suffix prefix to couple of files can be done by mv command.
But what if the number of files are in millions ?
Solutions: Command to Add Prefix-Suffix To one Existing Files in Linux Directory
When you have to change couple of files then mv command can be used. An example to change one files is given below. In this example we will change filename.txt to filename-test.txt.
Command to rename of add suffix prefix to one files:
Original File Name – filename.txt
Rename to – filename-test.txt
Command to Add Prefix-Suffix To Existing Files in Linux Directory
mv filename.txt filename-test.txt
Below images show couple of random files. We will add PRE_ prefix to all files.
When the directory has millions of files then we can use following command:
Oroginal File Name – filename.txt
Add Prefix PRE_ to all millions files
Command to Add Prefix-Suffix To All Existing Files in Linux Directory
for f in * ; do mv — “$f” “PRE_$f” ; done
Above screenshot confirms that all files which earlier started with o* are now starting with PRE_.
If the files number are in millions then it will take sometime to reflect and come out.
There are couple of other commands also available. You can use rename command
Use of rename Command
rename command can also be used to rename certain files. Go to the directory location where files need to be renamed and run rename command.
Old File Name- Pre-log.txt
Rename to – Post-log.txt
rename ‘s/^Pre/Post/’ *
Confirm the changes using ls command.
Suggest Cloud Services
Cloud Technology Course- AWS & OCI
Oracle Application DBA and Core DBA Course
Crash Courses for AWS, OCI, DBA Performance
Mock Interview Tips
Blogs ,Free Article & Documents
Web Development & Designing
Cloud Technology Skills have become important and critical. Up skill your knowledge with our Online Sessions on AWS and OCI.
Join our FREE Telegram Group and Part of Knowledge Sharing Group.
1. If you are already using Telegram, just add group name- @suggestcloud
2. If you are not using Telegram, Download from App Store or Google Play and Click