2022-11-16

Timeshift Backup Restore Ubuntu/Linux Mint Systems from USB/SSD Disk

11/16/2022 10:42:00 PM Posted by Hoàng Dũng , No comments

 Timeshift is a new app on Linux Mint 19 Tara. Timeshift is used to save a snapshot of the different states of your Linux Mint 19 operating system. When you try out new things on your computer, do upgrades and such stuff, your operating system may break. With the help of Timeshift, gladly, you can go back in time and restore your computer to a point at which it worked perfectly.

In this article, I will show you how to backup and restore Linux Mint 19 systems using USB Storage device with Timeshift. Let’s get started.

BTRFS for Timeshift on Linux Mint 19:

Timeshift works best on BTRFS filesystem. BTRFS has good support for Copy on Write (CoW) snapshots. BTRFS creates snapshots very fast and without any need additional disk space. That’s why I recommend you install Linux Mint 19 on BTRFS filesystem if you want to use Timeshift without hassle.

Timeshift on other Filesystems:

Let’s say, you had no idea about Timeshift or you didn’t know that Timeshift works best on BTRFS filesystem while installing Linux Mint 19. So you ended up installing Linux Mint 19 on filesystems such as EXT4. Now you know about Timeshift and you regret doing so.

Don’t worry about it if that’s the case. Timeshift also supports RSYNC. RSYNC works just as well.

The advantages of creating backups or snapshots with Timeshift using RSYNC is that it doesn’t depend on specific filesystem. RSYNC backups or snapshots can be stored on an external USB Hard Drive as well as a USB Thumb Drive. So when anything goes wrong, you just have to insert your USB storage device and restore your operating system from there. This is all this article is about.

Taking Snapshots and Storing it on USB Storage Device using Timeshift:

For taking Timeshift snapshots or backups, I recommend you use USB 3.0 32 GB USB thumb drive. Use the USB thumb drive for only taking Timeshift backups. That way you won’t accidentally remove any system file from the USB thumb drive. This is very important.

By the way, you can also use USB 3.0 external hard drive or SSD if you want. The procedures are the same.

Before I set up Timeshift, I am going to show you how to get your USB thumb drive ready for Timeshift.

First insert your USB Storage device and Linux Mint should detect it.

Now open the Disks app from the Mint Menu.

Then select your USB Storage device from the Disks app and click on the stop button as marked in the screenshot below.

The USB Storage device should be unmounted. Now click on the Settings icon and then click on Format Partition… as marked in the screenshot below. You can also press Shift + <Ctrl> + f

You should be the Format Volume window. Type in Timeshift on the Volume Name field, and set type to Ext4 as marked in the screenshot below. Once you’re done click on Next.

Note: I used Ext4 filesystem format because I don’t want it to be detected by Windows operating systems and corrupt my files in case it has viruses. Just playing on the safe side.

Now click on Format.

The USB thumb drive should be formatted.

Now search for timeshift on the Mint Menu and click on Timeshift as marked in the screenshot below.

Type in your login user’s password and click on Authenticate.

If you’re running Timeshift for the first time, you should see the following Window. You can configure Timeshift from here, but I am going to show you how to do that from the Timeshift app, as that way you can learn how to change configuration when you want to. So just click on Finish.

This is the main window of Timeshift.

To configure Timeshift, click on the Settings icon.

On the Type tab, select RSYNC. Now click on the Location tab.

From your Location tab, click on your USB thumb drive.

From the Schedule tab, you can configure how often a snapshot is to be taken. You can uncheck all of the Snapshot Levels to disable auto snapshots. That way you can take snapshots only when you want.

From the Filters tab, you can add directories. You can then include or exclude those directories from the Users tab. The excluded directories won’t be backed up when a snapshot is taken. So lost files can’t be restored. By default, the user’s HOME directories are excluded from the snapshot.

Once you’re done, click on Close.

As you can see, your USB thumb drive is now selected.

Now to create a snapshot of your Linux Mint 19 at this point, click on Create.

Timeshift should start creating snapshot. It takes quite a bit of time if it is the first Timeshift snapshot.

A snapshot should be taken.

You can even add comments to your snapshot, so that you will be able to remember what it is later. Just click on the comments column of your snapshot and start typing. Once you’re done, press <Enter>.

The comment should be saved.

You can create as many snapshots as you want if you have enough space on your USB storage device.

Restoring from Snapshot using Timeshift:

Now I am going to make some changes and then restore to this snapshot I created just now. I installed Apache 2 web server, with sudo apt install apache2 -y

As you can see, everything works. Now let’s just assume, it’s not working as you expected. Now you want to go back to the state before installing this web server.

Just insert your USB storage device and open Timeshift app. Now select the snapshot you want to go back to and click on Restore.

Click on Next.

The restore process should start.

You should be asked to confirm it. Just click on Next.

Click on Next.

Timeshift should restore the files and reboot your computer.

Once your computer boots, you should not find any trace of Apache 2 web server as you can see from the screenshot below.

That’s how you use Timeshift to take backups and restore Linux Mint 19 systems from USB drive. Thanks for reading this article.

@source: https://linuxhint.com/timeshift_linux_mint_usb/

2022-11-11

Check if reboot is required on CentOS | RHEL Server after upgrade

11/11/2022 11:55:00 AM Posted by Hoàng Dũng No comments

Hello good people!. In today’s article we will show you how to check if your CentOS / RHEL server requires a reboot after an upgrade. When you perform an upgrade in a RHEL/CentOS system, a reboot is often required if kernel related packages are updated before loading a newly-installed kernel.
In Red Hat operating system and its derivatives there is a command line program called needs-restarting which reports a list of process ids that should be restarted after an upgrade. The needs-restarting will look through running processes and tries to detect those that use files from packages that have been updated after the given process started.
This package may not be installed by default and it is part of yum-utils:

$ sudo yum  provides needs-restarting
Last metadata expiration check: 0:00:06 ago on Tue 30 Mar 2021 08:56:53 AM UTC.
yum-utils-4.0.17-5.el8.noarch : Yum-utils CLI compatibility layer
Repo        : @System
Matched from:
Filename    : /usr/bin/needs-restarting

yum-utils-4.0.17-5.el8.noarch : Yum-utils CLI compatibility layer
Repo        : baseos
Matched from:
Filename    : /usr/bin/needs-restarting

You can check if the binary is locally available in your system:

$ which needs-restarting
/usr/bin/needs-restarting

If not present install it through the yum-utils package:

sudo yum -y install yum-utils

Check if RHEL / CentOS Server needs restart after upgrade

Common packages that will require reboot.

Red Hat Enterprise Linux 7:

  • kernel
  • glibc[^2]
  • linux-firmware
  • systemd
  • udev
  • dbus

Red Hat Enterprise Linux 8:

  • kernel
  • glibc[^2]
  • linux-firmware
  • systemd
  • dbus

The needs-restarting tool has below options:

-u, --useronly: Only consider processes belonging to the running user
-r, --reboothint: Only report whether a reboot is required (exit code 1) or not (exit code 0)

If I run the program to report whether a reboot is required before upgrading the system it should return 0.

$ sudo needs-restarting -r
No core libraries or services have been updated since boot-up.
Reboot should not be necessary.

$ echo $?
0

The command echo $? returns the exit status.

Let’s perform an upgrade on the system:

sudo yum -y upgrade

It is always recommended that systems be rebooted after installation of an updated package. This ensures that all processes and services benefit from updates to core libraries and services.

Let’s confirm if a reboot is required after a successful upgrade, by rerunning needs-restarting command;

$ sudo needs-restarting -r
Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/2794

From the output we can see Core libraries or services have been updated since boot-up:

$ echo $?
1

Exit status of 1 means reboot is required.

Below commands will reboot the system only if exit status for needs-restarting -r is 1:

Check and reboot using bash script

Create a new bash script file:

vi check_rhel_system_restart.sh

Add below contents:

#!/bin/bash
echo "Checking if reboot is required.."
echo ""
sudo needs-restarting -r
RESULT=$?
echo ""
if [ $RESULT -eq 1 ]; then
  read -p "Do you wish to reboot the system now? (y/n): " yn
  case $yn in
       [Yy]* ) echo "Rebooting $HOSTNAME in 5 seconds to install updates" && sleep 5 && sudo reboot;;
       [Nn]* ) echo "Bye let's reboot later.."; exit;;
       * ) echo "Please answer yes or no.";;
  esac
else
  echo "No reboot required"
fi

Make the script executable:

chmod +x check_rhel_system_restart.sh

Run the script:

$ ./check_rhel_system_restart.sh

If you Answer n/N

$ ./check_rhel_system_restart.sh
Checking if reboot is required..

Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943

Do you wish to reboot the system now? (y/n): n

If you answer y/Y

$ ./check_rhel_system_restart.sh
Checking if reboot is required..

Core libraries or services have been updated since boot-up:
  * kernel
  * linux-firmware
  * systemd

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943

Do you wish to reboot the system now? (y/n): y
Rebooting centos in 5 seconds to install updates
Connection to 192.168.20.11 closed by remote host.
Connection to 192.168.20.11 closed.

You can then run the script every time you perform system upgrade.

2022-10-06

21 Tar Command Examples in Linux

10/06/2022 10:41:00 AM Posted by Hoàng Dũng No comments

 The Linux “tar” stands for tape archive, which is used by a large number of Linux/Unix system administrators to deal with tape drive backup in Linux.

The tar command is used to rip a collection of files and directories into a highly compressed archive file commonly called tarball or targzip and bzip in Linux.

The tar is the most widely used command to create compressed archive files that can be moved easily from one disk to another disk or machine to machine.


In this article, we will be going to review and discuss various tar command examples including how to create archive files using (tartar.gz, and tar.bz2) compression, how to extract archive files, extract a single file, view the content of the file, verify a file, add files or directories to the existing archive file, estimate the size of tar archive file, etc.

The main purpose of this guide is to provide various tar command examples that might be helpful for you to understand and become an expert in tar archive manipulation.

1. Create a tar Archive File in Linux

The below example of the tar command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in the current working directory.

See the example of the tar command in action.

# tar -cvf tecmint-14-09-12.tar /home/tecmint/

/home/tecmint/
/home/tecmint/cleanfiles.sh
/home/tecmint/openvpn-2.1.4.tar.gz
/home/tecmint/tecmint-14-09-12.tar
/home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

Let’s discuss each option used in the above tar command to create a tar archive file.

  • c – Creates a new .tar archive file.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.

2. Create tar.gz Archive File in Linux

To create a compressed gzip archive file we use the option z. For example, the below command will create a compressed MyImages-14-09-12.tar.gz file for the directory /home/MyImages. (Note: tar.gz and tgz both are similar).

# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
OR
# tar cvzf MyImages-14-09-12.tgz /home/MyImages

/home/MyImages/
/home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/home/MyImages/RobertKristenviolent101201.jpg
/home/MyImages/Justintimerlake101125.jpg
/home/MyImages/Mileyphoto101203.jpg
/home/MyImages/JenniferRobert101130.jpg
/home/MyImages/katrinabarbiedoll231110.jpg
/home/MyImages/the-japanese-wife-press-conference.jpg
/home/MyImages/ReesewitherspoonCIA101202.jpg
/home/MyImages/yanaguptabaresf231110.jpg

3. Create tar.bz2 Archive File in Linux

The bz2 feature compresses and creates an archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files than gzip, which takes less time.

To create a highly compressed tar file we use the option j. The following example command will create a Phpfiles-org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar to tb2).

# tar cvfj Phpfiles-org.tar.bz2 /home/php
OR
# tar cvfj Phpfiles-org.tar.tbz /home/php
OR 
# tar cvfj Phpfiles-org.tar.tb2 /home/php

/home/php/
/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/object.html
/home/php/video.php

4. Untar tar Archive File in Linux

To untar or extract a tar file, just issue the following command using option x (extract). For example, the below command will untar the file public_html-14-09-12.tar in the present working directory.

If you want to untar in a different directory then use option as -C (specified directory).

## Untar files in Current Directory ##
# tar -xvf public_html-14-09-12.tar

## Untar files in specified Directory ##
# tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/

/home/public_html/videos/
/home/public_html/videos/views.php
/home/public_html/videos/index.php
/home/public_html/videos/logout.php
/home/public_html/videos/all_categories.php
/home/public_html/videos/feeds.xml

5. Uncompress tar.gz Archive File in Linux

To uncompress the tar.gz archive file, just run the following command. If we would like to untar in different directories, just use option -C and the directory path, as shown in the above example.

# tar -xvf thumbnails-14-09-12.tar.gz

/home/public_html/videos/thumbnails/
/home/public_html/videos/thumbnails/katdeepika231110.jpg
/home/public_html/videos/thumbnails/katrinabarbiedoll231110.jpg
/home/public_html/videos/thumbnails/onceuponatime101125.jpg
/home/public_html/videos/thumbnails/playbutton.png
/home/public_html/videos/thumbnails/ReesewitherspoonCIA101202.jpg
/home/public_html/videos/thumbnails/snagItNarration.jpg
/home/public_html/videos/thumbnails/Minissha-Lamba.jpg
/home/public_html/videos/thumbnails/Lindsaydance101201.jpg
/home/public_html/videos/thumbnails/Mileyphoto101203.jpg

6. Uncompress tar.bz2 Archive File in Linux

To uncompress the highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.

# tar -xvf videos-14-09-12.tar.bz2

/home/public_html/videos/flv/katrinabarbiedoll231110.flv
/home/public_html/videos/flv/BrookmuellerCIA101125.flv
/home/public_html/videos/flv/dollybackinbb4101125.flv
/home/public_html/videos/flv/JenniferRobert101130.flv
/home/public_html/videos/flv/JustinAwardmovie101125.flv
/home/public_html/videos/flv/Lakme-Fashion-Week.flv
/home/public_html/videos/flv/Mileyphoto101203.flv
/home/public_html/videos/flv/Minissha-Lamba.flv

7. List Content of tar Archive File in Linux

To list the contents of the tar archive file, just run the following command with option t (list content). The below command will list the content of the uploadprogress.tar file.

# tar -tvf uploadprogress.tar

-rw-r--r-- chregu/staff   2276 2011-08-15 18:51:10 package2.xml
-rw-r--r-- chregu/staff   7877 2011-08-15 18:51:10 uploadprogress/examples/index.php
-rw-r--r-- chregu/staff   1685 2011-08-15 18:51:10 uploadprogress/examples/server.php
-rw-r--r-- chregu/staff   1697 2011-08-15 18:51:10 uploadprogress/examples/info.php
-rw-r--r-- chregu/staff    367 2011-08-15 18:51:10 uploadprogress/config.m4
-rw-r--r-- chregu/staff    303 2011-08-15 18:51:10 uploadprogress/config.w32
-rw-r--r-- chregu/staff   3563 2011-08-15 18:51:10 uploadprogress/php_uploadprogress.h
-rw-r--r-- chregu/staff  15433 2011-08-15 18:51:10 uploadprogress/uploadprogress.c
-rw-r--r-- chregu/staff   1433 2011-08-15 18:51:10 package.xml

8. List Content tar.gz Archive File in Linux

Use the following command to list the content of the tar.gz file.

# tar -tvf staging.tecmint.com.tar.gz

-rw-r--r-- root/root         0 2012-08-30 04:03:57 staging.tecmint.com-access_log
-rw-r--r-- root/root       587 2012-08-29 18:35:12 staging.tecmint.com-access_log.1
-rw-r--r-- root/root       156 2012-01-21 07:17:56 staging.tecmint.com-access_log.2
-rw-r--r-- root/root       156 2011-12-21 11:30:56 staging.tecmint.com-access_log.3
-rw-r--r-- root/root       156 2011-11-20 17:28:24 staging.tecmint.com-access_log.4
-rw-r--r-- root/root         0 2012-08-30 04:03:57 staging.tecmint.com-error_log
-rw-r--r-- root/root      3981 2012-08-29 18:35:12 staging.tecmint.com-error_log.1
-rw-r--r-- root/root       211 2012-01-21 07:17:56 staging.tecmint.com-error_log.2
-rw-r--r-- root/root       211 2011-12-21 11:30:56 staging.tecmint.com-error_log.3
-rw-r--r-- root/root       211 2011-11-20 17:28:24 staging.tecmint.com-error_log.4

9. List Content tar.bz2 Archive File in Linux

To list the content of the tar.bz2 file, issue the following command.

# tar -tvf Phpfiles-org.tar.bz2

drwxr-xr-x root/root         0 2012-09-15 03:06:08 /home/php/
-rw-r--r-- root/root      1751 2012-09-15 03:06:08 /home/php/iframe_ew.php
-rw-r--r-- root/root     11220 2012-09-15 03:06:08 /home/php/videos_all.php
-rw-r--r-- root/root      2152 2012-09-15 03:06:08 /home/php/rss.php
-rw-r--r-- root/root      3021 2012-09-15 03:06:08 /home/php/index.php
-rw-r--r-- root/root      2554 2012-09-15 03:06:08 /home/php/vendor.php
-rw-r--r-- root/root       406 2012-09-15 03:06:08 /home/php/video_title.php
-rw-r--r-- root/root      4116 2012-09-15 03:06:08 /home/php/report.php
-rw-r--r-- root/root      1273 2012-09-15 03:06:08 /home/php/object.html

10. Untar Single file from tar File in Linux

To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.

# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

cleanfiles.sh

11. Untar Single file from tar.gz File in Linux

To extract a single file tecmintbackup.xml from the tecmintbackup.tar.gz archive file, use the command as follows.

# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml
OR
# tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml

tecmintbackup.xml

12. Untar Single file from tar.bz2 File in Linux

To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.

# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php

/home/php/index.php

13. Untar Multiple files from tar, tar.gz, and tar.bz2 File

To extract or untar multiple files from the tartar.gz, and tar.bz2 archive files. For example, the below command will extract “file 1” and “file 2” from the archive files.

# tar -xvf tecmint-14-09-12.tar "file1" "file2" 

# tar -zxvf MyImages-14-09-12.tar.gz "file1" "file2" 

# tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"

14. Extract Group of Files using Wildcard in Linux

To extract a group of files we use wildcard-based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz, and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php'

# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'

# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/video.php

15. Add Files or Directories to tar Archive File in Linux

To add files or directories to the existing tar archive files we use the option r (append). For example, we add file xyz.txt and directory php to the existing tecmint-14-09-12.tar archive file.

# tar -rvf tecmint-14-09-12.tar xyz.txt

# tar -rvf tecmint-14-09-12.tar php

drwxr-xr-x root/root         0 2012-09-15 02:24:21 home/tecmint/
-rw-r--r-- root/root  15740615 2012-09-15 02:23:42 home/tecmint/cleanfiles.sh
-rw-r--r-- root/root    863726 2012-09-15 02:23:41 home/tecmint/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root  21063680 2012-09-15 02:24:21 home/tecmint/tecmint-14-09-12.tar
-rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/ 
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php 
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php 
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php 
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php 
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php 
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php

16. Add Files or Directories to tar.gz and tar.bz2 Files

The tar command doesn’t have an option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file. If we do try will get the following error.

# tar -rvf MyImages-14-09-12.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

tar: This does not look like a tar archive
tar: Skipping to next header
xyz.txt
tar: Error exit delayed from previous errors

17. How To Verify tar, tar.gz, and tar.bz2 Archive File

To verify any tar or compressed archived file we use the option W (verify). To do this, just use the following examples of commands. (Note: You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW tecmint-14-09-12.tar

tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root    863726 2012-09-15 02:23:41 /home/tecmint/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root  21063680 2012-09-15 02:24:21 /home/tecmint/tecmint-14-09-12.tar
tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar: /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root         0 2012-08-18 19:11:04 xyz.txt
Verify drwxr-xr-x root/root         0 2012-09-15 03:06:08 php/

18. Check the Size of the tar, tar.gz, and tar.bz2 Archive File

To check the size of any tartar.gz, and tar.bz2 archive file, use the following command. For example, the below command will display the size of the archive file in Kilobytes (KB).

# tar -czf - tecmint-14-09-12.tar | wc -c
12820480

# tar -czf - MyImages-14-09-12.tar.gz | wc -c
112640

# tar -czf - Phpfiles-org.tar.bz2 | wc -c
20480

19. Exclude Files and Directories When Creating Tar File

To exclude certain files and directories while creating a tar.gz file, you can use the following command with the --exclude an option that will exclude files and directories when creating the tar archive file as shown.

# tar --exclude='file1.txt' -zcvf backup.tar.gz /home/tecmint
# tar --exclude='/home/tecmint/uploads' -zcvf backup.tar.gz /home/tecmint

In the above command, we excluded file ‘file1.txt‘ and ‘uploads‘ directory from the /home/tecmint folder.

To exclude files with specific file extensions (.txt) when creating a tar archive file, use:

# tar --exclude='*.txt' -zcvf backup.tar.gz /home/tecmint

20. Remove File and Directory from Tar Archive

The following tar command will remove a file or directory from the already created tar file using the --delete option as shown.

# tar --delete -f backup.tar.gz file1.txt
# tar --delete -f backup.tar.gz '/home/tecmint/uploads'

21. Extract Specific File Extension in Tar Archive

The following tar command will only extract files with the specific extension .png from the tar archive file using the --wildcards option as shown.

# tar -xvf backup.tar.gz --wildcards '*.png'

22. Useful tar Command Usage and Options

  • -c – create an archive file.
  • -x – extract an archive file.
  • -v – show the progress of the archive file.
  • -f – filename of the archive file.
  • -t – viewing the content of the archive file.
  • -u – archives and adds to an existing archive file.
  • -j – filter archive through bzip2.
  • -z – filter archive through gzip.
  • -r – append or update files or directories to the existing archive files.
  • -W – Verify an archive file.
  • -A – concatenates the archive files.
  • --wildcards – Specify patterns in the UNIX tar command.
  • --exclude – excludes file and directory when creating the archive.
  • --delete – remove file and directory from the archive.

That’s it for now, hope the above tar command examples are enough for you to learn, and for more information please use the man tar command.

# man tar

If we’ve missed any examples please do share with us via the comment box and please don’t forget to share this article with your friends. This is the best way to say thanks…..