cowardly refusing to create an empty archive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cowardly refusing to create an empty archive
# 1  
Old 11-15-2011
cowardly refusing to create an empty archive

Hi,
I am trying to write an Archive script that should look for files older than x days, zip them and move the zip to the archive directory and delete the files that have been zipped. I am not sure how i can handle this error:
"cowardly refusing to create an empty archieve".
Just wanted to know if there is any workaround for this. I did look into one of the threads but didn't find the answer so posted it here!!
Any help will be greatly appreciated!!!

Here is the code that i have written:
--------------------------------------------------
Code:
#!/bin/sh

FILES=/abc/def
ARCH=/fff/ddd

cd $FILES
`find . -name "*.txt" -type f -mtime +10 | xargs tar -czvf filetar.tar.gz
`mv filetar.tar.gz $ARCH`
status_check=`echo $?`
if [ $status_check -ne 0 ]; then
echo "Error"
else
echo "Success"
`find . -name "*.txt" -type f -mtime +10 | xargs rm -rf {} \;
fi

---------------------------------------------------------------

THANKS!!!!

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-16-2011 at 03:44 AM.. Reason: Please use code tags
# 2  
Old 11-15-2011
It's being fed no files, and refuses to create an empty archive. Try leaving off the | xargs ... and see what it prints.
# 3  
Old 11-15-2011
Add -r to the xargs. This should prevent xargs from running the tar if there isn't anything on the input.


However, you really need to reconsider using tar with xargs. If the filename arguments are long enough to cause xargs to run a second tar command you'll overwrite the first and it will appear that you only got the last n files in your archive.


Try this in a directory with a few files and see what happens:
Code:
  ls | xargs -L 1 tar -czvf foo.tar.gz
echo "contents:"
gzip -dc foo.tar.gz|tar -tvf -

The output from the -v will indicate that all files were passed to tar, but the table of contents will show only one file. (-L causes xargs to use at most one input line per invocation simulating what it would do if it needed to invoke multiple instances of tar)

Last edited by agama; 11-15-2011 at 09:20 PM.. Reason: clarity
# 4  
Old 11-16-2011
Hey thank you so much for responding...
@corona...It works fine if i dont use xargs...as it just prints the output...but as i said i want to zip these files and move them to a safe location.
If i am not wrong..do you want me to split the "find" statement into multiple lines instead of passing "xargs"
like this:
Code:
`find . -mtime +7 -type f -name "*.txt"`
`tar -czvf sample.tar.gz`

Please let me know...

@agama..I did try your code..It printed out all the files (including zip files) in that directory and created a zip of all the mentioned files..But what i am not sure is..how would this help me out...Please make me understand!!!

Thanks Again!!!!
# 5  
Old 11-16-2011
Quote:
Originally Posted by stunnerz_84
Hey thank you so much for responding...
@corona...It works fine if i dont use xargs...as it just prints the output...
Which looks like what? Post a sample.

I don't see any particular reason it shouldn't work unless it's got spaces in filenames or something wacky like that.

Did you try adding -r to xarg's arguments, too?
# 6  
Old 11-16-2011
Try this one:
Code:
#!/bin/sh

FILES=/abc/def
ARCH=/fff/ddd

cd ${FILES}
find . -name "*.txt" -type f -mtime +10 | xargs -r -t -I {} tar -czvf filetar.tar.gz "{}"
mv filetar.tar.gz ${ARCH}
status_check=$?
if [ ${status_check} -ne 0 ]
then
	echo "Error"
else
	echo "Success"
	find . -name "*.txt" -type f -mtime +10 -exec rm -f {} \;
fi

It will print all files being added to the gzip file!
Code:
# find . -name "*.txt" -type f -mtime +10 | xargs -r -t -I {} tar -czvf filetar.tar.gz "{}"
tar -czvf filetar.tar.gz ./teste2.txt
./teste2.txt
tar -czvf filetar.tar.gz ./teste.txt
./teste.txt
# ls -l filetar.tar.gz
-rw-rw-r--  1 jboss jboss 154 Nov 16 15:01 filetar.tar.gz

I hope it heps! =o)
# 7  
Old 11-16-2011
Quote:
Originally Posted by stunnerz_84
@agama..I did try your code..It printed out all the files (including zip files) in that directory and created a zip of all the mentioned files..But what i am not sure is..how would this help me out...Please make me understand!!!

Thanks Again!!!!
My sample was to illustrate how using xargs with tar could lead to undesired results if xargs needs to "split" the command.

If the output from the find is more than can be placed on a single command, xargs will execute the command given (tar in your case) multiple times. With each execution the output file will be created and when finished it will contain only the files that were placed on the last command started by xargs. You may not be running into the limit now, and you might not ever hit the limit, but you could and the only symptom would be a "partial" backup.


Did you check the table of contents of the file that it created? When I ran it on my system the tar file contained only the last file printed.

---------- Post updated at 18:29 ---------- Previous update was at 18:05 ----------

I've been playing round with ways to safely use tar with xargs... I think this should work for you and will eliminate the xargs risk:

Code:
find . -name "*.txt" -type f -mtime +10 | xargs -r tar -rzvf filetar.tar.gz

Yes, there are two -r options. The one for xargs is as decribed before. Replacing the -c option on the tar command with -r causes the archive to be updated (appended to) if it exists. I tested this on both FreeBSD and Linux.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Debian

Need a Debian 8 jessie archive, or a solution to create from cdrom.

I need a debian 8 jessie mipsel for create packages for my vuduo(the system is identical to a Debian 8 mipsel environment). I have some problems. I usually use Slackware14.2+crosscompile. For some packages (tcpdump,rsync) works without problem, for other(extundelete for example) not So I decide... (1 Reply)
Discussion started by: Linusolaradm1
1 Replies

2. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

3. Shell Programming and Scripting

Create empty files from a list on file

Hello Guys. Please I would like to create empty files from a list In file1 will be the followin values, so i will like to create for each name a empty file. file1 2191off-r0.sps 2192off-r0.sps 2193off-r0.sps 2194off-r0.sps 2195off-r0.sps So I need to get 5 empty files. Thanks for... (7 Replies)
Discussion started by: jiam912
7 Replies

4. Shell Programming and Scripting

Create archive and nil the content of log file using script

Plese help I need a urgent requirement. Ex: test.log requirement : using shell script I need to archive the log file and nil and the content of (test.log) file to 0 kb and then in the archive folder log files are name to test.tar test1.tar test2.tar EX: /home/abc/ test.log ... (1 Reply)
Discussion started by: johney1981
1 Replies

5. UNIX Desktop Questions & Answers

how to create empty wav file

Dear All, Kindly explain me a command in unix to create a empty wav file with example. Thanks in Advance! (1 Reply)
Discussion started by: thillai_selvan
1 Replies

6. Shell Programming and Scripting

script assistance needed - create an archive of INI files

First and foremost - me != unix bubba. Here is the situation. We have a box with data AND settings in the same directory path. (Data files aren't in the SAME directories as settings.) I need a script that generates a tarred-up archive of only the INI files with the directory structure. We... (2 Replies)
Discussion started by: hindesite
2 Replies

7. UNIX for Dummies Questions & Answers

create tar archive without preserving directory structure?

I am adding some individual files to a tar archive and would like them to be added to the archive without any directory hierarchy, even though the files themselves exist in levels of hierarchy. Unfortunately, tar seems to always preserve the directory hierarchy when it adds the files. Here is... (2 Replies)
Discussion started by: Special_K
2 Replies

8. Shell Programming and Scripting

simultaneously create three empty files?

I can't get touch to simultaneously create three empty files file1, file2, file3. I tried:$ touch filebut all I got was one file:$ fileWhat did I do wrong? (4 Replies)
Discussion started by: na5m
4 Replies

9. UNIX for Dummies Questions & Answers

create a archive of old file

i need a script which will create a archive of the files older than 10 days........ (2 Replies)
Discussion started by: jayaramanit
2 Replies

10. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question