Creating A tar File in Linux Via Command Line Options

How do I create a tar file under Linux using command line options?

You need to use the tar command to create an archive (also known as tar ball) under Linux operating systems. The tar command can create and manipulate archive files. It can even extract files from zip, ar, cpio, shar, ISO 9660 cdrom images, pax and other archives.

What is an archive?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 5m

An archive is nothing but you bundle (put) many files together into a single file on a single tape or disk. You can restore individual files from the archive on any Unix-like systems.

Linux tar command syntax

The syntax is as follows to create a tar file:

tar -cvf output.tar /dirname


tar -cvf output.tar /dirname1 /dirname2 filename1 filename2


tar -cvf output.tar /home/vivek/data /home/vivek/pictures /home/vivek/file.txt


tar -cvf /tmp/output.tar /home/vivek/data /home/vivek/pictures /home/vivek/file.txt


  • -c : Create a tar ball.
  • -v : Verbose output (show progress).
  • -f : Output tar ball archive file name.
  • -x : Extract all files from archive.tar.
  • -t : Display the contents (file list) of an archive.

Linux tar Command Examples

Create a tar ball called /tmp/data.tar for /home/vivek/data directory, enter:

tar -cvf /tmp/data.tar /home/vivek/data

To View a Tar Ball Contains (list file inside a tar ball)

Type the following command:

tar -tvf /tmp/data.tar

To Extract a Tar Ball

Type the following command to extract /tmp/data.tar in a current directory, enter:

tar -xvf /tmp/data.tar

Type the following command to extract resume.pdf file from an archive called data-backup.tar in a current directory, enter:

tar -xvf data-backup.tar resume.pdf

Type the following command to extract pic1.jpg, file1.doc, and files from an archive called data-backup.tar in a current directory, enter:

tar -xvf data-backup.tar pic1.jpg file1.doc foo.movf

Type the following command to extract /tmp/data.tar in a directory called /home/sales/data, enter:

tar -xvf /tmp/data.tar -C /home/sales/data

How To Backup Files To a Tape Device /dev/st0?

You need to use the combination of mt and tar command to control magnetic tape drive operation.

## Backup /home/ directories to a tape drive called /dev/st0
tar -czf /dev/st0 /home/

See our previous tutorial “Linux Tape Backup With mt And tar Command Howto” for more information. I also suggest that you read Linux tar(1) command man page for all other syntax and options.

# Additional correction by Khaled Z; Editing by VG – log #

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Raj Apr 2, 2013 @ 22:56

    To View a Tar Ball Contains (list file inside a tar ball)

    Type the following command:

    tar -ctvf /tmp/data.ta

    This should be tar -tvf /tmp/data.tar

  • kman Nov 1, 2013 @ 9:46

    Dear all,
    I have problem when creating tar file. I want to zip file in /srv/backup/
    tar -czf /mnt/myfile.tar.gz /srv/backup/
    but i got error” tar: Removing leading `/’ from member names”


    • gaurav Nov 6, 2013 @ 8:38

      use -P option with -czf , then you will not have this error.
      #tar -Pczf /mnt/myfile.tar.gz /srv/backup/

      -P option can be used to create a tar archive without stripping leading β€˜/’s from member names . It will not change member names now and use absolute names.

  • Safa Dec 23, 2015 @ 13:47

    How To restore Files from a Tape Device /dev/nst0?

    I entered a command like the following:
    #tar -tvf /dev/nst0 xorg.conf
    But it couldn’t……
    Please help me how can I extract it? Thanks.

  • Joseph Jan 18, 2016 @ 16:33

    It came out to be a life saver :) Thanks.

  • jesusguevarautomotriz Feb 21, 2016 @ 18:01

    Here is another tar error, researching the multi volume tar, but I do not work for me

    $ vim

    #! /bin/bash
    # For this script it’s advisable to use a shell, such as Bash,
    # that supports a TAR_FD value greater than 9.

    echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE.

    name=`expr $TAR_ARCHIVE : ‘\(.*\)-.*’`
    case $TAR_SUBCOMMAND in
    -c) ;;
    -d|-x|-t) test -r ${name:-$TAR_ARCHIVE}-$TAR_VOLUME || exit 1
    *) exit 1

    echo ${name:-$TAR_ARCHIVE}-$TAR_VOLUME >&$TAR_FD

    $ chmod 755

    $ tar -c -L650M -f cd-disk.tar -F new-volume Videos
    tar: new-volume: Cannot exec: No such file or directory
    tar: Error is not recoverable: exiting now
    tar: β€˜new-volume’ command failed
    tar: Error is not recoverable: exiting now

    This creates the first tar file named cd-disk.tar 650 MB correctly, but then throws an error and not continuous creaando the multivolume.

    Some clue?, Thanks.

  • jesusguevarautomotriz May 10, 2016 @ 5:03

    I finally found much help the solution to my problem and I want to share here:

    I had a problem with the script name, Someone suggested “try instead of new-volume” and then, worked well, I show a successful example:

    $ tar -c -L650M -f cdromdisk.tar -F ‘./’ -C ~/Videos .
    Preparing volume 2 of cdromdisk.tar.
    Preparing volume 3 of cdromdisk.tar-2.
    Preparing volume 4 of cdromdisk.tar-3.


    $ ls -1

    Verifying extraction

    $ tar -x -f ../../backup/cdromdisk.tar -F ‘./’ .
    Preparing volume 2 of ../../backup/cdromdisk.tar.
    Preparing volume 3 of ../../backup/cdromdisk.tar-2.
    Preparing volume 4 of ../../backup/cdromdisk.tar-3.

    Verifying, number and size of files, etc …

    $ ls -1
    Video (2008) [1080p]
    Videos Flisol

    Solved, very thanks to @raghuu.
    Fuente: How to Create Tar multi-volume by using the automatic rename script provided in the manual of GNU Tar? – Linux – LinuxSay – A Discussion Forum for Linux Enthusiasts
    Fecha de consulta: mar 10 may 2016 01:02:48 VET

  • Brad Doty May 17, 2017 @ 15:52

    In my experience “tarball” describes a compressed “tar”. You need to add z to the command options to use zip to compress your tar into a tarball. (f has to be the last option prior to the filename argument.)

    What you have described here is a tar, not a tarball.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum