|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
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 Code:
cp | grep name\ to\ search How do I pipe grep to cp to transfer files based on name? |
|
|||
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 % disclaimer UNTESTED.
__________________
FreeBSD 13-STABLE |
|
|||
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 |
|
|||
Quote:
@ocicat: I feel so stupid... It didn't even cross my mind to write a script. |
Tags |
cp, grep |
|
|
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 |