Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Previous: short create, Up: create


2.6.5 Archiving Directories

You can archive a directory by specifying its directory name as a file name argument to tar. The files in the directory will be archived relative to the working directory, and the directory will be re-created along with its contents when the archive is extracted.

To archive a directory, first move to its superior directory. If you have followed the previous instructions in this tutorial, you should type:

     $ cd ..
     $

This will put you into the directory which contains practice, i.e., your home directory. Once in the superior directory, you can specify the subdirectory, practice, as a file name argument. To store practice in the new archive file music.tar, type:

     $ tar --create --verbose --file=music.tar practice

tar should output:

     practice/
     practice/blues
     practice/folk
     practice/jazz
     practice/collection.tar

Note that the archive thus created is not in the subdirectory practice, but rather in the current working directory—the directory from which tar was invoked. Before trying to archive a directory from its superior directory, you should make sure you have write access to the superior directory itself, not only the directory you are trying archive with tar. For example, you will probably not be able to store your home directory in an archive by invoking tar from the root directory; See absolute. (Note also that collection.tar, the original archive file, has itself been archived. tar will accept any file as a file to be archived, regardless of its content. When music.tar is extracted, the archive file collection.tar will be re-written into the file system).

If you give tar a command such as

     $ tar --create --file=foo.tar .

tar will report ‘tar: ./foo.tar is the archive; not dumped’. This happens because tar creates the archive foo.tar in the current directory before putting any files into it. Then, when tar attempts to add all the files in the directory . to the archive, it notices that the file ./foo.tar is the same as the archive foo.tar, and skips it. (It makes no sense to put an archive into itself.) GNU tar will continue in this case, and create the archive normally, except for the exclusion of that one file. (Please note: Other versions of tar are not so clever; they will enter an infinite loop when this happens, so you should not depend on this behavior unless you are certain you are running GNU tar.)

 
 
  Published under the terms of the GNU General Public License Design by Interspire