LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-13-2018, 08:18 PM   #1
SuzuBell
LQ Newbie
 
Registered: Dec 2013
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 06-14-2018, 12:32 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 8,413
Blog Entries: 5

Rep: Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047Reputation: 2047
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?
 
Old 06-14-2018, 02:06 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 3,122
Blog Entries: 3

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
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.
 
Old 06-14-2018, 02:21 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,865

Rep: Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233Reputation: 3233
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
 
Old 06-14-2018, 02:36 AM   #5
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 973

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Put the command in a script on the remote machine, then nohup the script
 
Old 06-14-2018, 10:39 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,040

Rep: Reputation: 949Reputation: 949Reputation: 949Reputation: 949Reputation: 949Reputation: 949Reputation: 949Reputation: 949
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unzip all .zip files in a directory tes15 Linux - Newbie 14 08-29-2014 11:46 AM
unzip the zip files to local directory S@R@H Linux - Newbie 5 05-05-2010 08:43 PM
unzip many files...each zip file needs a directory linuxhippy Linux - General 10 01-17-2010 05:08 PM
Script to unzip and gzip files into directory, eldaria Programming 3 03-16-2007 10:59 AM
about unzip to unzip some chinese files' trouble arcow Linux - Software 0 03-24-2006 02:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration