shell script to search and copy files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to search and copy files
# 1  
Old 04-14-2010
shell script to search and copy files

Hello

Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario:
I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new directory.

find . | xargs grep "TEST"
I use this file command in order to find the files that i need that have the word "test" inside. I need to run this from root in order to find all the files containing this word and then something that automaticaly copies all the files on a new directory.


Any idea?

Best Regards
# 2  
Old 04-14-2010
Try this:
Code:
find . -print0 |xargs -0 grep -l 'TEXT' | xargs -I '{}' cp {} /dest/dir

# 3  
Old 04-14-2010
Hi Franklin52. Could you explain this part

Code:
xargs -0 grep -l 'TEXT'

I don't understand what exactly is doing -0 and I can't find it with man
# 4  
Old 04-14-2010
Quote:
Originally Posted by kcoder24
Hi Franklin52. Could you explain this part

Code:
xargs -0 grep -l 'TEXT'

I don't understand what exactly is doing -0 and I can't find it with man
It's an option of the GNU version, the lines are terminated by a null character instead of a space.
If you have another version it can be ommited but it works only with file names without spaces:
Code:
find . -type f|xargs grep -l 'TEXT' | xargs -I '{}' cp {} /dest/dir

# 5  
Old 04-14-2010
Ok thanks Franklin52 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the previous month files using shell script?

could you please assist the below query. i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied. Please find the below directory structure:- ls -lrt total 1824... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

3. Shell Programming and Scripting

Shell script to copy files from on folder to another

I am trying to copy files with specific date and name to another folder. I am very new to shell scripting so i am finding it hard to do that. see the sample code i have written below. srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"... (13 Replies)
Discussion started by: Aqeel Abbas
13 Replies

4. UNIX for Dummies Questions & Answers

Copy files from Linux server local windows machine using a shell script

Hello, I need to create a shell script which will copy files - which are created on particular date and starting with particular name - to local windows XP machine. Is this possible.? Currently it is being done manually using winscp (1 Reply)
Discussion started by: NarayanaPrakash
1 Replies

5. Shell Programming and Scripting

Need Shell Script to copy files from hp UNIX to windows server 2008

Need Shell Script to copy files from hp unix to windows server 2008 I tried to google and found some options but nothing worked I want a script to copy a file in unix to windows server so I can schedule the script on daily basis.Please help me its more needful for me. Let me know if any... (4 Replies)
Discussion started by: Lucky2Bv
4 Replies

6. UNIX for Dummies Questions & Answers

Script to search and copy files

HI everyone, I been to this site before for help and found my answers on other threads now I am posting my own :). I have a list of file names with out extensions on an txt file. I need a way for the script to search on the server for each file name and copy the files over to a new directory.... (12 Replies)
Discussion started by: sergiol
12 Replies

7. Shell Programming and Scripting

Need a shell script which takes two inputs and copy the files from one directory to other

Hi, I am using solari 10 OS which is having bash shell. I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory. example:I hava a machine1 which is having some files in a... (8 Replies)
Discussion started by: muraliinfy04
8 Replies

8. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

9. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

10. Shell Programming and Scripting

Urgent !!! Shell script to copy files to VSS

Hi all !!! I need this sample script urgently. Please help. Suppose I do an "ls -ltr" in a directory and store the output in a text file (say "a.txt"). What I need to do is to write a shell script which reads file-names from "a.txt" and copies only those files to VSS. Let's say the destination... (1 Reply)
Discussion started by: devalin
1 Replies
Login or Register to Ask a Question