(Q) How to copy multiple files based on command-line arg?

(Q) How to copy multiple files based on command-line arg?

Post by Amer Neel » Sun, 28 Apr 1996 04:00:00



I've looked in this group for an FAQ hoping to find something to use but
to no avail. I'm on BSD Unix using csh as my shell. I have a directory of
numerous files that get updated frequently and I'd like to copy only
those files matching a command-line argument (such as "Apr 27") to
another directory.

This seems like such a trivial task, and I'm close to a solution, but
it's not quite working.

I've approached it from 2 directions. I don't care which one the solution
addresses. I just need *help* :-)

1) I can get a list of filenames quite easily using the following script:

#!/bin/csh -f
ls -l ~/myhtml/*.html | grep "$1" >! today.list
cut -c68-90 today.list >! names.list
cat names.list

However, this presents me with the problem of looping through this file
and processing each line. I don't know enough about awk, perl, or sed to
do this.

2) The other attempt is a shell script as follows:

#! /bin/csh
# update version 6
# Copy files matching command line argument (usually date).

foreach file (`ls -l ~/myhtml/*.html | grep $1`)
 cp $file ~/in/
 cp $file ~/public_html/
end

This doesn't work. If anyone can help I would *really* appreciate it.
Email direct would be preferable. Thanks in advance and bcnu...

--
May the fire be your friend, may the sea rock you gently,
May the moon light your way, 'til the wind sets you free. - Shriekback


|                    P.O. Box 1538, London ON, N6A 5M3                   |
|                           Voice: (519) 438-5887                        |
+=============Practice safe hex ... wear a write-protect tab=============+

 
 
 

(Q) How to copy multiple files based on command-line arg?

Post by Bill Marc » Tue, 30 Apr 1996 04:00:00




Quote:>#!/bin/csh -f
>ls -l ~/myhtml/*.html | grep "$1" >! today.list
>cut -c68-90 today.list >! names.list
>cat names.list

>However, this presents me with the problem of looping through this file
>and processing each line. I don't know enough about awk, perl, or sed to
>do this.

>2) The other attempt is a shell script as follows:

>#! /bin/csh
># update version 6
># Copy files matching command line argument (usually date).

>foreach file (`ls -l ~/myhtml/*.html | grep $1`)
> cp $file ~/in/
> cp $file ~/public_html/
>end

>This doesn't work. If anyone can help I would *really* appreciate it.
>Email direct would be preferable. Thanks in advance and bcnu...

You didn't include the "cut" in this script.  $file contains the entire
line from `ls -l`.  You could change the foreach line to
foreach file (`ls -l ~/myhtml/*.html | grep $1 | cut -c68-90`)

--

"Even the samurais have teddy bears, and even the teddy bears get drunk"
(don't ask me what it means, it came from the 'fortune' program).

 
 
 

(Q) How to copy multiple files based on command-line arg?

Post by Stanislaw Streln » Wed, 01 May 1996 04:00:00


: I've looked in this group for an FAQ hoping to find something to use but
: to no avail. I'm on BSD Unix using csh as my shell. I have a directory of
: numerous files that get updated frequently and I'd like to copy only
: those files matching a command-line argument (such as "Apr 27") to
: another directory.

[...]

: foreach file (`ls -l ~/myhtml/*.html | grep $1`)
                                              ^^
                                        try grep "$1" instead.

--

          St.Strelnik

Down with categorical imperative!

 
 
 

(Q) How to copy multiple files based on command-line arg?

Post by John Savag » Sun, 05 May 1996 04:00:00



>I'm on BSD Unix using csh as my shell. I have a directory of
>numerous files that get updated frequently and I'd like to copy only
>those files matching a command-line argument (such as "Apr 27") to
>another directory.

>This seems like such a trivial task, and I'm close to a solution, but
>it's not quite working.

>#!/bin/csh -f
>ls -l ~/myhtml/*.html | grep "$1" >! today.list
>cut -c68-90 today.list >! names.list
>cat names.list

Change that 3rd statement to:  cp `cat names.list` newdir
and you'll be close to what you're after.

But your whole task could be done in one line: cp  *'Apr 27'*  newdir

When you use a script and pass it an argument containing a space, you
do need to always quote that space on the command line, e.g., Apr\ 27
or 'Apr 27', as well as referencing it in double quotes in the script.
--

 
 
 

(Q) How to copy multiple files based on command-line arg?

Post by Corne Beers » Tue, 07 May 1996 04:00:00



> I've looked in this group for an FAQ hoping to find something to use but
> to no avail. I'm on BSD Unix using csh as my shell. I have a directory of
> numerous files that get updated frequently and I'd like to copy only
> those files matching a command-line argument (such as "Apr 27") to
> another directory.

Well, there are 2 basic aproaches for updating files or directories.
I guess you only like to copy files that are changed, updated or added
since the last time you did this.

1st: Have a look at the find command. You can search for files that
        are touched since a specified time/date or that are newer
        than an other file. If you only have the 'newer than file'
        option, end your job touching a timestamp file.
        The next works if  has no subdirectories.
find /source/dir -newer /TmStDir/TimeStampFile -exec cp {} /target/dir/`basename {}` \; ; touch
/TmStDir/TimeStampFile

2nd: If the number of files and the filenames are verry constant and
        they are only updated, you could make a `makefile`.
% cat makefile
/target/dir/file1:      /source/dir/file1
        cp /source/dir/file1 target/dir/file1
/target/dir/file2:      /source/dir/file
        cp /source/dir/file2 target/dir/file2
/target/dir/file2:      /source/dir/file2
        cp /source/dir/file2 target/dir/file2

and execute this in the directory with the makefile with:
% make

The first option will copy all files which are newer, including the realy new files
The second option will only copy the files you specify.

Groetjes en succes
Corne

--
My employer only sponsors the bandwidth, not my opinions
C.J.P. Beerse                          | Alcatel Telecom Nederland bv

tel:+31-70 3079108 fax:+31-70 3079 191 | NL-2280 GG  Rijswijk