SSH and executing command
I am trying to ssh into a server and execute several commands on the host.
I managed to do with the following and it works perfectly
ssh -t -X thor 'cd /stmp/username; ls -rtl ; rm ~/tmp.file; tcsh -l'
Currently, I am trying to expand the command to change directory to the latest folder listed in /stmp/username/
Firstly, I tested the command that I wanted to use in the terminal and it works. It will go to the latest folder in the directory listing. The command is
cd `ls -rtl | cut -d' ' -f9 | tail -1`
Now i'm trying to combine the above command into the ssh command. But it does not work.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d' ' -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
Is there something wrong with my command?
linux ssh
|
show 2 more comments
I am trying to ssh into a server and execute several commands on the host.
I managed to do with the following and it works perfectly
ssh -t -X thor 'cd /stmp/username; ls -rtl ; rm ~/tmp.file; tcsh -l'
Currently, I am trying to expand the command to change directory to the latest folder listed in /stmp/username/
Firstly, I tested the command that I wanted to use in the terminal and it works. It will go to the latest folder in the directory listing. The command is
cd `ls -rtl | cut -d' ' -f9 | tail -1`
Now i'm trying to combine the above command into the ssh command. But it does not work.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d' ' -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
Is there something wrong with my command?
linux ssh
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.
– David C. Rankin
Jan 25 at 8:05
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
3
Why you shouldn't parse the output ofls(1)
.
– Kamil Maciorowski
Jan 25 at 8:20
|
show 2 more comments
I am trying to ssh into a server and execute several commands on the host.
I managed to do with the following and it works perfectly
ssh -t -X thor 'cd /stmp/username; ls -rtl ; rm ~/tmp.file; tcsh -l'
Currently, I am trying to expand the command to change directory to the latest folder listed in /stmp/username/
Firstly, I tested the command that I wanted to use in the terminal and it works. It will go to the latest folder in the directory listing. The command is
cd `ls -rtl | cut -d' ' -f9 | tail -1`
Now i'm trying to combine the above command into the ssh command. But it does not work.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d' ' -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
Is there something wrong with my command?
linux ssh
I am trying to ssh into a server and execute several commands on the host.
I managed to do with the following and it works perfectly
ssh -t -X thor 'cd /stmp/username; ls -rtl ; rm ~/tmp.file; tcsh -l'
Currently, I am trying to expand the command to change directory to the latest folder listed in /stmp/username/
Firstly, I tested the command that I wanted to use in the terminal and it works. It will go to the latest folder in the directory listing. The command is
cd `ls -rtl | cut -d' ' -f9 | tail -1`
Now i'm trying to combine the above command into the ssh command. But it does not work.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d' ' -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
Is there something wrong with my command?
linux ssh
linux ssh
asked Jan 25 at 7:58
nabilishesnabilishes
1062
1062
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.
– David C. Rankin
Jan 25 at 8:05
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
3
Why you shouldn't parse the output ofls(1)
.
– Kamil Maciorowski
Jan 25 at 8:20
|
show 2 more comments
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.
– David C. Rankin
Jan 25 at 8:05
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
3
Why you shouldn't parse the output ofls(1)
.
– Kamil Maciorowski
Jan 25 at 8:20
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.
ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.– David C. Rankin
Jan 25 at 8:05
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.
ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.– David C. Rankin
Jan 25 at 8:05
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
3
3
Why you shouldn't parse the output of
ls(1)
.– Kamil Maciorowski
Jan 25 at 8:20
Why you shouldn't parse the output of
ls(1)
.– Kamil Maciorowski
Jan 25 at 8:20
|
show 2 more comments
1 Answer
1
active
oldest
votes
When your command works from the command-line, e.g.
cd `ls -rtl | cut -d' ' -f9 | tail -1`
There are no quoting problems present. However, when you attempt to add that to your ssh
command, you wrap your ssh
command in single-quotes, but you also have included single-quotes within your command to enclose the ' '
(space) with the cut
command -- causing problems.
The simplest solution is to leave the quoting around your ssh
command to single-quotes and change the quote around the space
to double-quotes within it, e.g.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d" " -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
(there are no variable or path-expansion issues as a result)
Make the change and let me know if you have further question.
note: you should also avoid command-substitution using backticks in favor of $(...)
-- much more readable.
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use$(ls -rtl |cut -d" " -f9 | tail -1)
instead of`ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did thessh -t -X thor "cd ... "
does not work?
– nabilishes
Jan 25 at 8:20
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
|
show 3 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398242%2fssh-and-executing-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When your command works from the command-line, e.g.
cd `ls -rtl | cut -d' ' -f9 | tail -1`
There are no quoting problems present. However, when you attempt to add that to your ssh
command, you wrap your ssh
command in single-quotes, but you also have included single-quotes within your command to enclose the ' '
(space) with the cut
command -- causing problems.
The simplest solution is to leave the quoting around your ssh
command to single-quotes and change the quote around the space
to double-quotes within it, e.g.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d" " -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
(there are no variable or path-expansion issues as a result)
Make the change and let me know if you have further question.
note: you should also avoid command-substitution using backticks in favor of $(...)
-- much more readable.
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use$(ls -rtl |cut -d" " -f9 | tail -1)
instead of`ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did thessh -t -X thor "cd ... "
does not work?
– nabilishes
Jan 25 at 8:20
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
|
show 3 more comments
When your command works from the command-line, e.g.
cd `ls -rtl | cut -d' ' -f9 | tail -1`
There are no quoting problems present. However, when you attempt to add that to your ssh
command, you wrap your ssh
command in single-quotes, but you also have included single-quotes within your command to enclose the ' '
(space) with the cut
command -- causing problems.
The simplest solution is to leave the quoting around your ssh
command to single-quotes and change the quote around the space
to double-quotes within it, e.g.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d" " -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
(there are no variable or path-expansion issues as a result)
Make the change and let me know if you have further question.
note: you should also avoid command-substitution using backticks in favor of $(...)
-- much more readable.
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use$(ls -rtl |cut -d" " -f9 | tail -1)
instead of`ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did thessh -t -X thor "cd ... "
does not work?
– nabilishes
Jan 25 at 8:20
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
|
show 3 more comments
When your command works from the command-line, e.g.
cd `ls -rtl | cut -d' ' -f9 | tail -1`
There are no quoting problems present. However, when you attempt to add that to your ssh
command, you wrap your ssh
command in single-quotes, but you also have included single-quotes within your command to enclose the ' '
(space) with the cut
command -- causing problems.
The simplest solution is to leave the quoting around your ssh
command to single-quotes and change the quote around the space
to double-quotes within it, e.g.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d" " -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
(there are no variable or path-expansion issues as a result)
Make the change and let me know if you have further question.
note: you should also avoid command-substitution using backticks in favor of $(...)
-- much more readable.
When your command works from the command-line, e.g.
cd `ls -rtl | cut -d' ' -f9 | tail -1`
There are no quoting problems present. However, when you attempt to add that to your ssh
command, you wrap your ssh
command in single-quotes, but you also have included single-quotes within your command to enclose the ' '
(space) with the cut
command -- causing problems.
The simplest solution is to leave the quoting around your ssh
command to single-quotes and change the quote around the space
to double-quotes within it, e.g.
ssh -t -X thor 'cd /stmp/username; cd `ls -rtl |cut -d" " -f9 | tail -1` ; rm ~/tmp.file; tcsh -l'
(there are no variable or path-expansion issues as a result)
Make the change and let me know if you have further question.
note: you should also avoid command-substitution using backticks in favor of $(...)
-- much more readable.
edited Jan 25 at 8:18
answered Jan 25 at 8:13
David C. RankinDavid C. Rankin
1539
1539
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use$(ls -rtl |cut -d" " -f9 | tail -1)
instead of`ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did thessh -t -X thor "cd ... "
does not work?
– nabilishes
Jan 25 at 8:20
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
|
show 3 more comments
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use$(ls -rtl |cut -d" " -f9 | tail -1)
instead of`ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did thessh -t -X thor "cd ... "
does not work?
– nabilishes
Jan 25 at 8:20
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
This solution works. Thanks. You mentioned in the post that i should avoid using backticks, can you show an example using $(..)
– nabilishes
Jan 25 at 8:19
Use
$(ls -rtl |cut -d" " -f9 | tail -1)
instead of `ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
Use
$(ls -rtl |cut -d" " -f9 | tail -1)
instead of `ls -rtl |cut -d" " -f9 | tail -1`
– David C. Rankin
Jan 25 at 8:19
I am just curious why did the
ssh -t -X thor "cd ... "
does not work?– nabilishes
Jan 25 at 8:20
I am just curious why did the
ssh -t -X thor "cd ... "
does not work?– nabilishes
Jan 25 at 8:20
1
1
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
@nabilishes It makes backticks act locally.
– Kamil Maciorowski
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
Since the outer double quotes do not prevent expansion, command was expanding locally -- wrapping in single-quotes prevents all expansion
– David C. Rankin
Jan 25 at 8:21
|
show 3 more comments
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398242%2fssh-and-executing-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have 2-sets of single-quotes, wrap the command in double-quotes, e.g.
ssh -t -X thor "cd ... "
to preserve the included single quotes for your space or escape the space.– David C. Rankin
Jan 25 at 8:05
@DavidC.Rankin This should be an answer, I think.
– Kamil Maciorowski
Jan 25 at 8:08
Yes, it was kind of a simple one, but I can do a write up. Thanks.
– David C. Rankin
Jan 25 at 8:09
@DavidC.Rankin changing to double quotes gives out a new error. Now the command list ls the current directory where i executed the command instead of ls in host thor's directory /stmp/username
– nabilishes
Jan 25 at 8:13
3
Why you shouldn't parse the output of
ls(1)
.– Kamil Maciorowski
Jan 25 at 8:20