Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 26, 2022 02:59 pm GMT

Archiving And Transferring Files

Managing Compressed Tar Archives
tar command _File archiving and compression is useful when backing up and transferring data over a network. One of the oldest and most common commands for creatng and working with backup archives is the tar command.

The tar command can list the contents

of archives or extract their files to the current system.
Listing Options of the tar Command
The tar command expects one of the three following options:

Use the -c or --create option to create an archive.

Use the -t or --list option to list the contents of an archive.

Use the -x or --extract option to extract an archive.

Other commonly used options are:

Use the -f or --file= option with a file name as an argument of the archive to operate.

Use the -v or --verbose option for verbosity; useful to see which files get added to or extracted from the archive

Archiving Files and Directories

The following command creates an archive named archive.tar with the contents of file1, file2, and file3 in the user's home directory.

[user@host ~]$ tar -cf archive.tar file1 file2 file3
[user@host ~]$ ls archive.tararchive.tar

The above tar command can also be executed using the long version options.

[user@host ~]$ tar --file=archive.tar --create file1 file2 file3

To create the tar archive named, /root/etc.tar, with the /etc directory as content as user root:

[root@host ~]# tar -cf /root/etc.tar /etctar: Removing leading `/' from member names[root@host ~]#

Listing Contents of an Archive

The t option directs tar to list the contents (table of contents, hence t) of the archive. Use the f option with the name of the archive to be queried. For example:

[root@host ~]# tar -tf /root/etc.taretc/etc/fstabetc/crypttabetc/mtab...output omitted.

Extracting Files from an Archive

To restore files from the /root/etc.tar archive to the /root/etcbackup directory, run:

[root@host ~]# mkdir /root/etcbackup
[root@host ~]# cd /root/etcbackup
[root@host etcbackup]# tar -tf /root/etc.taretc/etc/fstabetc/crypttabetc/mtab...output omitted...
[root@host etcbackup]# tar -xf /root/etc.tar

In this example, an archive named, /root/myscripts.tar, is extracted in the /root/scripts directory while preserving the permissions of the extracted files:

[root@host ~]# mkdir /root/scripts
[root@host ~]# cd /root/scripts
[root@host scripts]# tar -xpf /root/myscripts.tar

Creating a Compressed Archive

Use one of the following options to create a compressed tar archive:

-z or --gzip for gzip compression (filename.tar.gz or filename.tgz)

-j or --bzip2 for bzip2 compression (filename.tar.bz2)

-J or -xz for xz compression (filename.tar.xz)

To create a gzip compressed archive named /root/etcbackup.tar.gz, with the contents from the /etc directory on host:

[root@host ~]# tar -czf /root/etcbackup.tar.gz /etctar: Removing leading `/' from member names

To create a bzip2 compressed archive named /root/logbackup.tar.bz2, with the contents from the /var/log directory on host:

[root@host ~]$ tar -cjf /root/logbackup.tar.bz2 /var/logtar: Removing leading `/' from member names

To create a xz compressed archive named, /root/sshconfig.tar.xz, with the contents from the /etc/ssh directory on host:

[root@host ~]$ tar -cJf /root/sshconfig.tar.xz /etc/sshtar: Removing leading `/' from member names

After creating an archive, verify the content of an archive using the tf options. It is not mandatory to use the option for compression agent when listing the content of a compressed archive file. For example, to list the content archived in the /root/etcbackup.tar.gz file, which uses the gzip compression, use the following command:

[root@host ~]# tar -tf /root/etcbackup.tar.gz /etcetc/etc/fstabetc/crypttabetc/mtab...output omitted...

Extracting a Compressed Archive

To extract the contents of a gzip compressed archive named /root/etcbackup.tar.gz in the /tmp/etcbackup directory:

[root@host ~]# mkdir /tmp/etcbackup
[root@host ~]# cd /tmp/etcbackup
[root@host etcbackup]# tar -tf /root/etcbackup.tar.gzetc/etc/fstabetc/crypttabetc/mtab...output omitted...
[root@host etcbackup]# tar -xzf /root/etcbackup.tar.gz

To extract the contents of a bzip2 compressed archive named /root/logbackup.tar.bz2 in the /tmp/logbackup directory:

[root@host ~]# mkdir /tmp/logbackup
[root@host ~]# cd /tmp/logbackup
[root@host logbackup]# tar -tf /root/logbackup.tar.bz2var/log/var/log/lastlogvar/log/READMEvar/log/private/var/log/wtmpvar/log/btmp...output omitted...
[root@host logbackup]# tar -xjf /root/logbackup.tar.bz2

To extract the contents of a xz compressed archive named /root/sshbackup.tar.xz in the /tmp/sshbackup directory:

[root@host ~]$ mkdir /tmp/sshbackup
[root@host ~]# cd /tmp/sshbackup
[root@host logbackup]# tar -tf /root/sshbackup.tar.xzetc/ssh/etc/ssh/modulietc/ssh/ssh_configetc/ssh/ssh_config.d/etc/ssh/ssh_config.d/05-redhat.confetc/ssh/sshd_config...output omitted...
[root@host sshbackup]# tar -xJf /root/sshbackup.tar.xz

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

Transferring Files Between Systems Securely

Transferring Files Using Secure Copy

The following example demonstrates how to copy the local/etc/yum.confand/etc/hostsfiles onhost, to theremoteuser's home directory on theremotehostremote system:

[user@host ~]$ scp /etc/yum.conf /etc/hosts remoteuser@remotehost:/home/remoteuser remoteuser@remotehost's password: password yum.conf 100% 813 0.8KB/s 00:00 hosts 100% 227 0.2KB/s 00:00

You can also copy a file in the other direction, from a remote system to the local file system. In this example, the file/etc/hostnameonremotehostis copyed to the local directory/home/user. Thescpcommand authenticates toremotehostas the userremoteuser.

[user@host ~]$ scp remoteuser@remotehost:/etc/hostname /home/user remoteuser@remotehost's password: password hostname 100% 22 0.0KB/s 00:00

To copy a whole directory tree recursively, use the-roption. In the following example, the remote directory/var/logonremotehostis copied recursively to the local directory/tmp/onhost. You must connect to the remote system asrootto make sure you can read all files in the remote/var/logdirectory.

[user@host ~]$ scp -r root@remoteuser:/var/log /tmp root@remotehost's password: password...output omitted...

Transferring Files Using the Secure File Transfer Program

Just like thescpcommand, thesftpcommand uses[user@]hostto identify the target system and user name. If you do not specify a user, the command will attempt to log in using your local user name as the remote user name. You will then be presented with ansftp>prompt.

[user@host ~]$ sftp remoteuser@remotehost remoteuser@remotehost's password: password Connected to remotehost. sftp> 

To upload the/etc/hostsfile on the local system to the newly created directory/home/remoteuser/hostbackuponremotehost. Thesftpsession always assumes that theputcommand is followed by a file on the local file system and starts in the connecting user's home directory; in this case,/home/remoteuser:

sftp> mkdir hostbackup sftp> cd hostbackup sftp> put /etc/hosts Uploading /etc/hosts to /home/remoteuser/hostbackup/hosts /etc/hosts 100% 227 0.2KB/s 00:00 sftp>

To download/etc/yum.conffrom the remote host to the current directory on the local system, execute the commandget /etc/yum.confand exit thesftpsession with theexitcommand.

sftp> get /etc/yum.conf Fetching /etc/yum.conf to yum.conf /etc/yum.conf 100% 813 0.8KB/s 00:00 sftp> exit [user@host ~]$


Original Link: https://dev.to/allinkfla/archiving-and-transferring-files-53mb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To