Archiving & Transferrring Files

 


  1. The tar command is used to compress a group of files into an archive. The command is also used to extract, maintain, or modify tar archives. Tar archives combine multiple files and/or directories together into a single file. 
  2. In Linux TAR stand for "tape archiving" which is used to create maintain, modify and extract file.
  3. Archiving and compressing files are useful when creating backups and transferring data across a network.
  4. With tar, users can gather large sets of lines into a single file_(archive).
  5. The archive can be compressed using
  6. -gzip,bzip2 or xz compression.

Syntax of the "tar" command:
----------------------------

# tar [option] [archive-file] [file or directory to be archived]


Tar Operation
-cThis creat an archive file
-xThe operation extracts the archive file
-fSpecifies the filename of the archive file
-vThe prints verbose information for any tar operation on the terminal
-tThis lists all the files inside an archive file
-uThis archives a file and then adds it to an existing archive file
-rThis updates a file or directory located inside a .tar file
-zCreates a tar file using gzip compression
-jCreate an archive file using the bzip2 compression
-JThe xz compression typically archives a bettwer compression ratio than bzip2.(tzr.xz)
-wThis option verifies an archive file
-pPreserve the permission of the files and directories when extracting an archive.


Lab setup for practice:
-----------------------
Task-) Create a new directory & create some files in new directory thereafter create a sub-directory with files in new directory.

[root@localhost ~]# mkdir abhishek_dir-1

Note:- "abhishek_dir-1" is a directory name. 

[root@localhost ~]# ll    (Show_Details)
total 8
drwxr-xr-x. 2 root root    6 May 18 15:50 abhishek_dir-1
-rw-------. 1 root root 1499 May 19  2021 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 May 18 15:46 Desktop
drwxr-xr-x. 2 root root    6 May 18 15:46 Documents
drwxr-xr-x. 2 root root    6 May 18 15:46 Downloads
-rw-r--r--. 1 root root 1771 May 19  2021 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 May 18 15:46 Music
drwxr-xr-x. 2 root root    6 May 18 15:46 Pictures
drwxr-xr-x. 2 root root    6 May 18 15:46 Public
drwxr-xr-x. 2 root root    6 May 18 15:46 Templates
drwxr-xr-x. 2 root root    6 May 18 15:46 Videos


[root@localhost ~]# cd abhishek_dir-1/

Note:- This command is used to move into "abhishek_dir-1" dircetory.


[root@localhost abhishek_dir-1]# cat > Switch
This device work on data link layer of osi model.

[root@localhost abhishek_dir-1]# cat > Router
This device work on network layer of osi model.

[root@localhost abhishek_dir-1]# cat > Firewall
This device work on transport layer of osi model.

[root@localhost abhishek_dir-1]# ll
total 12
-rw-r--r--. 1 root root 50 May 18 15:59 Firewall
-rw-r--r--. 1 root root 48 May 18 15:58 Router
-rw-r--r--. 1 root root 50 May 18 15:58 Switch

[root@localhost abhishek_dir-1]# mkdir abhishek_Sub-Dir-1
[root@localhost abhishek_dir-1]# ll
total 12
drwxr-xr-x. 2 root root  6 May 18 16:11 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root 50 May 18 15:59 Firewall
-rw-r--r--. 1 root root 48 May 18 15:58 Router
-rw-r--r--. 1 root root 50 May 18 15:58 Switch


[root@localhost abhishek_dir-1]# cd abhishek_Sub-Dir-1/
[root@localhost abhishek_Sub-Dir-1]# cat > active_topology
This type of topology need external power.

[root@localhost abhishek_Sub-Dir-1]# cat > passive_topology
This topology don't need external power.

[root@localhost abhishek_Sub-Dir-1]# ll
total 8
-rw-r--r--. 1 root root 43 May 18 16:12 active_topology
-rw-r--r--. 1 root root 41 May 18 16:12 passive_topology


Task-) create an archive file of "Redhat.tar"  via "tar" in "abhishek_dir-1" directory with files then use "-cvf":
--------------------------------------

[root@localhost abhishek_dir-1]# tar -cvf Redhat.tar Firewall Router 
Firewall
Router

[root@localhost abhishek_dir-1]# ll    (Show_Details)
total 24
drwxr-xr-x. 2 root root    53 May 18 16:12 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 15:59 Firewall
-rw-r--r--. 1 root root 10240 May 18 16:21 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 15:58 Router
-rw-r--r--. 1 root root    50 May 18 15:58 Switch


Task-) For "gzip" create an archive file of "Redhat-1.tar.gz"  via "tar.gz" in "abhishek_dir-1" directory with files then use "-z" between "-cvf":
-----------------------------------------

[root@localhost abhishek_dir-1]# tar -czvf Redhat-1.tar.gz Firewall Router
Firewall
Router
[root@localhost abhishek_dir-1]# ll        (Show_Details)
total 28
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root 10240 May 18 17:22 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch

Task-) For "bzip2" create an archive file of "Redhat-4.tar.bz2"  via "tar.bz2" in "abhishek_dir-1" directory with files then use "-j" between "-cvf":
-----------------------------------------

[root@localhost abhishek_dir-1]# tar -cjvf Redhat-4.tar.bz2 Firewall Router
Firewall
Router
[root@localhost abhishek_dir-1]# ll         (Show_Details)
total 36
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:27 Redhat-4.tar.bz2
-rw-r--r--. 1 root root 10240 May 18 17:22 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch


Task-) For "xz" create an archive file of "Redhat-5.tar.xz"  via "tar.xz" in "abhishek_dir-1" directory with files then use "-J" between "-cvf":
-----------------------------------------
[root@localhost abhishek_dir-1]# tar -cJvf Redhat-5.tar.xz Firewall Router
Firewall
Router
[root@localhost abhishek_dir-1]# ll
total 40
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:27 Redhat-4.tar.bz2
-rw-r--r--. 1 root root   248 May 18 17:30 Redhat-5.tar.xz
-rw-r--r--. 1 root root 10240 May 18 17:22 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch


Task-) Show the contents of an archive file:-
----------------------------------------------

[root@localhost abhishek_dir-1]# tar -tf Redhat
Redhat-1.tar.gz  Redhat-4.tar.bz2  Redhat-5.tar.xz   Redhat.tar        
[root@localhost abhishek_dir-1]# tar -tf Redhat-1.tar.gz 
Firewall
Router

[root@localhost abhishek_dir-1]# tar -tf Redhat-4.tar.bz2 
Firewall
Router

[root@localhost abhishek_dir-1]# tar -tf Redhat-5.tar.xz 
Firewall
Router

[root@localhost abhishek_dir-1]# tar -tf Redhat.tar 
Firewall
Router

Task-) Adding new file in existing file via "-rvf":
----------------------------------------
[root@localhost abhishek_dir-1]# tar -tf Redhat.tar 
Firewall
Router

[root@localhost abhishek_dir-1]# tar -rvf Redhat.tar Switch 
Switch

[root@localhost abhishek_dir-1]# tar -tvf Redhat.tar 
-rw-r--r-- root/root        50 2023-05-18 17:21 Firewall
-rw-r--r-- root/root        48 2023-05-18 17:21 Router
-rw-r--r-- root/root        50 2023-05-18 17:20 Switch

Task-) Remove or delete files from an archive via "-f":
----------------------------------------
[root@localhost abhishek_dir-1]# tar -tvf Redhat.tar 
-rw-r--r-- root/root        50 2023-05-18 17:21 Firewall
-rw-r--r-- root/root        48 2023-05-18 17:21 Router
-rw-r--r-- root/root        50 2023-05-18 17:20 Switch

[root@localhost abhishek_dir-1]# tar --delete Switch -f Redhat.tar

[root@localhost abhishek_dir-1]# tar -tvf Redhat.tar 
-rw-r--r-- root/root        50 2023-05-18 17:21 Firewall
-rw-r--r-- root/root        48 2023-05-18 17:21 Router

Task-) Add all files and sub-directory(abhishek_Sub-Dir-1) of the directory(abhishek_dir-1) in the Open_Source.tar.
----------------------------------------

[root@localhost abhishek_dir-1]# tar -cvf Open_Source.tar abhishek_Sub-Dir-1/
abhishek_Sub-Dir-1/
abhishek_Sub-Dir-1/active_topology
abhishek_Sub-Dir-1/passive_topology

[root@localhost abhishek_dir-1]# ll     (Show_Details)
total 52
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root 10240 May 18 18:18 Open_Source.tar
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:26 Redhat-2.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:27 Redhat-4.tar.bz2
-rw-r--r--. 1 root root   248 May 18 17:30 Redhat-5.tar.xz
-rw-r--r--. 1 root root 10240 May 18 18:10 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch

[root@localhost abhishek_dir-1]# tar -tf Open_Source.tar 
abhishek_Sub-Dir-1/
abhishek_Sub-Dir-1/active_topology
abhishek_Sub-Dir-1/passive_topology

Task-) Unless any change is made in the files or directories which are archived. The update command does nothing.
------------------------------------------

[root@localhost abhishek_dir-1]# tar -uvf Open_Source.tar abhishek_Sub-Dir-1/

[root@localhost abhishek_dir-1]# cat >> abhishek_Sub-Dir-1/active_topology 
Star topology is an Active Topology.               

[root@localhost abhishek_dir-1]# tar -uvf Open_Source.tar abhishek_Sub-Dir-1/
abhishek_Sub-Dir-1/active_topology

[root@localhost abhishek_dir-1]# tar -tf Open_Source.tar 
abhishek_Sub-Dir-1/
abhishek_Sub-Dir-1/active_topology
abhishek_Sub-Dir-1/passive_topology
abhishek_Sub-Dir-1/active_topology    (Updated Files )

Note:- Newly added, modified and updated files are placed in the end of the archie.

Task-) Delete "archive" files single & multiple:
------------------------------------------------

[root@localhost abhishek_dir-1]# ll
total 64
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root 10240 May 18 19:02 Open_Source.tar
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:26 Redhat-2.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:27 Redhat-4.tar.bz2
-rw-r--r--. 1 root root   248 May 18 17:30 Redhat-5.tar.xz
-rw-r--r--. 1 root root 10240 May 18 18:10 Redhat.tar
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch

[root@localhost abhishek_dir-1]# rm -rf Redhat.tar 

[root@localhost abhishek_dir-1]# ll
total 52
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root 10240 May 18 19:02 Open_Source.tar
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:26 Redhat-2.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:27 Redhat-4.tar.bz2
-rw-r--r--. 1 root root   248 May 18 17:30 Redhat-5.tar.xz
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch

[root@localhost abhishek_dir-1]# rm -rf Redhat-5.tar.xz Redhat-4.tar.bz2 

[root@localhost abhishek_dir-1]# ll
total 44
drwxr-xr-x. 2 root root    53 May 18 17:22 abhishek_Sub-Dir-1
-rw-r--r--. 1 root root    50 May 18 17:21 Firewall
-rw-r--r--. 1 root root 10240 May 18 19:02 Open_Source.tar
-rw-r--r--. 1 root root   195 May 18 17:24 Redhat-1.tar.gz
-rw-r--r--. 1 root root   223 May 18 17:26 Redhat-2.tar.gz
-rw-r--r--. 1 root root    48 May 18 17:21 Router
-rw-r--r--. 1 root root    50 May 18 17:20 Switch


Compressing and Decompressing archive:-
---------------------------------------
  1. By default the "tar" command doesn't perform compression on archived files. But if require it can use the third party compression utility such as "gzip" and "bzip2" to compress and decompress the archived file.
  2. The gzip compress faster but low compression ratio. To create and compress the archive file with gzip utility use the option "g" with option "cvf" as "cgvf" and use the file extension ".gz" with the archive file name.
  3. The bzip2 compress slow but high compression ratio. To create and compress the archive file with bzip2 utility use the option "j" with option "cvf" as "cjvf" and use the file extension ".bz2" with the archive file name.

Following commands create the archive "abhishek_Sub-Dir-1.tar" from the "/home" directory without compression. with gzip compression and with bzip2 compression.


[root@localhost abhishek_dir-1]# tar -cvf abhishek_Sub-Dir-1.tar /home/

[root@localhost abhishek_dir-1]# tar -czvf abhishek_Sub-Dir-1.gz /home/

[root@localhost abhishek_dir-1]# tar -cjvf abhishek_Sub-Dir-1.bz2 /home/

To view the size of compressed archive file, once compression is done use "du" command with the option "-h".

[root@localhost abhishek_dir-1]# du -h abhishek_Sub-Dir-1.tar 
11M abhishek_Sub-Dir-1.tar    (No Compress)

[root@localhost abhishek_dir-1]# du -h abhishek_Sub-Dir-1.gz
1.4M abhishek_Sub-Dir-1.gz    (gzip compression)

[root@localhost abhishek_dir-1]# du -h abhishek_Sub-Dir-1.bz2
1.1M abhishek_Sub-Dir-1.bz2    (bzip2 compression)


Note:- "Once an archive file is compresses, advance operations such as append update and individual delete can not performed with it."


Task-) To extract a compressed archive, use the same option
[root@localhost abhishek_dir-1]# tar -xvf abhishek_Sub-Dir-1.tar 

[root@localhost abhishek_dir-1]# tar -xzvf abhishek_Sub-Dir-1.gz 

[root@localhost abhishek_dir-1]# tar -xjvf abhishek_Sub-Dir-1.bz2 

=================================================================

Secure Copying Files Between Host:-
-----------------------------------
  1. The scp command is secure way to transfer files between servers. It encrypts both the end password during transmission, ensuring secure file transfer.
  2. scp stands for Secure Copy Protocol.
  3. It's used to securely transfer files between a local and a remote host.
  4. scp use SSH (Secure Shell) protocol to encrypt the files being transferred.
  5. Usually scp use port number 22.
  6. By default SCP using "AES-128" to encrypt files.

  7. scp or secure copy encrypts files and transfer them from one host to another on a network. It uses SSH to establish a secure connection.
Basic Usage of scp command:-
----------------------------
  1. Use the syntax scp [option] [destination] to initiate a file transfer.
  2. # scp src_file_name username@dest.hostname:dest_path
  3. Specify the location of the source file and the destination where the file should be transferred.
  4. Use the -P option followed by the port number to specify a non-default port for the transfer.

Secure Copy File Option
-Pspecify the port number of the remote host.
-rcopy directories recursively.
-vverbose mode to display the progress of the transfer.
-pPreserves files modification and access times.
-CThis option forces scp to compresses the data as it is sent to the destination machine.







Thanks & Regards
Abhishek Pathak
+91-9621134014
abhishek9621134014@outlook.com



















No comments:

Post a Comment

Computer Hardware_(A+) & Operating System with Networking

Microprocessor Motherboard About Operating System Create Bootable Pen Drive via Command Map Network Drive File Transfer Protocol in Windows ...