Programming newbie -help!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Programming newbie -help!!
# 1  
Old 10-12-2012
Programming newbie -help!!

Hi I am trying to learn shell script and i ran into an issue.

I am trying to read a file with few directories and tar them up.
I used a while loop but i end up overwriting tar file with only the last directory in the file being tared .
Code:
cat test.txt |
(
while read line
do
tar -czPf backup.tar.gz $line
done
)

If test.txt has test1, backup1, goo1

I end up having only goo1 in the tar

how do i get all 3 directories in the tar?

Thanks for your help in advance

Last edited by vbe; 10-12-2012 at 10:47 AM..
# 2  
Old 10-12-2012
# 3  
Old 10-12-2012
That is useless use of cat..

Please see man pages for tar

tar -c is used to create new archieve.. try using tar -r
try
Code:
while read line
do
tar -rzPf backup.tar.gz $line
done<test.txt

# 4  
Old 10-12-2012
Every time the loop runs, it creates the tar file in the same name (backup.tar.gz) thus over writing the previous one, hence it ends up only with the last tared file.
The tar command in your while loop should have variable for both source and destination.

Try this:

Code:
cat test.txt |
(
while read line
do
tar -czPf  backup_$line.tar.gz  $line
done
)

# 5  
Old 10-12-2012
Hi Pamu,

I used -r but my understanding is it can used only if tar file already exists .. i am creating a new tar so it did not work
# 6  
Old 10-12-2012
you can just create a empty tar file, then use that tar file in while loop

Code:
 
tar czPf backup.tar.gz --files-from /dev/null

# 7  
Old 10-12-2012
Quote:
Originally Posted by SPR
Hi Pamu,

I used -r but my understanding is it can used only if tar file already exists .. i am creating a new tar so it did not work
You can create empty tar file as per itkamaraj's suggestion.

or add this to the command for checking presence of tar fileSmilie

Code:
if [[ -f backup.tar.gz ]]
then
tar -rzPf backup.tar.gz file
else
tar -czPf backup.tar.gz file
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

From iOS programming to Linux system programming

Hello. I like Linux and C programming language. Allways wanted to understand kernel and become a Linux system programmer. And I also like Objective-C and iOS. These two programming areas have relations: 1. Linux and iOS are UNIX-like systems, POSIX compliant. 2. It is useful to know C language... (2 Replies)
Discussion started by: Rockatansky
2 Replies

2. UNIX for Dummies Questions & Answers

Newbie needs help with Xlib / X11 programming,dunno who to ask,but you :(

Hi,I'm new here and to Unix also. I'm totall newbie. Here is what I have to do: -Basics of programming user interface with Xlib library -Xlib -Client/Server model of X-Windows system -Example of "Hello world" with button which changes text ,so that when u click displays another... (4 Replies)
Discussion started by: megane16v
4 Replies

3. Fedora

Newbie at Linux Kernel programming!

Hi Friends, This is my first ever post on this forum. I am a new user in the Linux field. Although, I have been working for sometime with CentOS at my work, I would consider myself an amateur only in this field. :D The way file system works in linux and the reason its open-source, has really... (7 Replies)
Discussion started by: rohitrajjain
7 Replies

4. Shell Programming and Scripting

perl newbie . &&..programming newbie

Hi, I am new to programming and also to perl..But i know 'perl' can come to my rescue, But I am stuck at many places and need help..any small help is much appreciated... below is the description of what i intend to acheive with my script. I have a files named in this format... (13 Replies)
Discussion started by: xytiz
13 Replies

5. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

6. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

7. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies

8. Programming

newbie to unix programming in C, needed a few simple prgs on these functions!

Hi all, I am a newbie to unix programming using C.. So i would like to have a few simple C programs to start off with.. I wanted programs on learning , abort,kill and raise,alarm and pause,I would also like to know how to use the vfork() in a prg It would be really great if i can have... (1 Reply)
Discussion started by: wrapster
1 Replies

9. UNIX for Dummies Questions & Answers

Programming Newbie Chick

OK, so I'm trying to finish my last individual assignment for this course, and it's the first time I've visited a forum (I've actually understood UNIX up to this point). I am having trouble with this one. I have to write a program that prompts the user to type their first name and stores it in a... (3 Replies)
Discussion started by: metalgoddess21
3 Replies

10. Programming

programming question from a newbie, please help

Hi Everyone, I really hope I could get some insight from a few of you, I've been searching the net for various resources, and this board seems to be the friendliest and most helpful by far. I work for a medical research company and we use sun 4 and we have different studies that have their... (1 Reply)
Discussion started by: milenky
1 Replies
Login or Register to Ask a Question