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 in Linux Administration by kal
• 120 points
79 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 by Omkar
• 67,600 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 by Gitika
• 25,340 points

edited Jul 2 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 in Linux Administration by Ritesh Singh
5,461 views
0 votes
1 answer
0 votes
1 answer
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 in Linux Administration by Rishi
98 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 in Linux Administration by DareDev
• 6,810 points
40 views
0 votes
1 answer
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 in Linux Administration by Omkar
• 67,600 points
33 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 in Linux Administration by Omkar
• 67,600 points
100 views