LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   tutorials on tar and date (https://www.linuxquestions.org/questions/linux-newbie-8/tutorials-on-tar-and-date-519318/)

pueblonative 01-14-2007 08:00 PM

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)

Ynot Irucrem 01-14-2007 08:27 PM

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).

osor 01-14-2007 09:47 PM

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.

Wim Sturkenboom 01-14-2007 11:33 PM

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.

pueblonative 01-15-2007 12:08 AM

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.

stress_junkie 01-15-2007 12:13 AM

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

:)

jschiwal 01-15-2007 12:42 AM

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".

b0uncer 01-15-2007 01:14 AM

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 :)

pueblonative 01-16-2007 06:55 AM

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.


All times are GMT -5. The time now is 07:15 PM.