How do I find all files containing specific text and rename in unix linux

0 votes

I need to find all the files which contain a specific word like 'time' in a particular path and rename the file like "filename_time"

Jul 2, 2019 in Linux Administration by kal
• 120 points
2,774 views

2 answers to this question.

+1 vote

You can write a bash script to do this. Open a terminal, and create a bash file (a file with .sh extension).

$ gedit change.sh

Then add the following lines in the file

rename 's/time/filename_time' *

Save and exit the file.

Change permission of the file:

$ chmod 777 change.sh

Then execute the file:

$ ./change.sh

Filename should be changed.

answered Jul 2, 2019 by Omkar
• 69,220 points
+1 vote

Hi, @Kal

You can use this command below to find out the particular/specific text from all the files:

$ grep "search_string" /path/to/dir

Then you can use this command below to rename that specific text in all the files:

$ grep -rl old string - / xargs sed -i -e 's/old string/new string/'

Try this, it should work. Just let me know if it works. 

answered Jul 2, 2019 by Gitika
• 65,770 points

edited Jul 2, 2019 by Gitika

Hi Gltka.. I am  trying to identify all files that contain a word like 'one' in all files of a directory  and  rename the filename appending with 'one'.

Example  directory path roor/k 

files t.txt ,t1.txt , t2.txt

cat t.txt out put qqqq,onexxxx ;cat t1.txt out put qqqq,one yyyy ;cat t3.txt out put qqqq,two xxxx

expected out put t_one.txt t1_one.txt

I am able to use below command and move the files but need help in renaming them.

grep -lir 'one' /root/k/' 'root/d/'

Hi @Kal, 

Did you check the different mentioned suggestion which is suggested above? 

Related Questions In Linux Administration

0 votes
2 answers

How do I find all files containing specific text on Linux?

grep -rnw 'text to find recursively inside ...READ MORE

answered May 20, 2019 in Linux Administration by Ritesh Singh
40,474 views
0 votes
1 answer

How to find all files containing specific text on Linux?

Hi@akhtar, Linux systems have one interesting command name grep. ...READ MORE

answered Sep 22, 2020 in Linux Administration by MD
• 95,460 points
1,344 views
0 votes
0 answers

How do I copy folder with files to another folder in Unix/Linux?

I am having some issues copying a ...READ MORE

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
392 views
0 votes
0 answers

How do I copy folder with files to another folder in Unix/Linux?

I am having some issues to copy ...READ MORE

Apr 21, 2022 in Linux Administration by Edureka
• 13,690 points
420 views
0 votes
1 answer

How can I find out which files differ in two directory trees?

Use this; diff -qr dir1/ dir2/ READ MORE

answered Jun 13, 2019 in Linux Administration by Upasana
• 8,620 points
810 views
0 votes
1 answer

How to Unblock SSH and FTP Access to Specific IP in Linux?

Firewalls(most of them) can either Reject or ...READ MORE

answered Jul 16, 2019 in Linux Administration by Archana
• 5,640 points
15,368 views
0 votes
0 answers

How do i select correct active serial port in linux?

I am trying to communicate with a ...READ MORE

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
592 views
0 votes
0 answers

How do I output coloured text to a Linux terminal?

How do I print coloured characters to ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,690 points
449 views
0 votes
1 answer

How to change permissions of folder and files in folder?

The chmod command is used to set/change the permissions ...READ MORE

answered Feb 1, 2019 in Linux Administration by Omkar
• 69,220 points
2,012 views
–1 vote
1 answer

How to get octal file permission in linux?

You can use this: stat -c "%a %n" ...READ MORE

answered Jan 3, 2019 in Linux Administration by Omkar
• 69,220 points
2,625 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP