LinuxCommandLibrary

shar

Create shell archive of files

TLDR

Create a shell script that when executed extracts the given files from itself

$ shar [path/to/file1 path/to/file2 ...] > [path/to/archive.sh]
copy

SYNOPSIS

shar [OPTION]... [FILE]...

PARAMETERS

-o ARCHIVE_FILE
    Specifies the output file for the generated shell archive. If not provided, shar writes to standard output (stdout).

-u
    Uses uuencode to encode binary files before embedding them in the archive. This ensures binary data is handled correctly when transmitted over text-based channels.

-z
    Compresses files using gzip before embedding them. The resulting script will automatically decompress them upon extraction. Note: some older versions may use compress (with -Z) instead.

-p
    Preserves the original file attributes, including permissions and modification times, when the files are extracted.

-v
    Enables verbose mode, displaying diagnostic messages and the names of files as they are processed.

-s SIZE
    Splits the output into multiple smaller archives, each with a maximum size of SIZE bytes. Useful for distributing large archives over channels with size limits, like email.

-l MAX_LENGTH
    Sets the maximum line length of the output archive to MAX_LENGTH characters. Defaults to 75 characters, which is suitable for many email systems.

-a
    Appends the new archive content to an existing archive file specified with -o.

DESCRIPTION

The shar command, short for "shell archive," creates an archive of one or more files as a self-extracting shell script. When this script is executed, it recreates the original files in the current directory. This method was traditionally used for distributing software and files over email or Usenet, especially when more sophisticated archiving tools like tar or zip were not universally available or suitable for plain-text email transmission.

A shar archive typically consists of a shell script header followed by the contents of the archived files, often encoded to handle binary data (e.g., using uuencode). Upon execution, the script uses standard shell commands like sed, cat, and cut to extract and recreate the original files, preserving their names and, optionally, permissions and modification times.

While largely superseded by more efficient and secure archiving formats like tar.gz or zip for general-purpose use, shar can still be useful for simple, small collections of text files that need to be easily unpackaged on any Unix-like system with a standard shell, without requiring specific archiving utilities.

CAVEATS

shar archives pose a security risk because they are executable shell scripts. Executing an unknown shar file can potentially run arbitrary commands on your system. Always inspect the contents of a shar archive before executing it, especially if obtained from an untrusted source. Furthermore, for very large files or directories, shar is less efficient and robust than tar combined with compression utilities like gzip or bzip2.

HISTORY

The shar command dates back to the early days of Unix, becoming a standard utility for distributing software and documents in the era before widespread internet and sophisticated package managers. It was particularly popular for sharing files via Usenet newsgroups and email, where plain text was the most reliable transport mechanism. The ability to create a self-extracting script made it incredibly convenient for users to unpack files without needing specialized archiving software. While its importance has diminished with the rise of tools like tar, zip, and distributed version control systems, shar remains part of the GNU sharutils package and is still available on most Unix-like systems.

SEE ALSO

unshar(1), tar(1), gzip(1), uuencode(1), uudecode(1)

Copied to clipboard