Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you tar a sequential file

Posted on 2011-09-17
9
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Accepted Solution

by:
acbxyz earned 2000 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.
Suggested Courses

730 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