15 49.0138 8.38624 1 0 4000 1 https://www.techpulsetoday.com 300
search-replace

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.

for file in $LIST; do newname=`echo $file|sed s/oldname/newname/g`; echo "change $file to $newname";mv $file $newname; done;

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

touch oldname.txt oldname.c oldname-test.c test-oldname.c

Search for and replace a string

export LIST=`ls`
for file in $LIST; do newname=`echo $file|sed s/oldname/newname/g`; echo "change $file to $newname";mv $file $newname; done;

Search-Replace Examples

[email protected]:~/Downloads/techpulsetoday$ touch oldname.txt oldname.c oldname-test.c test-oldname.c
[email protected]:~/Downloads/techpulsetoday$ ls
oldname.c  oldname-test.c  oldname.txt  test-oldname.c
[email protected]:~/Downloads/techpulsetoday$ export LIST=`ls`
[email protected]:~/Downloads/techpulsetoday$ for file in $LIST; do newname=`echo $file|sed s/oldname/newname/g`; echo "change $file to $newname";mv $file $newname; done;
change oldname.c to newname.c
change oldname-test.c to newname-test.c
change oldname.txt to newname.txt
change test-oldname.c to test-newname.c
[email protected]:~/Downloads/techpulsetoday$ ls
newname.c  newname-test.c  newname.txt  test-newname.c
[email protected]:~/Downloads/techpulsetoday$ ls -lash
total 8.0K
4.0K drwxrwxr-x  2 vijayan vijayan 4.0K Dec 25 20:48 .
4.0K drwxr-xr-x 19 vijayan vijayan 4.0K Dec 25 20:47 ..
   0 -rw-rw-r--  1 vijayan vijayan    0 Dec 25 20:48 newname.c
   0 -rw-rw-r--  1 vijayan vijayan    0 Dec 25 20:48 newname-test.c
   0 -rw-rw-r--  1 vijayan vijayan    0 Dec 25 20:48 newname.txt
   0 -rw-rw-r--  1 vijayan vijayan    0 Dec 25 20:48 test-newname.c
[email protected]:~/Downloads/techpulsetoday$

 

 

 

Share:
Category:Linux
PREVIOUS POST
How to avoid sudo password prompt
NEXT POST
The requested url /phpmyadmin/ was not found on this server
ABOUT
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!

0 Comment

LEAVE A REPLY