Ch12. Archive and Transfer Files


Ch12. Archive and Transfer Files

 

Create archive files and directories with tar (tap archive):

[root@master ~]# tar cvf archive.tar file1 file2 file3
[root@master ~]# tar -cvf etc.tar /etc/                   (requires root privilege)
[root@master ~]# tar -cf /root/etc.tar /etc/
[root@master ~]# du -hs /etc/ 
[root@master ~]# ll -h etc.tar
  • c (create an archive)
  • f file name (file name of the archive to operate on)
  • v (verbosity; useful to see which files get added to or extracted from the a rch ive)

Notes:

  • Before creating a tar archive, verify that there is no other archive in the directory with the same name as the new archive to be created. The tar command will overwrite an existing archive without any feedback.
  • For tar to be able to archive the selected files, it is mandatory that the user executing the tar command is able to read the files.
  • While tar stores ownership and permissions of the files, there are other attributes that are not stored in the tar archive by default, such as the SELinux context and ACLs. To store those extended attributes in the tar archive, the --xattrs option is required when creating an archive.
 

List contents of a tar archive:

[root@master ~]# tar tf archive.tar 
[root@master ~]# tar tvf archive.tar
• t ( list the contents of an archive)

Extract an archive created with tar:

[root@master ~]# tar xvf archive.tar 
[root@master ~]# tar xvf archive.tar -C /var/data/       (change the extract location)
• x (extract an archive)

Create a compressed tar archive:

[root@master ~]# tar cvzf etc.tar.gz /etc/
Or
[root@master ~]# tar cvzf etc.tgz /etc/

• z for gzip compression (filename.tar.gz or filename.tgz)
• j for bzip2 compression (filename.tar.bz2)
• J for xz compression (filename.tar.xz)

Extract a compressed tar archive:

[root@master ~]# tar xvzf etc.tar.gz 
[root@master ~]# tar xvjf etc.tar.bz2
[root@master ~]# tar xvJf etc.tar.xz

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

Compress and extract files:

[root@master ~]# gzip file.text 
[root@master ~]# gunzip file.text.gz 

[root@master ~]# bzip2 file.text 
[root@master ~]# bunzip2 file.text.bz2 

[root@master ~]# xz file.text 
[root@master ~]# unxz file.text.xz 

Note:
- Compressing a file will delete it by default.
[root@master ~]# gzip -c file.text > file.gz          (will not delete the file)

Copying Files Between Systems Securely:

[abeer@client ~]$ scp test_file 192.168.1.1:~
[abeer@client ~]$ scp -P 2200 test_file 192.168.1.1:~
[abeer@client ~]$ scp 192.168.1.1:/home/test_file /home/abeer
[abeer@client ~]$ scp root@192.168.1.1:/etc/hosts ~
[abeer@client ~]$ scp -P 2200 root@192.168.1.1:/etc/hosts ~
[abeer@client ~]$ scp -r root@192.168.1.1:/var/log /tmp        (copy recursively)

Transfer files remotely with sftp:

[root@client ~]# sftp 192.168.1.1
sftp> ls
sftp> mkdir client_dir
sftp> get file2
sftp> get -r /var/log                     (download recursively)
sftp> put test_file
sftp> exit

Synchronizing Files Between Systems Securely:

[abeer@client ~]$ rsync test_file 192.168.1.1:~
[abeer@client ~]$ rsync 192.168.1.1:/home/test_file /home/abeer
[abeer@client ~]$ rsync root@192.168.1.1:/etc/hosts ~
[root@client ~]# rsync -rvz file2 root@192.168.1.1:/root/
	-r, --recursive
-v, --verbose
-z, --compress    (during transfer)


Previous Post Next Post
Adv
AdSense advertisment Blog end
Adv

Contact Form