Announcement

Collapse
No announcement yet.

need bash script

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    need bash script

    Need a bash script from a bash expert. GUI's are nice but they have also rotted my brain and I have forgotten 99% of my CLI knowledge. What happens when the CLI is gone

    Okay I have 2 USB flash disks that I back-up all really important financial data to every time it changes.

    Right now I have a script that I have to change in the editor whenever I upgrade to a larger capacity flash drive. When I upgrade, the label on the flash drive changes which changes the directory under which the flash drive is mounted, thus requiring the manual change to the script.

    What I would like to have is a single script that will do the copy to all directories under '/media'.

    My current script is:

    Code:
    # extraneous housekeeping
    
    #
    echo "Updating to Flash Drive - Financial"
    cp --update -v -R ~/financial/* /media/TRAVELDRIVE/financial
    cp --update -v -R ~/financial/* /media/RUNDISK/financial
    
    #extraneous housekeeping
    I would like to replace the 'cp' lines with something so that the flash drive directory under '/media' isn't hardcoded.

    Suggestions??

    #2
    Re: need bash script

    Try:

    Code:
    find /media/* -maxdepth 0 -type d -exec cp --update -v -R ~/financial/* '{}' \;

    Comment


      #3
      Re: need bash script

      I'd probably use a for cycle like this:
      Code:
      # extraneous housekeeping
      
      #
      echo "Updating to Flash Drive - Financial"
      
      OLDIFS=$IFS
      IFS=":"
      
      for DIR in $(ls -d /media/*/); do
      cp --update -v -R ~/financial/* /media/$DIR
      done
      
      IFS=$OLDIFS
      
      #extraneous housekeeping
      The IFS trick is to deal with potential blank spaces in the names of the folders.

      EDIT: The solution presented by previous post is cleaner, didn't see it until I posted.

      Comment


        #4
        Re: need bash script

        Oh well, I was posting at the same time as others. Just in case there is any problem with the other ideas, here is my shot

        Code:
        #!/bin/bash
        
        for DIR in `find /media -mindepth 1 -maxdepth 1 -type d -print`; do
          echo $DIR
          cp --update -v -R ~/financial/* $DIR
        done
        BTW, I think rsync is very nice for this kind of thing ...

        Comment


          #5
          Re: need bash script

          Thanks again.

          rsync looks interesting, but I am having problems with file permissions on the flash drive.

          rsync is doing a chgrp operation that fails.

          I looked at the flash drive in Dolphin and everything on the flash drive (including the flash drive top directory) has me as the owner and root as the group.

          I'm guessing that having everything with group root is problem. How can I have the flash drive (and hence all files and directories thereon) mounted with my group?

          cp doesn't have this problem.

          Comment

          Working...
          X