LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-14-2007, 09:00 PM   #1
pueblonative
Member
 
Registered: Jun 2006
Location: Pueblo, Colorado
Distribution: OpenSuse
Posts: 55

Rep: Reputation: 15
tutorials on tar and date


I've been looking all over for tutorials on tar, but I can't seem to find one that I can understand. For instance, if I attempt to run the command:
tar --directory=$HOME/Documents/WiPs fz --create myfirstbackup.tar.gz

I get this response
tar: You must specify one of the `-Acdtrux' options
Try `tar --help' or `tar --usage' for more information.

I know what I'm doing wrong is inately obvious, but I can't see it at this point. What I want to do is get this to the point where I can cron this into a job that runs automatically and makes a file named based on the date (eg backup01132007.tar.gz).


Update: found out that I needed to switch the directory and the filename around. Seems a bit backwards to me, from the programs I've come across, but hey it works. Now two questions:

1. How do I compress down the directories so they only include the ones with files and don't create "home","jmeyer" when they are only part of the name.
2. How can I extract the month, day and year from date (I've tried +FORMAT, doesn't seem to work)
 
Old 01-14-2007, 09:27 PM   #2
Ynot Irucrem
Member
 
Registered: Apr 2005
Location: Perth, Western Australia
Distribution: Debian
Posts: 233

Rep: Reputation: 30
You can use ~ instead of $HOME, that way the shell can still autocomplete for you when you press tab.

The man or info pages are always a good place to start for help on a program ("man <programname>" or "info <programname>").

With date, are you literally typing +FORMAT? FORMAT is a placeholder for the format you want, so do e.g. "date +%A" prints "Monday". This is in the man page for date, and strftime (as led to by the date man page).
 
Old 01-14-2007, 10:47 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Quote:
Originally Posted by pueblonative
I've been looking all over for tutorials on tar, but I can't seem to find one that I can understand. For instance, if I attempt to run the command:
tar --directory=$HOME/Documents/WiPs fz --create myfirstbackup.tar.gz

I get this response
tar: You must specify one of the `-Acdtrux' options
Try `tar --help' or `tar --usage' for more information.
If your using GNU utilities, you can generally find good info in the texinfo database (e.g., by typing “info tar” in the terminal). I don’t think what you’re doing wrong is insanely obvious, because what you’re trying to do is insanely unobvious . Here’s what I think you want to do: You have a directory “$HOME/Documents/WiPs/fz”, which you want to backup. You want to keep fz as your highest level directory in the backup file. You intend to call the backup file myfirstbackup.tar.gz. The funny thing about tar is the myriad ways to use it. A couple ways to accomplish what you want is
Code:
tar --directory=$HOME/Documents/WiPs --create --gzip --file myfirstbackup.tar.gz fz
tar -C $HOME/Documents/WiPs -c -z -f myfirstbackup.tar.gz fz
Of course the “--gzip” or “-z” options may or man not be required, depending on what version of tar you are running, and whether it was built to recognize file extensions.
 
Old 01-15-2007, 12:33 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
One of the things that I see is that the -f option is not followed by the filename.
tar -czf myfile.tar *.php is OK but tar -cfz myfile.tar *.php is not correct.
 
Old 01-15-2007, 01:08 AM   #5
pueblonative
Member
 
Registered: Jun 2006
Location: Pueblo, Colorado
Distribution: OpenSuse
Posts: 55

Original Poster
Rep: Reputation: 15
Cool. Thanks for the help.
I got the format thing (sometimes you follow instructions a leetle too literally), now just need to learn how to store those things into variables to create the filename.
 
Old 01-15-2007, 01:13 AM   #6
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by pueblonative
I've been looking all over for tutorials on tar, but I can't seem to find one that I can understand. For instance, if I attempt to run the command:
tar --directory=$HOME/Documents/WiPs fz --create myfirstbackup.tar.gz

I get this response
tar: You must specify one of the `-Acdtrux' options
Try `tar --help' or `tar --usage' for more information.
The first argument to the tar command must be the action that you want it to take. Therefore you must tell it to either create, extract, append, or whatever. In your example the first parameter that you gave it was the directory. That doesn't follow the requirement to tell it what to do first.

Quote:
Originally Posted by pueblonative
Update: found out that I needed to switch the directory and the filename around. Seems a bit backwards to me, from the programs I've come across, but hey it works. Now two questions:
One thing that confused me when I first started to use the tar command is that the f parameter is always followed by the name of the archive file.

Quote:
Originally Posted by pueblonative
1. How do I compress down the directories so they only include the ones with files and don't create "home","jmeyer" when they are only part of the name.
Do you mean that if /home/jmeyer is empty it won't make an entry in the archive for the jmeyer directory? I don't know that the tar utility is that smart. Besides, what's the harm? If any file, including an empty directory, is in the directory tree to be archived then it will be backed up. You can have exclusions based on the name of files and directories but I am not aware of any way to have it test for files in a directory and then to take some action based on that test.

Quote:
Originally Posted by pueblonative
2. How can I extract the month, day and year from date (I've tried +FORMAT, doesn't seem to work)
The date function has a million ways to print the date. Use the man page to see what it can do. Here is an example to display the hour, minute, second, month, day, and year with spaces between all of the values.
Code:
date +"%H %M %S %e %B %Y"
01 10 34 15 January 2007
Here is the same thing with colons between the hour, minute, and seconds.
Code:
date +"%H:%M:%S %e %B %Y"
01:12:41 15 January 2007
Here is the same thing with the day of the week added on to the end.
Code:
date +"%H:%M:%S %e %B %Y %a"
01:14:23 15 January 2007 Mon
And lastly here is an example using a verbal time.
Code:
date -d tomorrow
Tue Jan 16 01:15:23 EST 2007

Last edited by stress_junkie; 01-15-2007 at 01:17 AM.
 
Old 01-15-2007, 01:42 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
If you use the find command to provide the filelist to tar, you can prune -empty directories.

The GNU findutils and tar source contain the .texi source for the info manuals. You might consider producing pdf or dvi documents from the source. Often there is a "make pdf" or "make ps" target after running "./configure". Some of the manuals are very long and comprehensive.

If memory serves, the tar source ( I was going to write tar tarball but decided that would be too cute ) has a manual "Backing Up Files".

Last edited by jschiwal; 01-15-2007 at 01:51 AM.
 
Old 01-15-2007, 02:14 AM   #8
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
1. How do I compress down the directories so they only include the ones with files and don't create "home","jmeyer" when they are only part of the name.
If you meant by this that archiving directory example (and it's contents) from within /home/jmeyer/example/ so that the tar archive only includes the directory example and not directory home under which is directory jmeyer under which is the wanted directory example, then one way is to cd straight into the /home/jmeyer directory first, and then do the ordinary tar command but only with example/ as the to-be-archived target, not the full path /home/jmeyer/example (did I make it unclar enough?)

An example: if you usually ran
Code:
tar ... -f /home/jmeyer/example
you would now run something like this:
Code:
cd /home/jmeyer && tar ... -f example
Or if you didn't mean this at all, forget what I said
 
Old 01-16-2007, 07:55 AM   #9
pueblonative
Member
 
Registered: Jun 2006
Location: Pueblo, Colorado
Distribution: OpenSuse
Posts: 55

Original Poster
Rep: Reputation: 15
Okay, I finally fixed all of the errors, I think. Here's the script:

myfilename="backup$(date +%m%d%y).tar.gz"
#echo $myfilename
cd $HOME/Documents && tar czf $HOME/$myfilename WiPs

Now I just need to cron this thing.
 
  


Reply


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
tar -N (newer that Date) problem johnny_cashier Linux - General 2 01-11-2006 09:08 AM
tar a date range rayia Programming 3 01-03-2006 08:52 AM
Getting date format right in tar incremental backup jonr Linux - Software 3 08-19-2004 10:53 AM
tar file name with system date mnauta Linux - General 4 12-19-2003 05:48 PM


All times are GMT -5. The time now is 06:10 AM.

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