Find the answer to your Linux question:
Results 1 to 8 of 8
I've written a script to update the "live" version of my website, so basically to copy files from a subdirectory of /home/giles to the apache htdocs directory. It works by ...
  1. #1
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283

    cp -i in a bash loop doesn't wait for input

    I've written a script to update the "live" version of my website, so basically to copy files from a subdirectory of /home/giles to the apache htdocs directory. It works by taking a command line argument of the filename (or names), finding the current path, and switching part of the current path for the apache document root:
    Code:
    ls -1 $1 | while read; do
            file="$REPLY"
            src=$(echo $(pwd)'/'$file)
            tgt=$(echo $src | sed -e "s'/old/path/'/new/path' ")
            echo "Copying $src to $tgt"
            cp -i "$src" "$tgt"
            echo "Is this point reached?"
    done
    The problem is that the cp command asks whether I want to overwrite the version currently in "/new/path", but doesn't wait for a reply. The script just exits, and the file isn't copied. The "Is this point reached?" text is echoed, but, even when there are several files to copy, the next ones aren't considered - a new "Copying $src to $tgt" for the next file never comes round, so presumably the loop doesn't restart.

    I think it might have something to do with the fact it's in a "while read" loop, because I've successfully used cp -i in a bash script before...

    Thanks for taking the time to look at this
    Giles
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    > copy files from a subdirectory of /home/giles
    > to the apache htdocs directory.

    you know where "the apache htdocs directory" is: hardcode that in.
    Start in whatever of your home directory's subdirectories you
    like, use

    cp -i <filenames> <path>/apache/htdocs

    or

    cp -i $1 <path>/apache/htdocs

    what are you keeping from us--why is this hard?
    the sun is new every day (heraclitus)

  3. #3
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    Quote Originally Posted by tpl View Post
    you know where "the apache htdocs directory" is: hardcode that in.
    Start in whatever of your home directory's subdirectories you
    like, use

    cp -i <filenames> <path>/apache/htdocs

    or

    cp -i $1 <path>/apache/htdocs

    what are you keeping from us--why is this hard?
    Sorry - I didn't mean to miss out useful information, but I can see that I wasn't totally clear. The reason I need to use the sed adjustment is that there are various sub-directories within the apache htdocs directory, which are the same in the offline version. So I'd want to copy files from /home/giles/site/htdocs/archives/ to /usr/local/apache/htdocs/archives, and I'd want to copy from /home/giles/site/htdocs/galleries to /usr/local/apache/htdocs/galleries, and several other cases. That's why I wanted the script to be able to adapt to where it is within /home/giles/site.

    Hope that makes it clearer, and thanks for your ideas.
    Giles
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi Giles,

    If you want to copy all the files from the underlying directories of /home/giles/site/htdocs to the underlying directories of /usr/local/apache/htdocs with the same name, try:

    Code:
    cd /home/giles/site/htdocs
    find * -print | cpio -pvdmu /usr/local/apache/htdocs
    The explanation of the options of cpio from the manpage:

    Code:
    -p      Read the standard input to obtain a list of path
            names of files which are then conditionally created and
            copied into the destination directory tree as determined
            by the options described below.  Destination path names
            are interpreted relative to the named directory.
    
    -v      Verbose: cause a list of file names to be printed.  When
            used with the t option, the table of contents looks like
            the output of an ls -l command (see ls(1)).
    
    -d      Create directories as needed.
    
    -m      Retain previous file modification time.  This option does
            not affect directories that are being copied.
    
    -u      Copy unconditionally (normally, an older file does not
            replace a newer file with the same name).
    Does this make sense?


    Regards

  5. #5
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    Quote Originally Posted by Franklin52 View Post
    Hi Giles,

    If you want to copy all the files from the underlying directories of /home/giles/site/htdocs to the underlying directories of /usr/local/apache/htdocs with the same name, try:

    Code:
    cd /home/giles/site/htdocs
    find * -print | cpio -pvdmu /usr/local/apache/htdocs
    Thanks for the idea, but I'm not really trying to copy all the files - I just want to copy across files when I've just edited them. Although copying everything across would update the files I'd just changed, it wouldn't exactly be the most efficient way to do it - so far I've only got 70mb of files that such a method would have to copy, but the site is still in it's early stages.

    (As a side note, is there a good reason why I couldn't have used cp -r if I was going to copy all the files?)

    Thanks anyway (and apologies to anyone who posts in the next few days if I dont reply straight away - it wont be because I'm ignoring you, but because I'll be on a hike around Snowdonia)
    Giles
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi Giles,

    Make a log file everytime you run the script as a reference file. After the copy modify the date of the logfile with the actual date.
    Now use the -newer option with find to copy only files older than the log file (i.e. the last copy date).

    For example:

    Code:
    cd /home/giles/site/htdocs
    find * -newer /home/giles/site/copy.log -print | cpio -pvdmu /usr/local/apache/htdocs
    echo "Copy made: `date`" >> /home/giles/site/copy.log
    Does it make sense?

    Regards

  7. #7
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    Thanks very much, that sounds like a good new angle of attack. I'll try it out later and get back to you .

    Giles
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

  8. #8
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    Argh... real life taking over, so I haven't been able to sit down and code properly... And because this is just a nice tool to make my life a little easier, it's got to go on the back burner when I do get time to tinker with my computers.

    Many apologies to anyone waiting to see how it turns out - I will let you know how it goes, but it may be a while.

    Giles
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...