tar command
- tar command creates tar files by converting a group of files into an archive. It also can extract tar archives, display a list of the files included in the archive, add additional files to an existing archive, and various other kinds of operations.
- tar was designed for creating archives to store files on magnetic tape, which is why its name “Tape Archive”
- There are two versions of tar, BSD tar and GNU tar, with some functional differences. Most linux systems come with GNU tar pre-installed by default.
tar syntax
- tar [operation_and_options] [archive_name] [file_name(s)]
- operation
- –create (-c) == create a new tar archive
- –extract (-x) == extract the entire archive or one or more files from an archive
- –list (-t) == display a list of the files included in the archive
- options
- –verbose (-v) == show the files being processed by the tar command
- –file=archive-name (-f archive-name) == specifies the archive name
creating tar archive
- tar supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz, compress.
- It is an accepted convention to append the compressor suffix to the archive file name.
- e.g. an archive has been compressed with gzip, it should be named archive.tar.gz
1 | create an archive named archive.tar with file1 - 3 |
listing tar archives
1 | tar -tf archive.tar |
extracting tar archive
1 | tar -xf archive.tar |
adding files to existing tar archive
1 | use the --append(-r) operation |
removing files from a tar archive
1 | use the --delete operation to remove files from an archive |