1

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.

  • If there is a space between files, put it between quotes: "/home/michael/Documents/GeneralNetwork /GNI" – blade19899 Oct 06 '14 at 08:27
  • 1
    You really have a directory called "GeneralNetwork " with a space at the end? Why? Your problem with copying boot etc is probably that space; is that a typo? --- anyway, consider using tools like Unison for this kind of job --- see for example http://askubuntu.com/a/522423/16395 – Rmano Oct 06 '14 at 08:37
  • Rmano, sorry that was not a typo- I did not even note it, as I was editing it from LeafPad..which was a stupid mistake. This issue has been corrected. Thank you. – DankyNanky Oct 06 '14 at 09:04

1 Answers1

2

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
blade19899
  • 26,704
  • Thank you, that would make logical sense. I ammend the script and test. On another note however, is there an ability to make a wildcard so my "script" just copies the folder? I mean, ideally I'd like to just copy /home/michael/Documents/GeneralNetwork and ignore all system files. – DankyNanky Oct 06 '14 at 08:48
  • @MichaelNancarrow, i updated my answer. Due note, the code is at the top of my head. Am on a Windows machine right now, and have no ability to test it on a Linux system. – blade19899 Oct 06 '14 at 08:53
  • @MichaelNancarrow, also note, if this answered your question, it is generally preferred, that one accept it as 'accepted answer' (checkbox below the up and down arrow). – blade19899 Oct 06 '14 at 08:55
  • 1
    Blade, using . is not a valid commend, is it? – DankyNanky Oct 06 '14 at 09:05
  • 1
    @MichaelNancarrow, Using a dot can be used as a valid command, see: source or dot operator Man Page | Bash | SS64.com. eg ./script.sh. – blade19899 Oct 06 '14 at 09:13