LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Unzip all files in a directory through SSH (https://www.linuxquestions.org/questions/linux-general-1/unzip-all-files-in-a-directory-through-ssh-4175631895/)

SuzuBell 06-13-2018 08:18 PM

Unzip all files in a directory through SSH
 
Hello,

I am trying to unzip all tar.gz files in my current directory, so I use the following command:

Code:

for f in *.gz ; do gunzip -c "$f" > ./"${f%.*}" ; done
It seems to work okay. But now I am trying to repeat this process on a directory that is located on a remote server to which I need to ssh into. Now, when I attempt to run the above code, it will unzip some of the tar.gz files but then I will lose my connection. I am trying to force the job to continue.

I read this can be done with the nohup command, so I tried the following:

Code:

nohup for f in *.gz ; do gunzip -c "$f" > ./"${f%.*}" ; done &
But got the message:

-bash: syntax error near unexpected token `do'

So, then I tried the following:

Code:

nohup sh -c "for f in *.gz ; do gunzip -c "$f" > ./"${f%.*}" ; done" &
but it only seemed to partially unzip one file.

My question is: How can I write a job that unzips all files in a directory even if my session through ssh times out?

Thank you.

ondoho 06-14-2018 12:32 AM

why do you lose connection?
maybe ssh has an option to carry on doing what it does on the remote machine, regardless of the connection?

https://www.startpage.com/do/dsearch...onnection+lost
- ok, it looks like nohup is what you need.
maybe the command fails for other reasons, have you considered that?

Turbocapitalist 06-14-2018 02:06 AM

Remember that the quotes are there inside other quotes of the same kind. If you want them passed through to the remote machine while they are inside of other quotes, they must be escaped:

Code:

nohup sh -c "for f in *.gz ; do gunzip -c \"$f\" > ./\"${f%.*}\" ; done" &
Or you since you want them passed through as literals, the outer layer of quotes could do that for you. Choose single quotes ' for that:

Code:

nohup sh -c 'for f in *.gz ; do gunzip -c "$f" > ./"${f%.*}" ; done' &
If you have intermittent connection to the remote machine you might consider using tmux instead.

Code:

while true; do
        ssh -t -i ~/.ssh/server_key_rsa -l suzubell server.example.com 'tmux a || tmux';
        sleep 5;
done;

Use a key for that and add it to your agent and the reconnection will go without asking for the passphrase each time.

pan64 06-14-2018 02:21 AM

nohup works on the first command, which is the for .... ; do is the second one. That's why it cannot work.

I would try:
1. create a script and run only that script on that remote host with nohup.
2. try remote execution by: echo "script" | ssh user@host /bin/bash

scasey 06-14-2018 02:36 AM

Put the command in a script on the remote machine, then nohup the script

jlinkels 06-14-2018 10:39 AM

On the remote machine install screen. Screen lets you open terminal process which keeps running after logout or SSH disconnect.

Most important commands:
Code:

screen -S myzip  #create the screen process
CTRL-A D        #detach

Now disconnect from SSH, have lunch, whatever
Reconnect with SSH
Code:

screen -ls      #find the name of your screen back
screen -r myzip  #reconnect to you terminal process

This is easier than nohup and friends.

jlinkels


All times are GMT -5. The time now is 06:31 AM.