Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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
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 . – Raja Aug 2 '12 at 18:01
based on their name @EliahKagan ,:D – Raja Aug 2 '12 at 18:05
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 . – Raja 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
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

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
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 – jmetz Aug 2 '12 at 18:59

Your Answer


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.