7

Say I'm in the / folder in a remote Linux machine I connect to over ssh.

There's a folder called testapp.

/testapp

I want to create an exact copy of this called:

/testapp-staging

Using a GUI I can just right click the folder, copy it then paste and rename it.

What command do I need to use for this?

8
  • 2
    actually it's not "in SSH" but "in shell" or "in bash prompt". SSH is a network protocol and then you can use shell commands.
    – mithrop
    Oct 28, 2013 at 16:15
  • -1 because this question could use a bit of rephrasing. Something like "How to copy a folder when I'm logged in using SSH" perhaps. I'll remove the -1 afterwards. Oct 28, 2013 at 19:29
  • 2
    @CristianCiupitu in that case, just edit it, don't downvote.
    – terdon
    Oct 28, 2013 at 20:56
  • @terdon, I would have preferred for the poster to edit it, as an exercise to understand things better if you wish. Oct 28, 2013 at 22:41
  • 1
    @CristianCiupitu the poster is a new user with <200 rep whose native language is not English. The phrasing of the question clearly shows that the OP is not aware of what exactly ssh is nor what exactly they are doing, that is not their fault, the OP is clearly new to this. If you want to show them how to post good questions, edit and demonstrate that way. A downvote with a vague statement like "could use a bit of rephrasing" just comes across as arrogant and helps neither the poster nor the site. That's why we have edit privileges.
    – terdon
    Oct 29, 2013 at 1:06

4 Answers 4

14

Use the cp command, but do it recursively:

cp -R /some/dir/ /some/other/dir/

If you want to print out each file copied, -Rv

1
  • 1
    I think -a (or --archive) would be more similar to what graphical file managers do. Oct 28, 2013 at 19:32
3

It's also possible by using rsync, for example:

rsync -vua src/ dst/

where:

  • -v, --verbose: increase verbosity
  • -u, --update: skip files that are newer on the receiver
  • -a, --archive: archive mode; equals -rlptgoD (no -H,-A,-X)

If you've root privileges, prefix with sudo to override potential permission issues.

2

There are text mode (console) file managers too. Midnight commander (command mc) is one of them.

2

It's incredible how many confusing posts you can find on stackoverflow and superuser concerning themselves with the "cp" and "mv" commands. Posters are like, "how to use 'cp'?" To which people post "cp" syntax, yet nobody ever states, WHICH version of "cp" or "mv" they are actually using. FYI, there are TWO utility suites, one by Berkeley, another by GNU, and they behave very differently, so it would really help if people actually stated what version they are talking about, otherwise it's like Blind talking to Deaf.

So people will tell you to use the "-R, -r, --recursive" flag to copy a folder, just look above, but this didn't work for me:

cp -av "/System/Library/Carrier Bundles/iPhone/EPlus_de.bundle/" /work/EPlus1/

cp -R "/System/Library/Carrier Bundles/iPhone/EPlus_de.bundle/" /work/EPlus1/

Instead of copying the folder "EPlus_de.bundle" to "/work/EPlus", all files inside "EPlus_de.bundle" are copied to "/work/EPlus1/", so the structure is:

/work/EPlus1/files

NOT

/work/EPlus1/EPlus_de.bundle/files

Now, somebody will test my command and claim, "It DOES work!" Yeah, it's working on your GNU version, but will NOT work as intended on the Berkeley version, so the poster above mentioning the recursive flag is not particurly helpful, then again, the original poster never mentioned, what version he' talking about.

Berkeley: if the FromFolder has a front slash ("/") at the end, it will be interpreted as:

/*

so basically, Berkeley "cp" is told to copy all files in the FromFolder to ToFolder, NOT the FromFolder itself.

https://www.freebsd.org/cgi/man.cgi?cp

If the source_file ends in a /, the contents of the directory are copied rather than the directory itself.

Then again, you search the net on how to copy a folder, people will tell you:

cp -avr ...

of course, this throws on the Berkeley version, so people are like, "You are wrong, it's not working! Yes, it does!" arguing past each other, because they are talking about completely different programmes. Why GNU hasn't called their apps "cp2" or something is beyond me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .