[Techtalk] copying similar files in one command

Finne Boonen hennar at gmail.com
Mon Nov 1 03:12:35 EST 2004


On Sun, 31 Oct 2004 16:55:35 +0100, Magni Onsoien
<magnio+lc-techtalk at pvv.ntnu.no> wrote:
> On 2004-10-31 05:21:38 -0600, percila orphan said:
> 
> 
> > 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?
> 
> You may use wildchars and let your shell take care of finding the files
> for you (this is called file expansion):
> 
> cp *ed* /home/newdir
> 
> will copy all files with *ed* in them; if you also want the directories
> copied, you'll have to use 'cp -r' in stead. And you can also use this
> for other commands, like mv (move) or scp (remote copy via ssh).

you can get better results with more specification, eg

copy all files ending with ed
cp *ed /home/newdir  

copy all files beginning with ed
cp ed* /home/newdir

copy all files ending with ed and beginning with be
cp be*ed /home/newdir


Basically * stands for whatever charachter and multiple ones at that
If there's only one character difference you could try something like

copy ed1 eda ed4 edt edT ....
cp ed? /home/newdir

? stands for exactly one character never mind wich one

If you need more information, you can lookup regular expressions, wich
is what makes the command line so powerfull from time to time.
With regular expression you can specify what kinds of characters
should be in there and how many, and even wich characters should not
be in there and so on and so on.

Finne


More information about the Techtalk mailing list