12.1 Archive and Copy Files Between Systems
12.1 Archive and Copy Files Between Systems
It is useful to store a group of files in one file for easy backup, for transfer to another directory,
or for transfer to another computer. An archive file is a collection of files and directories stored
in one file. Linux offers a rich set of protocols you can use to copy files between computers such
as scp, sftp and rsync.
Archive Files:
An archive file is a collection of files and directories that are stored in one file. The archive file is
not compressed it uses the same amount of disk space as all the individual files and directories
combined. A compressed file is a collection of files and directories that are stored in one file
and stored in a way that uses less disk space than all the individual files and directories
combined. An archive file is not compressed, but a compressed file can be an archive file.
Tar Command:
Tar stand for Tape Archiver. The tar program is used to create, maintain, modify, and extract
files that are archived in the tar format. This is a good way to create backups and archives. The
tar command does not compress the files by default. Archiving and compressing files are useful
when creating backups and transferring data across a network. A leading - is not required for
tar options it can work with - or without -.
Compression Tools:
The Archive can be compressed using compression tools gzip, bzip2, or xz. Gzip compression is
most frequently used to create (.tar.gz) or (.tgz) files, tar command also supports bzip2 and xz
compression. This allows you to create bzip2-compressed files, often named (.tar.bz2), (.tar.bz),
or (.tbz) files. The xz compressed files, named (.tar.xz). To do so, just replace the -z for gzip in
the commands here with a -j for bzip2 and -J for xz. Gzip is faster, but it generally compresses a
bit less, so the get larger file. Bzip2 is slower, but it compresses a bit more, so get a smaller file.
Tar's Options Description
-c Create a new archive (Create)
-j Compress the tar file with bzip2
-z Compress the tar file with gzip
-J Compress the tar file with xz
-x Extract files from an archive (Extract)
-v Displays Verbose Information (Verbose)
-t Lists the contents of an archive (Contents)
-f File name of the archive to work on (File Name)
-u Archives and adds to an existing archive file
-A Concatenates the archive files
-w Verify an archive file.
-C Change the extracted files Location (Change)
Copying Files:
Either a live or home server, moving files between local machines or two remote machines is a
basic requirement in Linux. There are many ways to achieve that such as SCP, SFTP and Rsync.
Commands Description
# sftp username@hostname To connect to remote host
# sftp [email protected] to connect to remote host through IP address
sftp> get remote-File Download file from remote server
sftp> get remote-File local-file Save remote server file with different name
sftp> get -r some-Directory Copy the folder and all its contents
sftp> put local-File Transfer files to the remote server
sftp> get -r Local-Directory Transfer folder to the remote server
sftp> cd Change the directory on the remote host
sftp> lcd Change the directory on the local host
sftp> pwd To check Remote server working directory
sftp> lpwd To check the Local present working directory
sftp> mget *.txt Download multiple files from remote server
sftp> mput *.txt Upload multiple files to remote server
Sftp> help or ? To get sftp commands help
Sftp> bye OR quit OR exit To exit from SFTP prompt
Commands Description
# yum install rsync Command to install Rsync
# rsync -zvh backup.tar /tmp/backups/ Copy & sync a file on local computer
# rsync -avzh /root/test /tmp/backups/ Copy & sync a folder on local computer
$ rsync -avz data/ [email protected]:/home/ Copy files from local to remote host
$ rsync -avz [email protected]: data/ /temp/bk Copying files from remote host to local