DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 28th May 2011
Daffy Daffy is offline
Fdisk Soldier
 
Join Date: Jun 2010
Posts: 73
Thanked 0 Times in 0 Posts
Default Pipe grep to copy files based on name.

I just finished restoring some files from a friend's hard disk. All files are now into a folder and I need to transfer some of them.

While it's easy to copy files with a certain extension, I cannot understand how I can copy them using the filenames.

To select files to copy based on the extension, I just use cp *.(extension) /destination/folder/.

To list items based in name, I used
Code:
ls | grep name\ to\ search
I think that it will be something like
Code:
cp | grep name\ to\ search
but where do I specify the destination folder?

How do I pipe grep to cp to transfer files based on name?
Reply With Quote
  #2   (View Single Post)  
Old 28th May 2011
jb_daefo jb_daefo is offline
Package Pilot
 
Join Date: May 2008
Posts: 201
Thanked 5 Times in 5 Posts
Default

Code:
find . -type f -name "some*.fil" -exec cp -iv {}  /destination/folder \;
# or less likely...
find . -type f -name "some*.fil" -print0 | xargs -J % cp -iv  /destination %
The second might fail if cp does not handle the incoming data with the addtion of a parameter, in other words it might be missing an item in the line to not fail.
disclaimer UNTESTED.
__________________
FreeBSD v9... pkg2ng somewhat working...
Reply With Quote
  #3   (View Single Post)  
Old 28th May 2011
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,935
Thanked 190 Times in 160 Posts
Default

While I agree that a solution using find(1) is much more elegant, you can also fall back to simple shell scripting to accomplish the same thing:
Code:
#!/bin/sh
for f in $(ls | grep foo) ; do
    echo "cp $f /dest"
done
  • Place the above code into a text file & execute as an argument to sh(1) or make the file executable through chmod(1).
  • Once satisfied that the output constructs the needed command, remove echo & the double-quotes.
Reply With Quote
  #4   (View Single Post)  
Old 28th May 2011
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 578
Thanked 14 Times in 13 Posts
Default

Something like

% cp *foo* /dest

works for me (unless it's going to overflow the command line). Replace the pattern *foo* as needed.
Reply With Quote
  #5   (View Single Post)  
Old 28th May 2011
Daffy Daffy is offline
Fdisk Soldier
 
Join Date: Jun 2010
Posts: 73
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by IdOp View Post
Something like

% cp *foo* /dest

works for me (unless it's going to overflow the command line). Replace the pattern *foo* as needed.
Thank you! Well, that was easy.

@ocicat: I feel so stupid... It didn't even cross my mind to write a script.
Reply With Quote
  #6   (View Single Post)  
Old 28th May 2011
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 578
Thanked 14 Times in 13 Posts
Default

Daffy, glad it worked easily for you.

That said, each of the suggested approaches has its strong and weak points. If you're feeling inquisitive, some things to think about would be:

1) What happens in each approach if nothing matches the pattern?

2) What happens if a directory matches the pattern?

3) How many times is the cp command run, each way?

4) What other goodies can be gleaned from the man pages of your implementation of cp, find and your shell?
Reply With Quote
Reply

Tags
cp, grep

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot copy large files to Flash Drive sharris FreeBSD General 6 30th July 2010 09:57 AM
Copy w/ active verification Weaseal FreeBSD General 4 5th February 2009 12:23 AM
How to copy FBSD installation from one which is already installed on a HDD? padmanabh FreeBSD Installation and Upgrading 2 7th October 2008 04:09 AM
how to copy a directory. bsdnewbie999 OpenBSD General 1 12th July 2008 02:36 PM
fstat | grep internet | grep -v -e '>' -e '<' mfaridi OpenBSD General 4 10th June 2008 09:58 PM


All times are GMT. The time now is 10:14 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick