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
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
Spam Deminer
 
Join Date: May 2008
Posts: 303
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 13-STABLE
Reply With Quote
  #3   (View Single Post)  
Old 28th May 2011
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
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: 1,027
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
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: 1,027
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 05:52 AM.


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