0% found this document useful (0 votes)
59 views5 pages

Archiving and Transferring Files: The Option Directs Tar To List The Contents (Table of Contents, Hence) of The Archive

The document discusses various methods for archiving, transferring, extracting, and synchronizing files between systems securely. It describes how to use the tar, scp, sftp, and rsync commands to archive files into compressed tar archives, copy files between local and remote systems, interactively transfer files via secure FTP, and synchronize directory contents between systems over SSH. Specific examples are provided for archiving, extracting, copying, uploading, downloading, and synchronizing files located in directories like /etc, /var/log, and between the local and remote hosts.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Archiving and Transferring Files: The Option Directs Tar To List The Contents (Table of Contents, Hence) of The Archive

The document discusses various methods for archiving, transferring, extracting, and synchronizing files between systems securely. It describes how to use the tar, scp, sftp, and rsync commands to archive files into compressed tar archives, copy files between local and remote systems, interactively transfer files via secure FTP, and synchronize directory contents between systems over SSH. Specific examples are provided for archiving, extracting, copying, uploading, downloading, and synchronizing files located in directories like /etc, /var/log, and between the local and remote hosts.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Archiving and Transferring Files

The following command creates an archive named archive.tar with the contents of file1,
file2, and file3 in the user's home directory.

[user@host ~]$ tar -cf archive.tar file1 file2 file3

The t option directs tar to list the contents (table of contents, hence t) of the archive.

[root@host ~]# tar -tf /root/etc.tar

Extracting Files from an Archive

[root@host etcbackup]# tar -xf /root/etc.tar

By default, when files get extracted from an archive, the umask is subtracted from the
permissions of archive content. To preserve the permissions of an archived file, the p option
when extracting an archive.

In this example, an archive named, /root/myscripts.tar, is extracted in the


/root/scripts directory while preserving the permissions of the extracted files:

[root@host ~]# mkdir /root/scripts


[root@host ~]# cd /root/scripts
[root@host scripts]# tar -xpf /root/myscripts.tar

 -z or --gzip for gzip compression (filename.tar.gz or filename.tgz)

 -j or --bzip2 for bzip2 compression (filename.tar.bz2)

 -J or -xz for xz compression (filename.tar.xz)

To create a gzip compressed archive named /root/etcbackup.tar.gz, with the contents


from the /etc directory on host:

[root@host ~]# tar -czf /root/etcbackup.tar.gz /etc

To create a bzip2 compressed archive named /root/logbackup.tar.bz2, with the contents


from the /var/log directory on host:

[root@host ~]$ tar -cjf /root/logbackup.tar.bz2 /var/log

To create a xz compressed archive named, /root/sshconfig.tar.xz, with the contents


from the /etc/ssh directory on host:
[root@host ~]$ tar -cJf /root/sshconfig.tar.xz /etc/ssh

List :-

List the contents of the etc.tar.gz archive before extracting.

[root@servera backuptest]# tar -tzf /tmp/etc.tar.gz

Extract :-

[root@host etcbackup]# tar -xzf /root/etcbackup.tar.gz


[root@host logbackup]# tar -xjf /root/logbackup.tar.bz2
[root@host sshbackup]# tar -xJf /root/sshbackup.tar.xz

Transferring Files Using Secure Copy

The following example demonstrates how to copy the local /etc/yum.conf and /etc/hosts
files on host, to the remoteuser's home directory on the remotehost remote system:

[user@host ~]$ scp /etc/yum.conf /etc/hosts


remoteuser@remotehost:/home/remoteuser

You can also copy a file in the other direction, from a remote system to the local file system.
In this example, the file /etc/hostname on remotehost is copyed to the local directory
/home/user. The scp command authenticates to remotehost as the user remoteuser.

[user@host ~]$ scp remoteuser@remotehost:/etc/hostname /home/user

To copy a whole directory tree recursively, use the -r option. In the following example, the remote
directory /var/log on remotehost is copied recursively to the local directory /tmp/ on host.

[user@host ~]$ scp -r root@remoteuser:/var/log /tmp

Transferring Files Using the Secure File Transfer Program

To interactively upload or download files from a SSH server, use the Secure File Transfer
Program, sftp. A session with the sftp command uses the secure authentication mechanism
and encrypted data transfer to and from the SSH server.
Just like the scp command, the sftp command uses [user@]host to identify the target system
and user name.

[user@host ~]$ sftp remoteuser@remotehost


remoteuser@remotehost's password: password
Connected to remotehost.
sftp>

The interactive sftp session accepts various commands that work the same way on the remote file
system as they do in the local file system, such as ls, cd, mkdir, rmdir, and pwd. The put command
uploads a file to the remote system. The get command downloads a file from the remote system.
The exit command exits the sftp session.

To upload the /etc/hosts file on the local system to the newly created directory
/home/remoteuser/hostbackup on remotehost. The sftp session always assumes that the
put command is followed by a file on the local file system and starts in the connecting user's
home directory; in this case, /home/remoteuser:

sftp> mkdir hostbackup


sftp> cd hostbackup
sftp> put /etc/hosts
Uploading /etc/hosts to /home/remoteuser/hostbackup/hosts
/etc/hosts 100% 227 0.2KB/s 00:00
sftp>

To download /etc/yum.conf from the remote host to the current directory on the local
system, execute the command get /etc/yum.conf and exit the sftp session with the exit
command.

sftp> get /etc/yum.conf


Fetching /etc/yum.conf to yum.conf
/etc/yum.conf 100% 813 0.8KB/s 00:00
sftp> exit
[user@host ~]$

Use the scp command to recursively copy the /etc/ssh directory from serverb to the
/home/student/serverbackup directory on servera.

[student@servera ~]$ scp -r root@serverb:/etc/ssh ~/serverbackup


Synchronizing Files Between Systems Securely

You can use rsync to synchronize the contents of a local file or directory with a file or directory on a
remote machine, using either machine as the source. You can also synchronize the contents of two
local files or directories.

For example, to synchronize contents of the /var/log directory to the /tmp directory:

[root@host ~]# rsync -av /var/log /tmp

A trailing slash on the source directory synchronizes the content of that directory without
newly creating the subdirectory in the target directory. In this example, the log directory is
not created in the /tmp directory, only the content of /var/log/ is synchronized into /tmp.

[root@host ~]# rsync -av /var/log/ /tmp

Like the scp and sftp commands, rsync specifies remote locations using the [user@]host:/path
format. The remote location can be either the source or destination system, but one of the two
machines has to be local.

[root@host ~]# rsync -av /var/log remotehost:/tmp

In the same way, the /var/log remote directory on remotehost can be synchronized to the
/tmp local directory on host:

[root@host ~]# rsync -av remotehost:/var/log /tmp

Use the rsync command to synchronize the /var/log directory tree on serverb to the
/home/student/serverlogs directory on servera. Note that only the root user can read all
the content in the /var/log directory on serverb. All files are transferred in the initial
synchronization.

[student@servera ~]$ rsync -av root@serverb:/var/log ~/serverlogs

As root on serverb, execute the logger "Log files synchronized" command to get a new
entry in the /var/log/messages log file to reflect when the last synchronization took place.

[student@servera ~]$ ssh root@serverb 'logger "Log files synchronized"'


On serverb, synchronize the /etc directory tree from servera to the /configsync directory.

rsync -av root@servera:/etc /configsync

You might also like