Create a TAR archive of a directory

Contributor Icon Contributed by qmchenry Date Icon July 16, 2003  
Tag Icon Tagged: UNIX

A common task in UNIX operating systems is creating an archive of many files or directories in a single file. The tar command is one option for archiving and has many options and can do many things.


In a simple example, you can create a file called archive.tar of all the files and subdirectories in the current directory with the following command:

tar cf archive.tar *

Note that this file is different from a zip archive commonly used in Windows environments because it is uncompressed.

Previous recipe | Next recipe |
 

Viewing 5 Comments

    • ^
    • v
    The example should be tar -cf archive.tar * (note the hyphen)
    • ^
    • v
    It may look funny without it, but the hyphen is optional. The traditional syntax for tar does not use the hyphen for command line options that we've become used to using. Likewise, the GNU double hyphen options are a relatively new addition to UNIX.

    For example, here is an excerpt from the Solaris 9 man page for tar:
    tar c [ bBeEfFhiklnopPqvwX@ [0-7]] [block] [tarfile]
    [exclude-file] {-I include-file | -C directory | file |
    file} ...

    I don't know when the hyphen options were implemented in tar, but I suspect that there are still versions of tar that would gag on a -cf. The hyphenless syntax should work everywhere.
    • ^
    • v
    NAME
    tar - The GNU version of the tar archiving utility

    SYNOPSIS
    tar [ - ] A --catenate --concatenate | c --create | d --diff --compare | r --append | t --list | u
    --update | x -extract --get [ --atime-preserve ] [ -b, --block-size N ] [ -B, --read-full-blocks ] [ -C,
    --directory DIR ] [ --checkpoint ] [ -f, --file [HOSTNAME:]F ] [ --force-local ] [ -F, --info-script
    F --new-volume-script F ] [ -G, --incremental ] [ -g, --listed-incremental F ] [ -h, --dereference ] [
    -i, --ignore-zeros ] [ -j, -I, --bzip ] [ --ignore-failed-read ] [ -k, --keep-old-files ] [ -K, --start-
    ing-file F ] [ -l, --one-file-system ] [ -L, --tape-length N ] [ -m, --modification-time ] [ -M, --multi-
    volume ] [ -N, --after-date DATE, --newer DATE ] [ -o, --old-archive, --portability ] [ -O, --to-stdout ]
    [ -p, --same-permissions, --preserve-permissions ] [ -P, --absolute-paths ] [ --preserve ] [ -R,
    --record-number ] [ --remove-files ] [ -s, --same-order, --preserve-order ] [ --same-owner ] [ -S,
    --sparse ] [ -T, --files-from=F ] [ --null ] [ --totals ] [ -v, --verbose ] [ -V, --label NAME ] [
    --version ] [ -w, --interactive, --confirmation ] [ -W, --verify ] [ --exclude FILE ] [ -X,
    --exclude-from FILE ] [ -Z, --compress, --uncompress ] [ -z, --gzip, --ungzip ] [ --use-compress-
    program PROG ] [ --block-compress ] [ -[0-7][lmh] ]

    filename1 [ filename2, ... filenameN ]

    directory1 [ directory2, ...directoryN ]
    • ^
    • v
    Yeah, even with GNU tar, the hyphen is optional as indicated by the square brackets around the hyphen like [ - ] .. there are so many scripts and old timers like me around who user tar with the old hyphenless syntax that it would be miserable if it didn't work anymore.
    • ^
    • v
    How to tar a folder which contains list subfolders in unix.Please get back to me immediately<font color="red"></font>
 
close Reblog this comment
blog comments powered by Disqus