Ubuntu – Need Help Improving Script

bashscriptssh

I have a group of folders (L1N-L5N), in which I want to copy from my /home/michael/documents/XXX to my Dropbox folder. At present, I have used the following script:

#!/bin/sh
cp -ra /home/michael/Documents/GeneralNetwork /GNI /home/michael/Dropbox/NetworkData
cp -ra /home/michael/Documents/GeneralNetwork /L1N /home/michael/Dropbox/NetworkData
cp -ra /home/michael/Documents/GeneralNetwork /L2N /home/michael/Dropbox/NetworkData
cp -ra /home/michael/Documents/GeneralNetwork /L3N /home/michael/Dropbox/NetworkData
cp -ra /home/michael/Documents/GeneralNetwork /L4N /home/michael/Dropbox/NetworkData
cp -ra /home/michael/Documents/GeneralNetwork /L5N /home/michael/Dropbox/NetworkData

This works, but poorly.

I am also unsure of the reasoning as to why it returns this:

cp: cannot stat ‘/GNI’: No such file or directory
cp: cannot stat ‘/L1N’: No such file or directory
cp: cannot stat ‘/L2N’: No such file or directory
cp: cannot stat ‘/L3N’: No such file or directory
cp: cannot stat ‘/L4N’: No such file or directory
cp: cannot stat ‘/L5N’: No such file or directory

I have thought of shortening it down:

cp -ra /home/michael/Documents/GeneralNetwork/L*

Or some other wildcard to pickup all folders containing "L". What I want is to copy the files (and overwrite) to the folder in this directory, however I cannot be certain they will fall under the above Folder Name Scheme.

What is the best way to create a .sh file to copy everythig from …/GeneralNetwork/* without copying hidden files etc? I have done the ../GeneralNetwork/. and it included boot, bin, sys etc.

Best Answer

  • cp: cannot stat ‘/GNI’: No such file or directory

    That's because of the whitespace:

    cp reads it like this:

    cp -ra /home/michael/Documents/GeneralNetwork /GNI
    

    That the folder: /home/michael/Documents/GeneralNetwork needs to be copied to /GNI. But there is no folder in /GNI, therefore the error.


    If there is a whitespace between files, put it between quotes:

    "/home/michael/Documents/GeneralNetwork /GNI"

    cp is seeing:

    /home/michael/Documents/GeneralNetwork
    

    Withing quotes, it will read beyond the whitespace:

    cp -ra "/home/michael/Documents/GeneralNetwork /GNI" /home/michael/Dropbox/NetworkData
    

    Per user request:

    If i understand you correctly, you want to copy /home/michael/Documents/GeneralNetwork

    and everything in it. Yes that is possible, like so:

    cp -ra "/home/michael/Documents/GeneralNetwork /*.*" /home/michael/Dropbox/NetworkData
    
  • Related Question