0

I have tried to tar archive via ssh and i thought nothing is tricky for me in this task. But then i need to tar file from a path that include the most recent changed directory. To get this i am using such command:

cd  "$(\ls -1dt ./*/ | head -n 1)"

The entire command looks like:

ssh root@$ip "cd /dir1/dir2/ && cd "$(\ls -1dt ./*/ | head -n 1)" && cd /dir3/ && tar -cpf - *.log" > $dir/out.tar

Unfortunately i get:

./Recent/: No such file or directory

I have tried to change \ls -1dt ./*/ | head -n 1 to \ls -1dt /*/ | head -n 1

Then i got:

cd: dir3/: No such file or directory

All of the directories in /dir1/dir2/, for which i am searching for most recently changed dir, contain dir3 directory, thats why previous error is imposible.

Also if i manually go to my remote machine via ssh and perform each command (diffirent cd, searching, tar) one by one, searching will work correctly without errors, but in my case i need the "whole string" command, because it is only a part of my entire automation script.

How to solve this?

Thanks

alex
  • 13
  • 3
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Feb 27 '18 at 16:53
  • @jww My impression was that `bash` was considered a programming language (wikipedia lists it as a "command language" in addition to a shell), I checked metaSO to see if there was a counter-argument but [the first question I found](https://meta.stackoverflow.com/questions/283158/where-should-bash-questions-be-posted) seems to support my opinion ; do you have any other source stating otherwise? (I'm in part motivated by avoiding downvotes on otherwise perfectly good answers, and it is likely that you were the source of the latest downvote, but I hope that doesn't make the question less valid) – Aaron Feb 27 '18 at 17:08
  • Bash scripting, yes. Commands in Bash, no. – jww Feb 27 '18 at 17:28
  • @jww isn't a script nothing more than a sequence of commands? Would you think the question would be more appropriate if the (single but composed) command was inside a `.sh` file? Here the problem comes from a lack of understanding of how the code is parsed, which seems like a language feature that can be discussed on SO whether it's used in a single command or a 100-lines script. It's obviously a noob question and could be closed as a duplicate to [this one](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash#6697781) (hopefully with a bit ... – Aaron Feb 27 '18 at 18:28
  • ...of explanation aside to explain the relation) but I feel like closing it as off-topic is unfair to the asker who produced a question of reasonable quality for a newcomer. I don't want you to retract any close- or down-vote, but I'm under the impression that the community has been especially harsh to newcomers lately, and while it can reasonably be explained by an increase of the volume of bad questions I feel like we should be careful not to overreact and lose the opportunity to attract people that could later be assets to the community – Aaron Feb 27 '18 at 18:28

1 Answers1

-1

You wrap the command you provide to ssh in double quotes which leads to two undesirable behaviours :

  1. Your immediate problem is that your local shell expands the quoted string before passing it to ssh. In doing that, it executes the subshell ($(ls ...)) locally, which is likely why the remote server complains about not finding the resulting directory

  2. You've got two double-quoted strings and a subshell between them. As long as the subshell returns a single word you're good, but if its result contained multiple words you'd suddently pass multiple parameters to ssh which would likely fail

These two problems can be fixed/avoided by enclosing the command you want your remote host to execute in single-quotes :

ssh root@$ip 'cd /dir1/dir2/ && cd "$(\ls -1dt ./*/ | head -n 1)" && cd /dir3/ && tar -cpf - *.log' > $dir/out.tar

In this case your local shell doesn't expand the string and the remote host will correctly execute the following command (in which it will expand the double-quoted part, executing the subshell) :

cd /dir1/dir2/ && cd "$(\ls -1dt ./*/ | head -n 1)" && cd /dir3/ && tar -cpf - *.log
Aaron
  • 24,009
  • 2
  • 33
  • 57