Using grep to move files that contain a line of text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using grep to move files that contain a line of text
# 1  
Old 02-24-2009
Using grep to move files that contain a line of text

I have a folder with about 4000 files in it. I need to extract the files that contain a certain line of text to another directory.

for example files with 'ns1.biz.rr.com' need to be extracted to the directory above or some other directory.

I tried using the following but was not successful.

Quote:
grep -l -i -r "biz.rr.com"
# 2  
Old 02-24-2009
Try this:

Code:
$ mkdir new_dir
$ grep -il "biz.rr.com" * | xargs -n1 -i mv {} new_dir

# 3  
Old 02-24-2009
It does actually find the files, but it isn't moving them into the new directory... any other suggestions?


-edit does it matter if the file names end in .db and aren't pure text files?
# 4  
Old 02-24-2009
How do you know it's finding the files ?

Try this and tell me what's the output

Code:
$ grep -il "biz.rr.com" * | xargs -n1 -ti mv {} new_dir

It shouldnt matter if the files end with .db and they're not pure text file
# 5  
Old 02-24-2009
If i remove the section after the Pipe it will list all the files straight down the page is how i found out it was actually finding the files.

I let it run through, it takes about 30 seconds and nothing ever moves into the new directory. It seems like it's the move that's not working for some reason.
# 6  
Old 02-24-2009
Can you copy the output of the last command I asked you to try ?

At least part of it (in case its too long)
# 7  
Old 02-26-2009
$ for i in `grep -iH "bizz.rr.com" *; do mv $i /new_directory; done

... will work as well Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to grep and print portions of a very loooong line of text.?

Hi, A bit stumped here trying to grep and wanting to display portion of a text Using the grep below, I am able to find the line containing the string select ~]$ ~]$ srvctl -h | grep -i "select" Usage: srvctl add service -db <db_unique_name> -service <service_name> {-preferred... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All It's me again with another huge txt files. :confused: What I have: - I have 33 huge txt files in a folder. - I have thousands of line in this txt file which contain many the letter "x" in them. - Some of them have more than one "x" character in the line. What I want to achieve:... (8 Replies)
Discussion started by: Nexeu
8 Replies

3. Shell Programming and Scripting

Move a text to next line in a file

Hi , I need your help for the below issue. I have a file which has data as below An error came (/u01/app/12.csv) pkg1.func1: detail s 1111-->pkg1.func1: detail s 2222--> Now pkg1.func1: .... --> can come multiple times in the second line. I need to arrange the data in the below... (9 Replies)
Discussion started by: bhaski2012
9 Replies

4. Shell Programming and Scripting

grep a line from a text file

Hi all, I need to grep a line from a log file which ensures me that the application server script is executed successfully. Some body please help me on this. I also need to write a while loop in which i need to use the status of the above grep output. Could some one please tell me how to use... (12 Replies)
Discussion started by: firestar
12 Replies

5. Shell Programming and Scripting

Unix Script To Move Files Based On Grep

I am looking for advice on how to write a script that will rename and/or move files to a different directory based upon the results of a grep. Let's say I have ten files in a directory. Some of them - not all - contain the text 'HELLO'. I would like to be able to grep the files for that text,... (3 Replies)
Discussion started by: rjhjr64
3 Replies

6. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

7. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies

8. Shell Programming and Scripting

grep files without header and move to new dir

Hi, i have number of files in a directory to be processed. the problem is some of the files does not have a header and the process is giving an error of no header found. example of good file : file1 HDR|20080803233401 record 1 record 2 TRA|2 example of a bad file in the same dir ... (6 Replies)
Discussion started by: sitaldip
6 Replies

9. Shell Programming and Scripting

Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier. What I would like to do is have each line that looks like this: setmqaut -m... (4 Replies)
Discussion started by: sysera
4 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question