Compressing Files

  • Lossless. Lossless compression preserves all the data contained in the original. This means that when a file is restored from a compressed version, the restored file is exactly the same as the original, uncompressed version.
  • Lossy. Lossy compression, on the other hand, removes data as the compression is performed to allow more compression to be applied. When a lossy file is restored, it does not match the original version; rather, it is a close approximation. Examples of lossy compression are JPEG (for images) and MP3 (for music).

gzip gunzip

gzip, gunzip, zcat - compress or expand files

Option Long option Description
-c —stdout
—to-stdout
Write output to standard output and keep the original files.
-d —decompress
—uncompress
Decompress. This causes gzip to act like gunzip.
-f —force Force compression even if a compressed version of the original file already exists.
-h —help Display usage information.
-l —list List compression statistics for each file compressed.
-r —recursive If one or more arguments on the command line is a directory, recursively compress files contained within them.
-t —test Test the integrity of a compressed file.
-v —verbose Display verbose messages while compressing.
-number Set amount of compression. number is an integer in the range of 1 (fastest, least compression) to 9 (slowest, mostcompression). The values 1 and 9 may also be expressed as —fast and —best, respectively. The default value is 6.

gunzip -c foo.txt.gz = zcat foo.txt.gz
gunzip -c foo.txt.gz | less = zless foo.txt.gz

bzip2 bunzip2 bzcat bzip2recover

bzip2, bunzip2 - a block-sorting file compressor
bzcat - decompresses files to stdout
bzip2recover - recovers data from damaged bzip2 files

Archiving Files

tar

  • .tar Indicates a “plain” tar archive
  • .tgz Indicates a gzipped archive

tar mode[options] pathname...

mode

  • c Create an archive from a list of files and/or directories.
  • x Extract an archive.
    • If we are operating as the superuser, files and directories extracted from archives take on the ownership of the original owner, otherwise the user performing the restoration.
  • r Append specified pathnames to the end of an archive.
  • t List the contents of an archive.

    options

  • z Enable gzip

  • j Enable bzip2
  • v Verbose
  • f FILENAME Specify file name of the tar archive
    • cf - 将结果输出至 stdout
    • xf - 从 stdin 提取内容
  • --wildcards Enable pathnames wildcards
  • -T, --files-from=FILENAME Read pathnames from a file rather than the command line
    • --files-from=- 从 stdin 里读取 pathnames

      examples

      find playground -name 'file-A' | tar cf - --files-from=- | gzip > playground.tgz
      find playground -name 'file-A' | tar czf playground.tgz -T -
      find playground -name 'file-A' | tar cjf playground.tbz -T -
      ssh remote-sys 'tar cf - Documents' | tar xf -

      zip

      The zip program is both a compression tool and an archiver.
      zip options zipfile file...
      omitted~

      rsync Synchronizing Files and Directories

      rsync options source[/] destination
      where source and destination are one of the following:
  • A local file or directory
  • A remote file or directory in the form of [user@]host:path
  • A remote rsync server specified with a URI of rsync://[user@] host[:port]/path

    Note that either the source or the destination must be a local file. Remote-to-remote copying is not supported.

source 后面如果跟了 /,那么只同步 source 目录里的文件,而不包括 source 本身

options

  • a archiving, recursion and preservation of file attributes
  • v verbose output
  • --delete delete extraneous files from destination dirs
  • -e, --rsh=COMMAND choose an alternative remote shell program

    Local

    ```bash $ rsync -av playground foo $ ls foo playground

$ rsync -av playground/ foo # Note the trailing / $ ls foo file1.txt file2.txt

$ mkdir /media/BigDisk/backup $ sudo rsync -av —delete /etc /home /usr/local /media/BigDisk/backup

$ alias backup=’sudo rsync -av —delete /etc /home /usr/local /media/BigDisk/backup’

  1. <a name="qpKyK"></a>
  2. ### Remote
  3. ```bash
  4. $ sudo rsync -av --delete --rsh=ssh /etc /home /usr/local remote-sys:/backup
  5. $ rsync -av –delete rsync://archive.linux.duke.edu/fedora/linux/development/rawhide/Everything/x86_64/os/ fedora-devel