Solved

How do you tar a sequential file

Posted on 2011-09-17
9
397 Views
Last Modified: 2012-05-12
I have multiple files starting from test.file1 and ends at test.file890. I need to tar test.file122 to test.file300. I can't just type them all because its a lot to type in. What can I do to easily tar my needed files?

0
Comment
Question by:SuperRoot
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Accepted Solution

by:
acbxyz earned 500 total points
ID: 36554022
You can use a little bash-script to do that
tar -cxf tarfile.tar.gz $(for i in `seq 122 300`; do echo -n "test.file$i "; done)

Open in new window


If your command gets too long for cmdline it is also possible to update the tar file for each file.
tar -cf tarfile.tar file.test122
for i in `seq 123 300`; do
tar -uf tarfile.tar test.file$i
done
gzip tarfile.tar

Open in new window

0
 
LVL 1

Author Comment

by:SuperRoot
ID: 36554105
this is what I get tar:

tar: test.fileseq: Cannot stat: No such file or directory
tar: 1: Cannot stat: No such file or directory
tar: 167: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
0
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554285
This error means you forgot the backticks around the seq-command or more probably used single quotes instead. In English layout, the backticks are found on the left upper corner between the escape key and the tab key.
With backticks bash starts a new process with the command line inside these backticks and uses its output instead.
For testing you can type only "seq 1 167" on terminal and should get a list of numbers starting at 1 forward to 167.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 1

Author Comment

by:SuperRoot
ID: 36554405
Hi acbxyz,

this is exactly what I did:

tar -czvf drive.cap.tar.gz drive.cap.test
for i in `seq 1 167`; do
tar -uvf drive.cap.tar.gz drive.cap.test$i
done

when I press enter, I get the error:
tar: test.fileseq: Cannot stat: No such file or directory
tar: 1: Cannot stat: No such file or directory
tar: 167: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors

I'm sorry if it sounds stupid but the commands above should have worked right?
0
 
LVL 10

Assisted Solution

by:acbxyz
acbxyz earned 500 total points
ID: 36554432
Yes, this should work. Actually, it doesn't because tar doesn't support updates on compressed archives, but it should print that it can't; 167 times.

Which shell do you use? "help" may help you ;-)
Maybe it'll help replacing the for-line with this, when your shell doesn't support subshells via backtick:
for i in $(seq 1 167); do

Open in new window

0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36554495
user@box:~$ touch a1 a2 a3
user@box:~$ tar cvf abc.tar a[2-3]
a2
a3
user@box:~$

Open in new window

0
 
LVL 1

Author Closing Comment

by:SuperRoot
ID: 36554548
YES! that works!! Man you are a life saver
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36554553
Did you try my command?  A bit cleaner solution...
0
 
LVL 48

Expert Comment

by:Tintin
ID: 36554827
Here's another alternative which is much more efficient



tar czvf file.tar.gz $(seq 122 1300 | xargs -i echo  test.file.{})

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fedora linux on laptop - setup sendmail - or some kind of email 5 57
winscp 000webhost.com 6 75
Samba 4, Users Permission, 5 52
Unix Command (AIX) 3 25
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question