DEV Community

Adit-Rk20
Adit-Rk20

Posted on

ARCHIVING FILES AND SCP

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]
Enter fullscreen mode Exit fullscreen mode

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

  • 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

  • To list the contents of the tar file, you can use the -t option. Example :
    Image description

  • 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

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

# tar -xpf [name archive-file]
Enter fullscreen mode Exit fullscreen mode

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

You can extract it using the command :
Image description

2. bzip2
You can create a bzip2 compressed archive use -j option. Example :
Image description

You can extract it using the command :
Image description

3. xz
You can create a xz compressed archive use -J option. Example :
Image description

You can extract it using the command :
Image description



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 
Enter fullscreen mode Exit fullscreen mode

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.


  1. Copying files from a remote host to a local host. For Example :
scp iyan@80.11.205.22:/home/iyan/bicycle.txt home/adit
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)