Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Archiving script

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
CA
Hello All,

I have couple of files named as follows:

file1.txt
file2.txt

I am intrested in zipping them up, tar, add date and move to another directory to make it look as follows:

allfiles.May.tgz

Here is my effort: This file is named myscript.sh
#!/usr/bin/sh

tar -czvf logfiles.date.tgz files1.txt files2.txt

mv logfiles.May.tgz new_location/

exit

HOwever, I am having some problems. Also, how can i ran this shell script without typeing:
bash myscript.sh
 
like this?

Code:
#!/usr/bin/bash

MONTH=`date '+%b'`

tar -cvzf logfiles.$MONTH.tgz files1.txt files2.txt
mv logfiles.$MONTH.tgz new_location
 
And you run it without typing "bash" by

chmod 755 yourscript

(or 700 yourscript if you want no one but you to be able to use it)

and then store it somewhere in your $PATH

(type "echo $PATH" and put your script in one of the directories shown)


Tony Lawrence
Linux/Unix/Mac OS X Resources
 
before taring the files, I want to check for the exsitence of the files, like so:

if ls *.log
then
echo -=- Now renaming files
# rename files
mv logfile.log logfile.$MONTH.log
mv nifty.* nifty.$MONTH.log
# move files to backup directory.
mv *.$MONTH.* $DEST
echo -=- Archiving Process completed....
else
echo -=- Log files does not exist
fi

However this does not help, please advise.
 
It would work if you just changed one line:

if `ls *.log`


If you want to suppress the output of the ls do


if `ls *.log > /dev/null 2>&1`

There are other ways to do this. For example, I might do:

count=`ls *.log 2>/dev/null | wc -l`
if [ "$count" -gt 0 ]
then
echo "$count files to back up"

.. etc.





Tony Lawrence
Linux/Unix/Mac OS X Resources
 
No need to backquote the [tt]ls[/tt]: you don't want the output of [tt]ls[/tt], just its exit code ([tt]ls[/tt] returns an error if a file does not exist).
Just remove the standard and error outputs and check the return code (it's what [tt]if[/tt] do):
Code:
if ls *.log >/dev/null 2>&1; then
    : # Here at least one .log file exists
else
    : # No .log file in current dir
fi

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

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top