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
127 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,000 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
• 26,590 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
8,924 views
0 votes
1 answer
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,600 points
1,374 views
0 votes
1 answer

How to find path of software installed in Linux?

There are multiple ways to find the ...READ MORE

answered May 19, 2019 in Linux Administration by Rishi
559 views
0 votes
1 answer

How to find the group associated with a user in linux?

To list all the groups groups or to list ...READ MORE

answered Jun 20, 2019 in Linux Administration by DareDev
• 6,850 points
99 views
0 votes
1 answer

How do I use a shell script to SSH in to a remote machine to execute commands?

Your first problem can be solved like ...READ MORE

answered Jun 27, 2019 in Linux Administration by ajs3033
• 7,280 points
198 views
0 votes
1 answer
+1 vote
3 answers
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 Jan 31, 2019 in Linux Administration by Omkar
• 69,000 points
68 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,000 points
201 views