How to resize static/non-LVM partition without losing data in linux

In my previous article, i have showed how can you make dynamic partitions using LVM and can resize it on the fly without unmount, format, etc. You can read it from below link👇

🔰But sometimes in industries, there are static partitions already in use with important data in it and we need to resize them, sometimes increase, sometime decrease, and here the need comes which we are going to do🔰.

Note- For increasing the size of partition there should be enough disk space present

👉I have a folder → /dn4 which is mounted to sdb1 and it is of 2gb in size as you can see in below ss. Also i have a file in it. Now , i want to increase it’s size from 2gb to 5gb. so, let’s see how we can do

🔰For increasing or decreasing the size of partition there are three steps:

  1. Delete the previous partition
  2. make a new partition with larger size and use e2fsck command to save the data(it will help not to loose data when we resize and format partition)
  3. resize2fs command to format the newly created partition🔰

👉Let’s first see how to increase

First, delete your previous partition present on your disk. Go into your disk using fdisk nameofdisk command and use d to delete and w to save it as shown in below ss👇

Now comes the second step

we are now creating a new partition of larger size. see below👇

🔰Note- it is asking to remove the previous signature. select no otherwise you will lose your data.🔰

Then press w to save and exit.

Now run the command → e2fsck -f /dev/sdb1 to remove the bad blocks.

👉Now use resize2fs to format the newly created partition (it only formats the extra 2 gb partition that we have created as old 2 gb partition is already formatted and hence helps not losing data. mkfs command removes old format also and will create entire new inode table and we will loose data. (Hence dont use it.)

Now we are done. lets mount and see our data.

As you can see we have our data present and the size of our disk is also increased from 2Gb to 4Gb.

………………………………………………………………………..…………

Now lets move to 2nd part and see how we can decrease size of a partition. All process is same, lets see practically directly.

As you can see in above ss, i have a partition /dev/sdb1 of 10Gb mounted on /root/dn4 folder. There is also some data present as shown. Now i want to reduce this partition size to 5Gb without losing this data.

For this let us first unmount it👇

you can confirm by df -h command and see there is no info about /dn4 therefore its unmounted successfully.

🍤Now, this time, first run e2fsck and resize2fs command and then we will delete our partition and create a new of smaller size. In this case, first deleting the partition would lead to loss of data as it is possible the part we reduced contain our data🍤.

Now we are good to delete our partition and create a smaller one of 5Gb size(previously it was of 10Gb)👇

🔰Don’t remove the old signature. Always press n to say NO🔰.

Then press w to save the new partition.

you can confirm by lsblk command and see its 5gb in size now🔥

🎇Now time to again mount to the /dn4 folder(you can mount to any folder of your wish)🎇

lets check our data is present or not👇

🔥🔥🔥🔥🔥 so, we did it🔥🔥🔥🔥🔥

thanks for sparing your time here 😊