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
385 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,110 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,870 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
16,569 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,140 points
83 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
157 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
5,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
2,917 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,890 points
245 views
0 votes
2 answers

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

Sorry in advance for any formatting. Check out ...READ MORE

answered Feb 7 in Linux Administration by anonymous
8,089 views
0 votes
1 answer

How do i enable universe repository in Ubuntu 18.04?

If you want in one command and ...READ MORE

answered Aug 27, 2019 in Linux Administration by Sirajul
• 59,030 points
3,986 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 Jan 31, 2019 in Linux Administration by Omkar
• 69,110 points
242 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,110 points
614 views