diff --git a/src/main/java/com/xebialabs/overthere/ConnectionOptions.java b/src/main/java/com/xebialabs/overthere/ConnectionOptions.java
index 0ff2e517..375dc476 100644
--- a/src/main/java/com/xebialabs/overthere/ConnectionOptions.java
+++ b/src/main/java/com/xebialabs/overthere/ConnectionOptions.java
@@ -117,7 +117,7 @@ public class ConnectionOptions {
/**
* See the online documentation
*/
- public static final String DIRECTORY_COPY_COMMAND_FOR_UNIX_DEFAULT = "tar c -C {0} . | tar xm -C {1}";
+ public static final String DIRECTORY_COPY_COMMAND_FOR_UNIX_DEFAULT = "cd {1} ; tar -cf - -C {0} . | tar xpf -";
/**
* See the online documentation
diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java
index 063427dd..197d2bd0 100644
--- a/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java
+++ b/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java
@@ -122,14 +122,21 @@ protected CmdLine processCommandLine(final CmdLine cmd) {
@VisibleForTesting
CmdLine prefixWithElevationCommand(final CmdLine commandLine) {
CmdLine commandLineWithSudo = new CmdLine();
- commandLineWithSudo.addTemplatedFragment(elevationCommandPrefix, elevatedUsername);
if (quoteCommand) {
+ commandLineWithSudo.addTemplatedFragment(elevationCommandPrefix, elevatedUsername);
commandLineWithSudo.addNested(commandLine);
} else {
+ boolean shouldAddElevationCommand = true;
for (CmdLineArgument a : commandLine.getArguments()) {
+ if(shouldAddElevationCommand && !a.toString(os, false).equals("cd")) {
+ commandLineWithSudo.addTemplatedFragment(elevationCommandPrefix, elevatedUsername);
+ }
+ shouldAddElevationCommand = false;
+
commandLineWithSudo.add(a);
+
if (a.toString(os, false).equals("|") || a.toString(os, false).equals(";")) {
- commandLineWithSudo.addTemplatedFragment(elevationCommandPrefix, elevatedUsername);
+ shouldAddElevationCommand = true;
}
}
}