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
# 8  
Old 11-17-2011
Hi Guys,
Sorry for the late reply...Thanks for your effort!!

@agama, @corona...I did try adding -r option.

This is how i have my code now!!
Code:
func()
{
cd ${dir}
`find . -mtime +90 -type f -name "*.txt" | xargs -r tar -rzvf filename.tar.gz`
`mv -f filename.tar.gz $archive`
status_check=`echo $?`
if [ $status_check -ne 0 ]; then
  echo "Archiving Unsuccessful"
else
  echo "Archiving Successful"
  `find . -mtime +90 -type f -name "*.txt" | xargs -exec rm -f {} \;`
fi
}

Since there were no files for the code to process, this is what is the output:
Code:
mv: cannot stat `filename.tar.gz': No such file or directory

Archiving Unsuccessful



Thanks!!!

---------- Post updated at 01:18 PM ---------- Previous update was at 01:16 PM ----------

Hi Guys,
Sorry for the late reply...Thanks for your effort!!

@agama, @corona...I did try adding -r option.

This is how i have my code now!!
Code:
func()
{
cd ${dir}
`find . -mtime +90 -type f -name "*.txt" | xargs -r tar -rzvf filename.tar.gz`
`mv -f filename.tar.gz $archive`
status_check=`echo $?`
if [ $status_check -ne 0 ]; then
  echo "Archiving Unsuccessful"
else
  echo "Archiving Successful"
  `find . -mtime +90 -type f -name "*.txt" | xargs -exec rm -f {} \;`
fi
}

Since there were no files for the code to process, this is what is the output:
Code:
mv: cannot stat `filename.tar.gz': No such file or directory


Archiving Unsuccessful



Thanks!!!

Last edited by Franklin52; 11-17-2011 at 01:47 PM.. Reason: Please use code tags for data and code samples, thank you
# 9  
Old 11-17-2011
Why do you have all your commands in backticks? At best the backticks are useless, at worst they try to run tar's error messages as commands and cause their own errors.

Don't mv the file unless you know tar succeeded. Check first.

Don't mv the file unless you know tar did anything. Check first.

You can't use -r and -z at the same time, compress it after.

And certainly don't delete the files unless you know they're in the archive!

Code:
func()
{
        cd ${dir}
        if ! find . -mtime +90 -type f -name "*.txt" | xargs -r tar -rvf filename.tar
        then
                echo "Couldn't archive" >&2
                return 1
        fi

        [ -f "filename.tar" ] || return 1
        gzip "filename.tar" || return 1
        if ! mv -f filename.tar.gz $archive
        then
                echo "Moving Unsuccessful"
                return 1
        fi

        find . -mtime +90 -type f -name "*.txt" -exec rm '{}' ';'
}

# 10  
Old 11-17-2011
Be a little carefull.

Say this command took a while to run:

Code:
if ! find . -mtime +90 -type f -name "*.txt" | xargs -r tar -rvf filename.tar

It could be possible that some files become older than 90days while the archive step is running. These files would then be deleted without getting to the archive.

If your tar supports it, the remove-files option might be the way to go:

Code:
if ! find . -mtime +90 -type f -name "*.txt" | xargs -r tar -rvf --remove-files filename.tar

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