Linux - tar


tar -C dirname > archive.tar
gzip archive.tar

tar -xvzf filename.tar.gz
gzip -d -C ../rsaref20.tar.Z | tar -xvf

tar -cf archive.tar dirname

How can we create a tar archive of a folder?

tar -zcvf archiveName.tar.gz directoryName

How can we unarchive an archive?

tar -zxvf archiveName.tar.gz

What are the command line options (or keys) for the tar command?

tar [key] files
  1. r: named files are written at the end
  2. x: named files are extracted from tape
  3. t: named files are listed if they are on tap
  4. u: files are added to tape if not already there or newer since last archived
  5. c: create an archive
  6. w: interactive
  7. f: use the sub-options <pathname> as the archive
  8. -I: filter the output through bzip2
  9. -k: keep existing files. Do not override them from the archive
  10. -p: preserve permissions
  11. -P: do not strip leading '/' from filenames
  12. -s: preserve order. List of names to extract is sorted to match the archive
  13. --preserve: like -p -s
  14. --same-owner: create the extracted files with the same ownership
  15. -T F: get the names to extract from a file F
  16. -W: verify. Attempt to verify the archive after writing it.
  17. --exclude files: exclude files from the resulting archive
  18. -Z: filter the archive through compress to produce a file ending with .Z
  19. -z: filter through gzip
tar -c /dev/tape/usros /usr/os
tar -cf /dev/floppy /usr
tar -cfv /dev/floppy /home /etc/passwd /etc/group
tar -cf archive.tar dirname; gzip archive.tar

cd /chem1
tar -cf - -c /chem olddir | tar -xvpf -

The -C option of the first tar command set the current directory for the first tar command. The - argument for the -f option specifies that the output to standard output. The first tar command creates an archive of /chem/olddir and write it to standard output. The second tar command extract files from standard input ( specified by the - ) and write its current directory which is /chem1. The -p option for the second tar command tells it to preserve ownership.

tar -c /dev/tape filenames // create an arhive on a tape device
tar -cf archiveName.tar filenames // create an archive using normal file
tar -rf archiveName.tar filenames // append files to archive
tar -uf archiveName.tar filenames // only update files that are new.

tar -tf archiveName.tar filenames        // list given files in given archive
                            // omit the filenames argument to list all files in the arhive

tar -xf archiveName.tar filenames    // extract only the given files from the archive
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License