Skip to main content
Engineering LibreTexts

12-F.19: tar / cpio / dd

  • Page ID
    43140
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The tar Command

    The Linux command tar stands for tape archive. It is used to create Archive and extract the Archive files. The tar command in Linux is one of the most important commands which is used for archiving. We can use the Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

    Syntax:

    tar [ OPTIONS ] [archive-file] [file or directory to be archived]

    Options:

    Options Option Meaning
    -c Creates archive
    -x Extracts the archive
    -f Creates archive with given filename
    -t Displays or lists files in archived file
    -v Displays Verbose Information
    -z Zip, tells tar command that creates the tar file to compress the created file using gzip

    An example of creating an uncompressed tar file using option -cvf: This command creates a tar file called file.tar which is the archive of all .c files in current directory.

    pbmac@pbmac-server $ tar -cvf file.tar *.c

    Because the -v option was specified, each filename is echoed to the screen as it is included in the tar file:

    os2.c
    os3.c
    os4.c
    

    Extracting files from archive using option -xvf: This command extracts files from tar file.

    pbmac@pbmac-server $ tar -xvf file.tar

    Because the -v option was specified, each filename is echoed to the screen as it is extracted from the tar file:

    os2.c
    os3.c
    os4.c
    

    The following command shows gzip compression on the tar file, using option -z: This command creates a tar file called Cfiles.tar.gz of all the .c files in the current directory.

    pbmac@pbmac-server $ tar -cvzf Cfiles.tar.gz *.c

    Showing the extraction of a gzip tar Cfiles.tar.gz using option -xvzf.

    pbmac@pbmac-server $ tar -xvzf Cfiles.tar.gz

    The dar Command

    The dar command can store a backup in several files (called "slices" in the following) of a given size, eventually pausing or running a user command/script before starting the next slice. This can allow, for example, the burning of the last generated slice on a DVD-R(W), Blue-ray Disk, or changing of USB key before continuing on the next one. Like its grand-brother, the great "tar" command, dar may also use compression, with the difference that compression is used inside the archive to be able to have compressed slices of the defined size.

    But the most important feature of dar is its ability to make differential, incremental and decremental backups. In other words, backups that contain only new files or files that have changed from a backup of reference. Binary delta is available but not activated by default: in combination with differential and incremental backups, it leads not only to not save a file that has not changed (something dar does without binary delta), but also to only save an rsync patch of any modified file, which lead to even smaller backups.

    The cpio Command

    The cpio command is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files.

    The following archive formats are supported: binary, old ASCII, new ASCII, crc, HPUX binary, HPUX old ASCII, old tar, and POSIX.1 tar. The tar format is provided for compatibility with the tar program. By default, cpio creates binary format archives for compatibility with older cpio programs. When extracting from archives, cpio automatically recognizes which kind of archive it is reading and can read archives created on machines with a different byte-order.

    • Copy-out mode: Copy files named in name-list to the archive.
      cpio -o < name-list > archive
      

      To create a *.cpio archive with selected files: We can create *.cpio files containing specific files with the help of cpio command. In the example we are using .txt files.

      pbmac@pbmac-server $ find . -iname "*.txt" | cpio -ov > archive
    • Copy-in mode: Extract files from the archive.
      cpio -i < archive

      To extract a *.cpio file: We can extract *.cpio files containing files and directory with the help of cpio command.

      cpio -iv < archive
    • Copy-pass mode: Copy files named in name-list to destination-directory.
      cpio -p destination-directory < name-list

    The dd Command

    The dd is a command-line utility for Linux whose primary purpose is to convert and copy files.

    • On Linux, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
    • dd can also read and/or write from/to these files, provided that function is implemented in their respective drivers
    • As a result, dd can be used for tasks such as backing up the boot sector of a hard drive and obtaining a fixed amount of random data.
    • The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

    Some practical examples on dd command :

    1. To backup the entire hard disk: To back up an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.
      pbmac@pbmac-server $ dd if = /dev/sda of = /dev/sdb
      
      • “if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
      • If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
      • Input file and output file should be mentioned very carefully. Just in case you mention source device in the target and vice versa, you might lose all your data.
      • To copy hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.
        pbmac@pbmac-server $ dd if = /dev/sda of = /dev/sdb conv=noerror, sync
        
    2. To backup a partition : You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command.
      pbmac@pbmac-server $ dd if=/dev/hda1 of=~/partition.img
      
    3. To create an image of a hard disk: Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices. There are many advantages of backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe. It creates the image of a hard disk /dev/hda.
      pbmac@pbmac-server $ dd if = /dev/hda of = ~/hdadisk.img
      
    4. To restore using the hard disk image: To restore a hard disk with the image file of a another hard disk, the following dd command can be used:
      pbmac@pbmac-server $ dd if = hdadisk.img of = /dev/hdb
      

      The image file hdadisk.img file is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

    5. dd command reads one block of input and processes it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048 bytes as a block size in the above command.

       

    Adapted from:
    "tar command in Linux with examples" by Akansh Gupta., Geeks for Geeks is licensed under CC BY-SA 4.0
    "cpio command in Linux with Examples" by Mandeep_Sheoran, Geeks for Geeks is licensed under CC BY-SA 4.0
    "‘dd’ command in Linux" by Kishlay Verma, Geeks for Geeks is licensed under CC BY-SA 4.0


    12-F.19: tar / cpio / dd is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?