How to search for and replace a string in the file name

As a developer, you may sometimes want to rename a file in a directory by substituting certain string in the name. The following one line tip in the Linux shell will help you to achieve this.

Here, ‘oldname‘ is the search string and ‘newname‘ is the replacement string.

The for loop will iterate and file the ‘file‘ variable with every instance of the files founds. The ‘sed‘ command will replace the found string with a replacement string, and pass it as the second parameter for the ‘mvcommand.

Here is an example of how to rename all files that have the strings ‘oldname‘ in their file names, which need to be replaced with ‘newname‘.

Create dummy a file

Search for and replace a string

Search-Replace Examples




Vijayan J

Thanks for stopping by guys! I’m Vijayan and Techpulse is my beloved brainchild.
Currently working as a PHP developer in a digital marketing start-up, I’m overly passionate about not just learning new things but also putting those into practice. I swear by a quote I once came across…
‘What separates successful people from unsuccessful people is the former’s ability to execute’.
Feel free to reach out to me if you have any questions, suggestions or feedback. Hoping to see more of you here!