Posted By

klovera on 10/19/11


Tagged


Versions (?)

tar


 / Published in: Bash
 

URL: http://lowfatlinux.com/linux-tar.html

  1. To create an archive using tar, use a command like this, which bundles all the files in the current directory that end with .doc into the alldocs.tar file:
  2. tar cvf alldocs.tar *.doc
  3.  
  4. Here's a second example, which creates a tar file named panda.tar containing all the files from the panda directory (and any of its subdirectories):
  5.  
  6. tar cvf panda.tar panda/
  7.  
  8. In these examples, the c, v, and f flags mean create a new archive, be verbose (list files being archived), and write the archive to a file. You can also create tar files on tape drives or floppy disks, like this:
  9.  
  10. tar cvfM /dev/fd0 panda Archive the files in the panda directory to floppy disk(s).
  11. tar cvf /dev/rmt0 panda Archive the files in the panda directory to the tape drive.
  12.  
  13. The /dev/fd0 entry is Linux-ese for "floppy drive zero" (your A drive under DOS), and /dev/rmt0 means "removable media tape zero," or your primary tape drive. The M flag means use multiple floppy disks--when one disk is full, tar prompts you to insert another.
  14.  
  15. To automatically compress the tar file as it is being created, add the z flag, like this:
  16.  
  17. tar cvzf alldocs.tar.gz *.doc
  18.  
  19. In this example, I added the .gz suffix to the archive file name, because the z flag tells tar to use the same compression as the gzip command.
  20.  
  21. To list the contents of a tar file, use the t (type) flag in a command, like this:
  22.  
  23. tar tvf alldocs.tar List all files in alldocs.tar.
  24.  
  25. To extract the contents of a tar file, use the x (extract) flag in a command, like this:
  26.  
  27. tar xvf panda.tar Extract files from panda.tar.
  28.  
  29. This will copy all the files from the panda.tar file into the current directory. When a tar file is created, it can bundle up all the files in a directory, as well as any subdirectories and the files in them. So when you're extracting a tar file, keep in mind that you might end up with some new subdirectories in the current directory.
  30.  
  31. We've used several different flags in the sample tar commands so far. Here's a list of the most common flags:
  32.  
  33. c Create a new archive.
  34. t List the contents of an archive.
  35. x Extract the contents of an archive.
  36. f The archive file name is given on the command line (required whenever the tar output is going to a file)
  37. M The archive can span multiple floppies.
  38. v Print verbose output (list file names as they are processed).
  39. u Add files to the archive if they are newer than the copy in the tar file.
  40. z Compress or decompress files automatically.
  41.  
  42.  
  43.  
  44. Read more: http://lowfatlinux.com/linux-tar.html#ixzz1b8kwCjFL

Report this snippet  

You need to login to post a comment.