2

I have few files in sub directories, all the files are just text files like faq, user guides.There are no c/cpp src code in it. Following is the file and directory structure.

scr
|_Makefile                     #Top level Makefile
|_other_dirs
|_some_other_dirs
|_mydir
   |_Makefile                  #Makefile of mydir, need to put some code here
   |_dir1
   |  |_textfile0
   |  |_textfile1 
   |_dir2
      |_textfile2
      |_textfile3

Question, How can I tar the contents of dir1 and dir2 into one tar ball? I tried searching over internet about the Makefile and how to use it to create the take ball from top Makefile but no success yet. I am not very familiar with Makefiles, any starting point will be appreciated. Thanks.

Following is my novice attempt to have a very basic Makefile:

 -->cat Makefile
mydir.tgz : *
    tar -zcvf mydir.tgz mydir/

 -->make
Makefile:1: *** missing separator.  Stop.

Idea is to run top Makefile and have tar file generated for mydir.

monk
  • 1,953
  • 3
  • 21
  • 41
  • Your question has a lot in common with [Makefile rule that depends on all files under a directory (including within subdirectories)](https://stackoverflow.com/q/14289513/1380680). – Reinier Torenbeek Dec 21 '18 at 23:35
  • Missing separator means you didn't use TAB to indent the action line. https://stackoverflow.com/questions/16931770/makefile4-missing-separator-stop – Barmar Dec 21 '18 at 23:35
  • thanks, `tab` was the problem. – monk Dec 21 '18 at 23:50
  • @monk Your current approach will not properly detect when the tar-file has to be remade or not. If you add a file in one of the subdirectories, then `make` will not re-create the tar file. – Reinier Torenbeek Dec 21 '18 at 23:56

1 Answers1

4

You can add all files and directories in mydir recursively as a prerequisite of mydir.tgz. That way, your tar file will be executed if, and only if, a change occurs somewhere under mydir. For example like this:

mydir.tgz: $(shell find mydir)
    tar -zcvf mydir.tgz mydir

The line with the tar command should start with at TAB.

Most of the mechanisms of this answer are also described in this SO question, but it seemed to make sense to me to add it here to concisely answer your specific question.

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
  • 1
    To handle deleted files, you need to include the timestamp of directories. – o11c Dec 21 '18 at 23:56
  • @o11c I have changed my answer. I think it is better and simpler now, thanks to you :-) – Reinier Torenbeek Dec 22 '18 at 00:44
  • Is it possible to use the `ar` syntax of make to do the same, you know syntax is `archive(member)` compatible with ar, but is it so with tar? – Sandburg Aug 21 '19 at 15:56
  • @Sandburg Out of the box that does not seem to be the case. However, the `gmake` manual section [11.4 Suffix Rules for Archive Files](https://www.gnu.org/software/make/manual/make.html#Archive-Suffix-Rules) looks like a promising mechanism to support using `tar` in stead of `ar` to with the archive syntax. – Reinier Torenbeek Aug 21 '19 at 16:15