Visit Our UNIX and Linux User Community


How to tar files inside a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to tar files inside a script?
# 1  
Old 08-02-2012
How to tar files inside a script?

Hi ,
I have a file which contains few file names. I need to tar those files but am unable to do inside the script. Any help will be useful.
Code:
cat /tmp/test
aa.txt
bb.txt
cc.txt

I have tried the below code but its not working.
Code:
for i in `cat /tmp/test';do tar -cvf TEST.tar $i;done


Last edited by Franklin52; 08-03-2012 at 06:41 AM.. Reason: Please use code tags for data and code samples
rogerben
# 2  
Old 08-02-2012
Hi,
please check that the cat command is inside backticks (the last one seems a single quote instead).

Moreover, the -c switch of tar command results in the re-creation of the .tar archive at each for loop iteration; to avoid this use the -r switch instead of the -c switch.

let us know
see ya
fra
This User Gave Thanks to frappa For This Post:
# 3  
Old 08-02-2012
Running tar 99,000 times to archive 99,000 files is painful to watch. Also see Useless Use of Cat and Useless Use of Backticks, which may cause your program to actually fail when there's large numbers of files.

Here, use this. It will run tar the minimum number of times actually needed.

Code:
xargs tar -rcf /path/to/file.tar < /tmp/test

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-02-2012
Code:
tar cvf myTar.tar -I /tmp/test

'/tmp/test' is a file with file names to be tar-ed up.
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 08-02-2012
sample file with filenames to be tar-ed
aa.txt bb.txt cc.txt
Code:
tar -cvf tarname.tar `cat path/to/file`


Last edited by Franklin52; 08-03-2012 at 06:42 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 08-02-2012
Quote:
Originally Posted by praveen_reddy
sample file with filenames to be tar-ed
aa.txt bb.txt cc.txt

tar -cvf tarname.tar `cat path/to/file`
See useless use of cat and useless use of backticks.

There is a better way to convert stream data into commandline arguments known as 'xargs' which I have demonstrated above.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-02-2012
Quote:
Originally Posted by vgersh99
Code:
tar cvf myTar.tar -I /tmp/test

'/tmp/test' is a file with file names to be tar-ed up.
In case the OP is using GNU/Linux, that won't work. -I is a BSD-ish feature available on *BSD and Solaris (perhaps others?), but GNU uses -T instead.

I've come to embrace pax. It's consistent across platforms by virtue of being the only standardized archiver. It also fits very naturally into UNIX pipelines.
Code:
pax -w < /tmp/test > my.tar

Regards,
Alister
This User Gave Thanks to alister For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #934
Difficulty: Medium
The C standard library does not specify any specific resolution, epoch, range, or datatype for system time values.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uncompress several tar.gz files inside several folders

Hi, I need to uncompress sevral tar.gz files that are located insides sevral folders using a shell script. Folder tree looks like this : /folder/001 /folder/002 /folder/003 Inside each folder dossier (001,002,003) we can find 1 or several tar.gz files. I've scripted something... (9 Replies)
Discussion started by: shellX
9 Replies

2. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

3. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

4. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

5. Shell Programming and Scripting

Not correct processing of \ in names of dirs inside shell script (tar command - system backup scr

Hello, Recently, I've started with shell scripting, and decided to write a script for my system backup using tar. When I was dealing with tar execution inside shell script I found this, inside shell we have the following code: tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_STARTwith... (6 Replies)
Discussion started by: ilnar
6 Replies

6. Shell Programming and Scripting

Need script to find errored files inside directories

Hi people. I working on a script to check for files that they are suposed not to be on the directory. I mean, inside of each directory it must have some files but some could be wrong, and i want to move the files that are wrong. Ex: CSPOTGET edpst/CargadoresSPOT Historicos_Spot_MDI.zip... (4 Replies)
Discussion started by: osramos
4 Replies

7. Shell Programming and Scripting

Script to tar up old log files

Hell All! I need help writing a script for my job. I never really wrote alot of scripts but I guess it a first time for everything. I need to write a simple script that goes out to our log file directory on our servers and tar and compress them. I need to know how to add an date time stamp to tell... (5 Replies)
Discussion started by: aojmoj
5 Replies

8. Shell Programming and Scripting

How to process the files using .tar.gz files in script

Hi, I have some file in /users/xyz directoty with .tar.gz extension. i need to find these files and if found in need to run other commands. I now the command for finding files,but how to put if condition ?please help me Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies

9. Shell Programming and Scripting

ftp files inside a shell script

I have a shell script where I am trying to ftp some files but I get the error message "EOF unclosed" every time the script reaches the ftp section. Here is how my script is written. #more code up here rm -f $object >> $LOG_FILE 2>&1 fi #end of if done #end of for loop ... (5 Replies)
Discussion started by: matrix1067
5 Replies

10. Shell Programming and Scripting

Using tar inside a shell script

Hi, I am using tar cvf inside a shell script to archive files. Is there an option to surpress any prompts which come up if the desired archive name already exists ? Thanks in Advance. Kas. (2 Replies)
Discussion started by: kas7225
2 Replies

Featured Tech Videos