[Techtalk] copying similar files in one command

Terri Oda terri at zone12.com
Mon Nov 1 04:11:24 EST 2004


On Oct 31, 2004, at 10:48 AM, Terri Oda wrote:
> On Oct 31, 2004, at 6:21 AM, percila orphan wrote:
>> I need help with the cp command.
>> Sometimes I'm have to copy a lot of files that
>> have something in thier names in common.
>> For example, if they all have the word "ed" in them.
>> /home/ed/ ls -l | grep "ed"  displays all the files with the word
>> "ed" in them within that directory /home/ed/
>> I want to copy those files to the directory  /usr/temp
>> Can I copy them all with one command? What might
>> that be?
>
> It's not *exactly* one command, but assuming you're in the bash shell, 
> I'd use a loop as follows:
>
> for file in /home/ed/*ed*; do cp $file /usr/temp; done

I should mention that the reason I like loops is that I often want to 
do more complex things, like, for example, rename each file rather than 
move everything to a directory, or even just produce output telling me 
the name of each file I moved.

But if you're just using cp to do what you described, it does make more 
sense to just cp /home/ed/*ed* /usr/temp :)




More information about the Techtalk mailing list