• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | tar -- manipulate tape archives
  1. Notes
  2. Examples
  3. Command Help (man tar)

  1. Notes
    tar {-c} [options] [-f archive-file] [files | directories]
    
    tar {-r | -u} [options] [-f archive-file] [files | directories]
    
    tar {-t | -x} [options] [-f archive-file]

    You can use the options '-z' or '-j' to either redirect the output to the gzip or bzip2 commands for compression.

    You can use gzip and gunzip to compress and decompress archive files: gzip -- compression/decompression
  2. Examples
    • Create an archive file:

      • Archive a folder:
        $ tar -cf archive1.tar folder1
        
        #List archive contents
        $ tar -tf archive1.tar
        folder1/
        folder1/file1

      • Archive only the content of the folder:
        $ tar -cf archive1.tar folder1/*
        
        #List archive contents
        $ tar -tf archive1.tar
        folder1/file1

    • List archive contents:

      • List archive contents to standard output:
        $ tar -tf archive1.tar
        folder1/
        folder1/file1

      • Use the -v option to show additional details about the archive contents:
        $ tar -tvf archive1.tar
        drwxr-xr-x  0 mtitek mtitek       0  1 Feb 20:44 folder1/
        -rw-r--r--  0 mtitek mtitek       4  3 Feb 19:56 folder1/file1

    • Extract archive contents to disk:

      • The archive contents will extracted to the folder "folder1":
        $ tar -xf archive1.tar
        
        $ ls -al folder1/
        -rw-r--r--  1 mtitek  mtitek    4  3 Feb 19:56 file1

      • Use the -m option to not extract modification time:
        $ tar -xmf archive1.tar
        
        $ ls -al folder1/
        -rw-r--r--  1 mtitek  mtitek    4  3 Feb 20:39 file1

    • Archive a file and compress it using the option '-z':
      $ tar -cvzf archive1.gz file1
      file1

    • Uncompress a file and extract the archive file using the option '-z':
      $ tar -xvzf archive1.gz -C archive1
      file1
  3. Command Help (man tar)
    The following options decide which mode the command tar will use:
    -c
    |Create a new archive containing the specified files/directories.
    
    -r
    |Like -c, but new files/directories are added to the archive.
    |Note that this only works on uncompressed archives stored in regular files.
    |The -f option is required.
    
    -u
    |Like -r, but new files/directories are added only if they have a modification date newer than the corresponding file/directory in the archive.
    |Note that this only works on uncompressed archives stored in regular files.
    |The -f option is required.
    
    -t
    |List archive contents to standard output.
    
    -x
    |Extract to disk from the archive.
    |If a file with the same name appears more than once in the archive, each copy will be extracted, with later copies overwriting (replacing) earlier copies.

    The following additional options can be used:
    -f file
    |Read/write the archive from/to the specified file.
    |The filename can be - for standard input or standard output.
    
    -C directory
    |Change the output to the specified directory.
    
    -z
    |Redirect the output to the gzip command for compression.
    
    -j
    |Redirect the output to the bgzip2 command for compression.
    
    -m
    |(x mode only)
    |Do not extract modification time.
    |By default, the modification time is set to the time stored in the archive.
    
    -w
    |Ask for confirmation for every action.
    
    -v
    |Produce verbose output.
    |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.
© 2025  mtitek