Operating System - HP-UX
1831242 Members
2831 Online
110021 Solutions
New Discussion

Re: unix script to create tar file

 
SOLVED
Go to solution
Nitin Nigam
Occasional Advisor

unix script to create tar file

Hi

I have more than 50 files in one directory what I want is to separate the files depending on the first 5 letters (ex. tcpco*, nwint*, voice*..etc).
there is possibilty that few more files with different first 5 letters can be added to this directory.
what i want is the script that will check the first 5 letters and tar them (ex. - all files starting with tcpco* to tcp.tar and so on)

since new files with different names can also be added so I dont want to use
(tar xvf ./tcpco*), how can i use the command in script so I dont have to worry about the new files as well, so script will check the filenames (i.e. tcpco*, voice*....) and tar them individually.
thanks
8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: unix script to create tar file

Scratch look at this(I'm not going to do it all).

ls -1 > file.temp

Thats ls dash 1 to create a alphabetized file list.

# Then

while read -r xx
do
test=substr(5,1)$xx #This you'll have to get to work yourself.

if [ "$test != "$test2" ]
then
tar cfv $test.tar $test*
$test2=$test
fi
test2=$test
done < file.temp

I know its not everything, but its the best I can do right now. Its been a long,long day.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Mark Grant
Honored Contributor

Re: unix script to create tar file

This works though I'm not sure I like it :)


#!/bin/sh
DONE="none"

for i in `ls`
do
PREFIX=`expr $i : "\(.....\).*"`
if [ $PREFIX != $DONE ]
then
tar cvf $PREFIX.tar $PREFIX*
DONE=$PREFIX
fi
done
Never preceed any demonstration with anything more predictive than "watch this"
Bhuvaneswari Selvaraj
Valued Contributor
Solution

Re: unix script to create tar file

Copy this script to the parent dir of the dir that you want to dir. In this script I am assuming that the name of the dir you want to tar is test, if not change the string test in the scirpt with your di name.

for i in `ll test| awk '{print $NF}' | cut -c 1-5 | sort -u`
do
tar -cvf $i.tar test/$i*
done
Mark Grant
Honored Contributor

Re: unix script to create tar file


SEP,

I can tell it's been a long, long day for you, you were posting here when I went to bed!

Bhuveneswari,

I think it's only fair to include a bit of a test to stop the script creating the same tar file each time the same five characters come up.
Never preceed any demonstration with anything more predictive than "watch this"
Bhuvaneswari Selvaraj
Valued Contributor

Re: unix script to create tar file

Hi Mark,

The catch is that, I had included sort -u in the command, and hence only one instance of the five characters will come. So, the condition "each time, the same five charactiers coming up" will not come in to picture.

I guess sort -u will take care of this, feel free to correct me if I am wrong.

Mark Grant
Honored Contributor

Re: unix script to create tar file

Bhuveneswari, You are absolutely right of course. I therefore vote that your solution is the best one so far! Regards
Never preceed any demonstration with anything more predictive than "watch this"
Bhuvaneswari Selvaraj
Valued Contributor

Re: unix script to create tar file

thx...

btw, is the owner's question answered or not???
Nitin Nigam
Occasional Advisor

Re: unix script to create tar file

Thanks for the suggestions guys. Bhuvaneswari I used your script and it worked fine for me.