Archiving And Transferring Files
Published: 2022-09-26 14:59:48

**Managing Compressed Tar Archives**
tar command _File archiving and compression is useful when backing up and transferring data over a network. One of the oldest and most common commands for creatng and working with backup archives is the tar command.

**The tar command can list the contents**

 of archives or extract their files to the current system.
Listing Options of the tar Command
The tar command expects one of the three following options:

Use the -c or --create option to create an archive.

Use the -t or --list option to list the contents of an archive.

Use the -x or --extract option to extract an archive.

Other commonly used options are:

Use the -f or --file= option with a file name as an argument of the archive to operate.

Use the -v or --verbose option for verbosity; useful to see which files get added to or extracted from the archive

**Archiving Files and Directories**

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
```

```
[user@host ~]$ ls archive.tar
archive.tar
```

The above tar command can also be executed using the long version options.

```
[user@host ~]$ tar --file=archive.tar --create file1 file2 file3
```

To create the tar archive named, /root/etc.tar, with the /etc directory as content as user root:

```
[root@host ~]# tar -cf /root/etc.tar /etc
tar: Removing leading `/' from member names
[root@host ~]#
```


**Listing Contents of an Archive**

The t option directs tar to list the contents (table of contents, hence t) of the archive. Use the f option with the name of the archive to be queried. For example:

```
[root@host ~]# tar -tf /root/etc.tar
etc/
etc/fstab
etc/crypttab
etc/mtab
...output omitted.

```


**Extracting Files from an Archive**

To restore files from the /root/etc.tar archive to the /root/etcbackup directory, run:

```
[root@host ~]# mkdir /root/etcbackup
```

```
[root@host ~]# cd /root/etcbackup
```

```
[root@host etcbackup]# tar -tf /root/etc.tar
etc/
etc/fstab
etc/crypttab
etc/mtab
...output omitted...
```

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

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
```

**Creating a Compressed Archive**

Use one of the following options to create a compressed tar archive:

-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
tar: Removing leading `/' from member names
```

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
tar: Removing leading `/' from member names
```

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
tar: Removing leading `/' from member names
```

After creating an archive, verify the content of an archive using the tf options. It is not mandatory to use the option for compression agent when listing the content of a compressed archive file. For example, to list the content archived in the /root/etcbackup.tar.gz file, which uses the gzip compression, use the following command:

```
[root@host ~]# tar -tf /root/etcbackup.tar.gz /etc
etc/
etc/fstab
etc/crypttab
etc/mtab
...output omitted...

```


**Extracting a Compressed Archive**

To extract the contents of a gzip compressed archive named /root/etcbackup.tar.gz in the /tmp/etcbackup directory:

```
[root@host ~]# mkdir /tmp/etcbackup
```

```
[root@host ~]# cd /tmp/etcbackup
```

```
[root@host etcbackup]# tar -tf /root/etcbackup.tar.gz
etc/
etc/fstab
etc/crypttab
etc/mtab
...output omitted...
```

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

To extract the contents of a bzip2 compressed archive named /root/logbackup.tar.bz2 in the /tmp/logbackup directory:

```
[root@host ~]# mkdir /tmp/logbackup
```

```
[root@host ~]# cd /tmp/logbackup
```

```
[root@host logbackup]# tar -tf /root/logbackup.tar.bz2
var/log/
var/log/lastlog
var/log/README
var/log/private/
var/log/wtmp
var/log/btmp
...output omitted...
```

```
[root@host logbackup]# tar -xjf /root/logbackup.tar.bz2
```

To extract the contents of a xz compressed archive named /root/sshbackup.tar.xz in the /tmp/sshbackup directory:

```
[root@host ~]$ mkdir /tmp/sshbackup
```

```
[root@host ~]# cd /tmp/sshbackup
```

```
[root@host logbackup]# tar -tf /root/sshbackup.tar.xz
etc/ssh/
etc/ssh/moduli
etc/ssh/ssh_config
etc/ssh/ssh_config.d/
etc/ssh/ssh_config.d/05-redhat.conf
etc/ssh/sshd_config
...output omitted...
```

```
[root@host sshbackup]# tar -xJf /root/sshbackup.tar.xz
```

Listing a compressed tar archive works in the same way as listing an uncompressed tar archive.


**Transferring Files Between Systems Securely**

**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 remoteuser@remotehost's password: password yum.conf 100% 813 0.8KB/s 00:00 hosts 100% 227 0.2KB/s 00:00
```


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 remoteuser@remotehost's password: password hostname 100% 22 0.0KB/s 00:00
```

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. You must connect to the remote system as root to make sure you can read all files in the remote /var/log directory.

```
[user@host ~]$ scp -r root@remoteuser:/var/log /tmp root@remotehost's password: password...output omitted...
```

**Transferring Files Using the Secure File Transfer Program**

Just like the scp command, the sftp command uses [user@]host to identify the target system and user name. If you do not specify a user, the command will attempt to log in using your local user name as the remote user name. You will then be presented with an sftp> prompt.

```
[user@host ~]$ sftp remoteuser@remotehost remoteuser@remotehost's password: password Connected to remotehost. sftp> 
```

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 ~]$