Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Using Ubuntu 12.04 I need help on copying files that were returned from a search with grep .

I have many pdf files, so to filter specific named files I am using General grep command as follows.

raja@badfox:~/Desktop/mtech/M.Tech II Sem-April 2012$ grep "DESIGN OF FAULT TOLERANT SYSTEMS" *.pdf
Binary file NR-DESIGN OF FAULT TOLERANT SYSTEMS.pdf matches
Binary file R09-DESIGN OF FAULT TOLERANT SYSTEMS.pdf matches

How do I automatically copy the files returned from the search to another directory?

share|improve this question
1  
Do you want to copy files based on the name of the file? (Then just do it this way.) Or do you want to copy all files that contain a certain string? Also, remember that if a string appears in a PDF when you view it, that string will often but not always appear as a contiguous sequence of characters in the PDF file itself, as there may be formatting in the string that breaks it up. –  Eliah Kagan Aug 2 '12 at 17:55
    
@Ankit as i mentioned only a specific files , not all . –  raaz Aug 2 '12 at 18:01
    
based on their name @EliahKagan ,:D –  raaz Aug 2 '12 at 18:05
add comment

2 Answers

up vote 5 down vote accepted

An easier way to do it would be to just use cp.

cp *"DESIGN OF FAULT TOLERANT SYSTEMS"*.pdf /some/folder/
share|improve this answer
    
this is great and simple . –  raaz Aug 2 '12 at 17:55
    
Thanks. As tgm4883 answered, you can use grep, but it is a lot harder to remember the commands to do so if you aren't using the advanced features of grep. –  reverendj1 Aug 2 '12 at 17:58
1  
Also, grep (as used in the question) looks in the files, not at their names. A file won't always contain its own name. –  Eliah Kagan Aug 2 '12 at 18:04
    
I've deleted my other answer as this one is the best for what the user wants to do, and it's far more work to even correct mine to give the user what he wants. –  tgm4883 Aug 2 '12 at 18:11
add comment

Another way to do this in general when you are searching for files is in conjunction with the find command, e.g.

find -name "*DESIGN OF FAULT TOLERANT SYSTEMS*.pdf" -exec cp '{}' /some/folder/ \;

which combines the searching and copying (generally any action you want to perform on the files) into one command.

If you wanted to limit the search to the current directory you could add the -maxdepth 1 option to the find command.

find has many options for specifying which paths to search or not search, including checking modification times, path specifiers, filenames, and many more. Used correctly it's an extremely powerful tool.

share|improve this answer
1  
Note - this will copy files found in the current directory and any subdirectory. So, for example if you have archived versions of the DESIGN* PDF files in a subdir, those would be found and copied to the target location. –  Code Friendly Aug 2 '12 at 18:39
    
@CodeFriendly - Cheers, updated the post –  mutzmatron Aug 2 '12 at 18:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.