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.
Requirement Analysis
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.
Example:
Old File Name- Pre-log.txt
Rename to – Post-log.txt
Command:
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
Job Assistance
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
Suggest Cloud
Does not work —
DW$ ls -l
total 388252
-rw-r–r– 1 sshrivastav sshrivastav 58236527 Jun 9 17:56 sharda_0520_acct_call_attempt_sm.sam.gz
-rw-r–r– 1 sshrivastav sshrivastav 50280849 Jun 9 17:57 shardb_0520_acct_call_attempt_sm.sam.gz
-rw-r–r– 1 sshrivastav sshrivastav 142767776 Jun 9 18:02 shardc_0520_acct_call_attempt_sm.sam.gz
-rw-r–r– 1 sshrivastav sshrivastav 146273070 Jun 9 18:06 shardf_0520_acct_call_attempt_sm.sam.gz
DW$ for f in * ; do mv — “$f” “na4$f” ; done
mv: target ‘“na4sharda_0520_acct_call_attempt_sm.sam.gz”’ is not a directory
mv: target ‘“na4shardb_0520_acct_call_attempt_sm.sam.gz”’ is not a directory
mv: target ‘“na4shardc_0520_acct_call_attempt_sm.sam.gz”’ is not a directory
mv: target ‘“na4shardf_0520_acct_call_attempt_sm.sam.gz”’ is not a directory
for f in * ; do mv — “$f” “na4$f” ; done
Try this and this applies to all existing files.
what script you have used? can you please let me know and paste here
You should try this: for f in * ; do mv — “$f” “na4$f” ; done
This applies to all existing FILES.