Tar cheatsheet

tar command

$ tar [-cxtzjvfpPN] files or directories

Arguments:

  • -c: Create a new archive containing the specified items.

  • -x: Extract to disk from the archive.

  • -t: List archive contents.

Notice:
-c/x/t Only one can be used.

  • -z: Compress the resulting archive with gzip. In extract or list modes, this option is ignored.

  • -j: Compress the resulting archive with bzip2. In extract or list modes, this option is ignored.

  • -v: In create and extract modes, tar will list each file name as it is read from or written to the archive. In list mode, tar will produce output similar to that of ls.

  • -f: Read the archive from or write the archive to the specified file.

    $ tar -zcvfP tfile sfile # Incorrect
    
    $ tar -zcvPf tfile sfile # Correct
    
  • -p: (x mode only) Preserve file permissions.


Examples:

  1. Tarring all files in /etc to /tmp/etc.tar

    $ tar -cvf /tmp/etc.tar /etc      # No compressing
    $ tar -zcvf /tmp/etc.tar.gz /etc  # Compressing with gzip
    $ tar -jcvf /tmp/etc.tar.bz2 /etc # Compressing with bzip2
    
  2. Listing files in /tmp/etc.tar.gz

    $ tar -ztvf /tmp/etc.tar.gz       # Cause we compressed with gzip, the -z is necessary
    
  3. Decompress /tmp/etc.tar.gz to /usr/local/src

    $ cd /usr/local/src
    $ tar -zxvf /tmp/etc.tar.gz
    
  4. Decompressing the etc/passwd in /tmp/etc.tar.gz to /tmp

    $ cd /tmp
    $ tar -zxvf /tmp/etc.tar.gz etc/passwd
    
  5. Tarring all files in /etc/, and keeping their permission

    $ tar -zxvpf /tmp/etc.tar.gz /etc
    
  6. Only the files later than 2005/06/01 will be tarred into /home

    $ tar -N '2005/06/01' -zcvf home.tar.gz /home
    
  7. To tar /home, /etc excluding /home/dmtsai

    $ tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
    

你可能感兴趣的:(Tar cheatsheet)