Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-25-2010, 11:33 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 20

Rep: Reputation: 0
Modify date command to show previous year when performing December backups in January

I know the subject is a little verbose, but that is exactly the trouble I am having. I have a script (many thanks to those on LQ who helped me create it) that moves files from one directory to another, based on the numeric date of the file name (i.e., 20091212 would go to the December directory). Now, since this script will be ran at the beginning of the following month (December's files to be tarred and gzipped will be performed the first day of January, and January's in February, etc.), it appears to me that the script will be tricky when it comes time to do the December files in January.

Here's part of the script:

# Define working directory and target directory.
hostname=`uname -n`

# Switch to working directory.
cd "$DIR"
echo "Entered working directory $DIR"

# Main loop.
for file in 2*.gz; do

     echo "-----------------------"
     echo "Processing file: $file."

     # Test whether file is actually a file.
     if [[ ! -f $file ]]; then
          echo "$file is not a regular file.  Skipping"

     case "${file:5:2}" in
          01)  mkdir -vp $target/January
               mv -v --target-directory $target/January $file
          02)  mkdir -vp $target/February
               mv -v --target-directory $target/February $file

####    -----This continues for the remaining months-----

*)   echo "File $file does not contain a valid month number."
               # Provides feedback for invalid hits.

echo "-----------------------"


date=`date +%B`

     case "${date}" in

          January) cd $target/December
                   tar -czvf * December.`date +%Y.$hostname
         February) cd $target/January
                   tar -czvf * January.`date +%Y.$hostname
echo "Finished"

exit 0
I can't seem to figure out a way to carry the output of the date command to the next command, and the year for the December files will always be wrong. Anyone out there who can make sense of my needs?
Old 01-25-2010, 01:11 PM   #2
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
December is the only month which will be "wrong".
So we need to do some simple maths.
     case "${date}" in

         January) cd $target/December
                   tar -czvf * December.$(( $(date +%Y) - 1 )).$hostname
         February) cd $target/January
                   tar -czvf * January.$(date +%Y).$hostname
Note $(command) is replacing the "backticks": `command` format.
Old 01-26-2010, 09:14 AM   #3
LQ Newbie
Registered: Dec 2009
Posts: 20

Original Poster
Rep: Reputation: 0
The date modification part works. Now, I seem to have poorly thought out the tarring of all the files in the directory and renaming them to the month.year.hostname format. As I have it now, the script wants to tar all the files in the directory and it looks for the file to tar, too. I want to tar all the files in the directory and name the tarball Will this entail somehow storing the files using something like {} and then give the destination file name? Will I also have to include the tar.gz extension in the renaming of the files, or will it automatically tack on the tar.gz extension?

Last edited by No_one_knows_me; 01-28-2010 at 08:54 AM.


bash, date, script, shell, tar

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
i want to know the command which show the date of creation of file abhi_singh4 Red Hat 6 09-16-2011 01:02 AM
vi help, how to modify previous command ufmale Linux - Newbie 1 09-05-2008 01:20 PM
Tar not overwriting previous backups neocookie Linux - General 6 02-13-2007 06:03 AM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 07:00 AM
date command getting previous day swinchen Programming 6 08-23-2004 01:17 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration