ARCHIVING FILES AND SCP
Published: 2022-09-24 18:13:29

## 
## ARCHIVING FILES

## 
## 
###1. `tar` Command
You can archiving files or directory use the `tar` command. The `tar` command can create an archive file from multiple files. Archiving and compressing files or directory are useful when creating beckups and transferring data  across a network.

---

## 
## 
### 2. Syntax and Options of the `tar` Command
**Syntax :**
```
$ tar [options] [archive-file] [file or directory to be archived]
```
**Options :**

1. `-c` or `--create` : Create a new archive.
2. `-x` or `--extract` : Extract from an existing archive.
3. `-t` or `--list` : List the table of contents of an archive.
4. `-v` or `--verbose` : Show which files get archived or extracted.
5. `-f` or `--file=` : File name.
6. `-p` or `--preserve-permissions` : Preserve the permissions of files and directories when extracting an archive, without subtracting the umask.
7. `-z` or `--gzip` : Use gzip compression (.tar.gz).
8. `-j` or `--bzip2` : Use bzip2 compression (.tar.bz2). bzip2 typically achieves a better compression ratio than gzip.
9. `-J` or `--xz` : Use xz compression (.tar.xz). The xz compression typically achieves a better compression ratio than bzip2.

---

## 
##
### 3. The `tar` Command Usage :
**Example** : 
- `User A` will create an archive file named `friend.tar` with the content files of `agung, iyan, and adit` in the `user A` home directory.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bhsxv39o5vv7784acej.png)

- The `tar` command can also create an archive file from a directory. Example :
`User A` will create an archive file named `tjkt2.tar` with the contents file from `/etc` directory, before archiving `/etc` directory, `User A` must switch to `root` user, because only the root user is allowed to read all of the files present in the /etc directory. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z1mlpb4qilpxqet648g.png)

- To list the contents of the `tar` file, you can use the `-t` option. Example :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq5dxrn0mqagmb6383ns.png)

- To extract archive file, you must create an empty directory so as not to overwrite other files. You can use `-x` option to extract archive file. Example :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/555g4fpb8i8jl6fwaio5.png)

> 
_**- NOTE :**
Use `-p` option to preserve the permissions of an archived file. Use command :_

> 
```
# tar -xpf [name archive-file]
```

---

## 
### 4. `gzip`, `bzip2`, and `xz`
There are three different compression methods supported by the tar command:

**1. `gzip`**
You can create a `gzip` compressed archive use `-z` option. Example :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7z7ucy252mepdz5fs12.png)

You can extract it using the command :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyqd4uzz0u2day6f6kqg.png)

**2. `bzip2`**
You can create a `bzip2` compressed archive use `-j` option. Example :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezcb3o9cqfetwva1zwad.png)

You can extract it using the command :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ab19oovpylkzyhxquxc4.png)

**3. `xz`**
You can create a `xz` compressed archive use `-J` option. Example :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qj29xo2smxnnklvo613.png)

You can extract it using the command :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scf40i2rfla58qlxijo0.png)

---

---

## 
## SCP

## 
## 
### Meaning of SCP
SCP (Secure Copy Protocol) is a network protocol used to securely copy files/folders between Linux (Unix) systems on a network. SCP protects your data while copying across an SSH (Secure Shell) connection by encrypting the files and the passwords. 

---

## 
## 
### Use of SCP
SCP can be used to :
1. Copying files from a local host to a remote host.
For Example :

```
$ scp home/adit/schedule.txt agung@10.28.140.44:/home/agung 
```

## 
> 
_**NOTE :**_
_- **home/adit/schedule.txt** : the name of the file being copied and its location._
_- **agung@10.28.140.44** : the username and IP address of the remote host._
_- **/home/agung** : the location where to store the copied file._
> 


---

2. Copying files from a remote host to a local host.
For Example :

```
scp iyan@80.11.205.22:/home/iyan/bicycle.txt home/adit
```

## 
> _**NOTE :**_
_- **iyan@80.11.205.22** :  the username and IP address of the remote host from where the file is currently located._
_- **/home/iyan/bicycle.txt** : the name of the file being copied and its location._
_- **home/adit** :  the location where to store the copied file._
> 

## 

> 

---

##                          **Thank You....**
>