[Techtalk] copying similar files in one command

Kathryn Hogg kjh at flyballdogs.com
Mon Nov 1 03:27:28 EST 2004


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?

cp /home/ed/*ed* /usr/temp/

PS. Your ls command above could easily have been done with
ls -l *ed*

What happens is that your shell takes the line that you type in and does
various expansions/substitions

Whenever it sees a '*' or '?' the shell attempts to match them against
file names.  A "*" matches any sequence of characters and a '?' matches a
single character.   Any matches are found and the shell replaces the
pattern with the matched values.  This is all transparent to the program
being called.

If you ever want to do this kind of matching in C/C++ code, have a look at
the fnmatch() function.

PS. Your ls command above could easily have been done with
ls -l *ed*

-- 
Kathryn
http://womensfooty.com



More information about the Techtalk mailing list