LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2011, 04:40 PM   #1
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Rep: Reputation: 0
How To Remove Words/Text In A Directory?


Here is my problem.
I have a folder with many many files. e.g html, docs, excel sheet, script etc.
Now I want to find {using grep command}a certain word in that folder/directory and delete it in all the files and scripts that have it.

For example, I want to delete the word /testing (with the slash) in all files in a directory.

Thank you,
 
Old 02-24-2011, 05:15 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
You can use the following:

Code:
find /home/user -name "/testing" -type f | xargs rm
Just needs to be modified to the directory you want to search in.
 
Old 02-24-2011, 06:25 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
This would remove all the files with "/testing" in the names, but the OP wants to remove the word "/testing" form the files contents.

@prayingtosky: I think that it should work with sed. Try something like this:
Code:
sed 's/\/testing//g' *
I am new to sed, but I think that it should work, at least with normal text-files. Try
Code:
man sed
for more info.
I know nothing about the interns of the .doc and Excel-files, but I doubt that this approach will work.

Edit: OK, this will only print the contents of the files with "/testing" removed, but will not alter the files.
Have to triy it with a for-loop, will post that later.

Last edited by TobiSGD; 02-24-2011 at 06:31 PM.
 
Old 02-24-2011, 06:33 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
The best way would be using grep IMO

Code:
grep -r "/testing" /home/directory | xargs rm
 
Old 02-24-2011, 07:37 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
How about:
Code:
sed -i 's@/testing@@g' *
If you wish to test on a file first to see if it works you can do:
Code:
sed -i.bak 's@/testing@@g' test_file
test_file.bak will be the backup of the original file
 
Old 02-25-2011, 04:29 AM   #6
mayursingru
Member
 
Registered: Nov 2010
Location: Pune
Distribution: CentOS
Posts: 51

Rep: Reputation: 5
try this
replace "string u want to replace" "string u want in replaced position" -- filename
in u case it would be
replace testing " " -- filename
 
Old 02-25-2011, 11:26 AM   #7
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
The best way would be using grep IMO

Code:
grep -r "/testing" /home/directory | xargs rm
I tried this but got error

rm: missing operand

Thank you.
None of the above work by the way.
 
Old 02-25-2011, 11:29 AM   #8
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
You can use the following:

Code:
find /home/user -name "/testing" -type f | xargs rm
Just needs to be modified to the directory you want to search in.

tried and got this error.

find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name `/physics'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `/physics''.
rm: missing operand
Try `rm --help' for more information.
 
Old 02-25-2011, 11:32 AM   #9
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by prayingtosky View Post
I tried this but got error

rm: missing operand

Thank you.
None of the above work by the way.
Hmm, most likely grep isn't passing the file name to xargs through the pipe. What results do you get when you do just find /home/user -name "/testing" -type f?
 
Old 02-25-2011, 11:38 AM   #10
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Code:
find /home/user -name "/testing" -type f -exec rm -i {} \;
Would this be more up your alley? I completely forgot about the exec flag. Sorry!

Edit: Added -i to be interactive just in case.
 
Old 02-25-2011, 12:44 PM   #11
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
I don't get it.
What is you want to achieve? Is it a) delete all files that have /testing in their names, or b) remove the word /testing from the content of all files in a specific directory, or c) delete all the files that have the word /testing in their content?
 
Old 02-25-2011, 03:44 PM   #12
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TobiSGD View Post
I don't get it.
What is you want to achieve? Is it a) delete all files that have /testing in their names, or b) remove the word /testing from the content of all files in a specific directory, or c) delete all the files that have the word /testing in their content?
I would be the b) remove the word /testing from the content of all files in a directory.

I'll get back after trying the options above.
 
Old 02-25-2011, 04:07 PM   #13
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
All the options with a "rm" in the command are useless to you, they will only work for option a) and c)
 
Old 02-25-2011, 04:07 PM   #14
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
Hmm, most likely grep isn't passing the file name to xargs through the pipe. What results do you get when you do just find /home/user -name "/testing" -type f?
Here is the error
find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name `/physics'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `/physics''.
 
Old 02-25-2011, 04:08 PM   #15
prayingtosky
LQ Newbie
 
Registered: Dec 2010
Location: New York, USA
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
Code:
find /home/user -name "/testing" -type f -exec rm -i {} \;
Would this be more up your alley? I completely forgot about the exec flag. Sorry!

Edit: Added -i to be interactive just in case.
I got the same error as above.

find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name `/physics'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `/physics''.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove words in a file using sed or any other command Kilam orez Linux - Newbie 4 11-30-2009 09:52 AM
Command to delete words out of a text file. Dazamondo Linux - Newbie 13 06-30-2009 11:48 AM
Replacing words in a text file Raghavan_sat Programming 3 05-27-2008 04:11 PM
bash : remove replicator words?, how? Guru Mind Programming 13 05-18-2006 01:24 AM
How do I remove words from ispell dictionary.hash? geokker Linux - Software 1 04-20-2006 05:55 AM


All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration