Archiving and Transferring files
Published: 2022-09-26 21:07:58

## **A. Archiving files**
     
Archiving is the process of combining multiple files and directories (same or different sizes) into one file. On Linux itself to archive files and directories using the tar program.

Tar stands for Tape archive and is used to combine multiple files into one file. Also use to create and extract archive files. You can also compress older and rarely used files and save them for future use which helps you conserve disk space. Files that have been compressed using the tar command will be saved in the form of a .tar file. Subsequent compression using gzip will result in a file in `.tar.gz` format.




##**Operate Command tar**

Here I will explain how to use operate tar in may uses.

**1. Create a new archive file**
To create a file use : 

- `-c` or `--create` means Create new archive as one file.

- `-f` or `--file` means filename that you gonna choose, with filename as archive argument to operate.

```
[student@servera ~]$ tar -cf newfile.tar file1
```
> `newfile.tar` archive created with file1 in it

If you want to know how the tar file process is created you can add

- `-v` or `--verbose`, Verbosely show the .tar file progress between tar commands

```
[student@servera ~]$ tar -cvf newfile.tar file1
```


**2. Extracting archive files**
to extract files, you can use the following command

- `-x`,`--extract`, extracting from an existing archive. 

- `-f`, `--file`,  Archive file name. put the file you want to extract

```
[student@servera ~]$ tar -xf archivefile.tar
```
> archive `archivefile.tar` is being extracted to the current directory

**3. View the table of contents of an archive file**

- `-t`,`--list`, table of contents of an archive file

- `-f`,`--file`, Archive file name.

```
[student@servera ~]$ tar -tf file.tar
```

**4. Add Files or Directories to tar Archive File in Linux**

- `-r`, means append, to add files or directories to the existing tar archive files
- `-f`, `--file`, Archive file name.

```
[student@servera ~]$ tar -rf archivefile.tar newfile1 newfile2
```

> 
newfile1 and newfile2 will go to file archivefile.tar

##**2. Compressing file**
In addition to archiving I will also discuss the program for compressing. Previously  Compression is the process of reducing the size of a file or directory. To be able to compress on a Linux system use the Zip program to run it

**Overview of tar Compression Options**

- `-z`, `--gzip`, Use gzip compression (.tar.gz).

- `-j`, `--bzip2`, Use bzip2 compression (.tar.bz2). bzip2 typically achieves a better compression ratio than gzip.

- `-J`, `--xz`, Use xz compression (.tar.xz). The xz compression typically achieves a better compression ratio than bzip2.

Here's how to use the Zip program

**Create a Zip file (compress files)**
- `-z` or `--gzip` for gzip compression
```
[student@servera ~]$ tar -czf newfile.tar.gz file1
```

- `-j` or `--bzip2` for bzip2 compression

```
[student@servera ~]$ tar -cjf newfile.tar.bz2 file1
```

- `-J` or `-xz` for xz compression

```
[student@servera ~]$ tar -cJf newfile.tar.xz file1
```

**Extract multiple files from a tar archive**

If you want to extract multiple files at once, use the following command format:

```
[student@servera ~]$ tar -zxvf file.tar.gz file1 file2
```

> This command will extract file1 file2 from the archive files. Use -z for gzip compression 

If you want to extract, view or otherwise compress the file, just add the Zip option between the tar command.

##**3. Transferring files between systems** 
The secure copy command, scp, which is part of the OpenSSH suite, copies files from a remote system to the local system or from the local system to a remote system. The format of the remote location should be in the form `[user@]host:/path`. The user@ portion of the argument is optional. If it is missing, your current local username will be used

Scp can be used to send a copy of a file to another system or vice versa to send a copy of a file from another system to our system as in the example below:

**1. Transferring copy files to other systems safely**

```
[student@servera ~]$ scp /etc/yum.conf /etc/hosts user@serverb:/home/remote
```

> 
The above program copies the `/etc/yum.conf` and `/etc/hosts` files on `servera` to the `remote's` home directory on the `serverb` remote system

**2. Transferring copy files to our system (local system)**

```
[student@servera ~]$ scp user@serverb:/etc/hostname /home/user
```
To transfer files to the system we use the above program which exemplifies 

> 
The file `/etc/hostname` on `serverb` is copyed to the local directory `/home/user`.

**3. Transfer the entire directory tree recursively**
If you want to do a full transfer of a directory Use `-r` in the program to run the process.

```
[student@servera ~]$ scp -r  student@serverb:/home/remote /home/directory
```
> the remote directory `/home/remote` on `serverb` is copied recursively to the local directory `/home/directory` on `servera`

##**4. Synchronizing files and folders**

in addition to transfers we can also synchronize files and folders. Synchronizing folders or copying files manually would be a huge waste of time. Rsync can do a lot of things, including adding some powerful features to save time. The Linux rsync command serves to move and synchronize files effectively between local devices, remote servers, or other similar devices.

here are the options of commonly used rsync,

- `-a`, `--archive`, activate archive mode.

- `-v`, `--verbose`, showing visual progress of a process.

- `-h`, results in a readable format.

- `-z`, `--compress`, compress data files during transfer.

**1. Synchronizing files**
If you want to sync files – copy files that might be in the destination folder to the original folder,

```
[student@servera ~]$ rsync ori/ duplicate/
```
> This will copy or synchronize files in the `ori` directory to the `duplicate` directory.

**2. Synchronizing the entire directory tree**

The -r (–recursive) option instructs rsync to copy anything including subdirectories and files from the original folder

```
[student@servera ~]$ rsync -r ori/ duplicate/
```

**3. Synchronizing files with archive mode**


If you want the copied files and folders to have the same information as spoken, you need to use archive mode. This mode is represented by the -a option.

- `-v`, serves to show the synchronization process

- `-a`, synchronize files and folders and save their information

```
[student@servera ~]$ rsync -av ori/ duplicate/
```

**4. Synchronizing files and folders between systems**

rsync also able to synchronize folders and files between systems
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.

```
[student@servera ~]$ rsync -av /home/ori student@serverb:/home/duplicate 
```
> In this example, synchronize the local `/home/ori` directory to the `/home/duplicate` directory on the `serverb` system

**5. Synchronizing duplicate folder**

If you want to sync two folders, but want to delete items in the duplicate folder that are not in the original folder, add –-delete

```
[student@servera ~]$ rsync -av --delete  original/ duplicate/
```