Disk Management – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Fri, 05 Mar 2021 03:16:03 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 How to Resize and Format Disk with PartedMagic https://linuxhint.com/resize-format-disk-partedmagic/ Tue, 02 Mar 2021 05:59:19 +0000 https://linuxhint.com/?p=92301 PartedMagic is a Linux distribution used for partitioning, data recovery, and other data-related forensics operations. It mainly comes packaged as an ISO tool you can burn to a CD/DVD or USB flash drive and booted live without installing it.

Its primary use is on GNU Gparted and PartitionMagic packages, although PartedMagic is a commercial Software. It’s essential for data.

This tutorial will walk you through PartedMagic operations like creating bootable media, booting up, partitioning, data recovery, etc.

How to Use PartedMagic To Create a Bootable Media

  1. Let us start by getting a copy of the PartedMagic ISO by navigating to the official website: https://partedmagic.com/store/
  2. Next, we need to burn the PartedMagic ISO to a CD or USB. For this tutorial, we will illustrate using a USB drive. You can use tools such as PowerISO (on Windows) or K3B (For Linux).
  3. Download a USB bootable media creation tool. Open the following resource link to download balenaEtcher: https://sourceforge.net/projects/etcher.mirror/
  4. Install the application—it works for Mac, Windows, and Linux systems, then launch it.
  5. Ensure you have your USB drive connected and is visible under the Devices* Tab in balenaEtcher.
  6. Select the PartedMagic iso to burn. If you have multiple USB drives connected, select the correct one and click Flash
  7. ALL THE DATA STORED IN THE DRIVE WILL BE ERASED! BACKUP IMPORTANT CONTENT!
  8. Wait until the Flash process completes, allowing you to boot into PartedMagic.

How to Boot Up PartedMagic

  1. To use PartedMagic, we need to boot into it like a Normal OS.
  2. Reboot your Machine and Press the Boot Key. Check this site for your boot-up key: https://www.disk-image.com/faq-bootmenu.htm
  3. This will take you to the PartedMagic Boot-up Menu. Here’re you have various options and operations you can perform. However, we want to boot into PartedMagic Interface.
  4. Select the Live with Default Settings 64
  5. Selecting this option will boot up the OS, and you will get to the PartedMagic Desktop Environment.
  6. Once in the desktop environment, you can perform tasks like partitioning disks, cloning, erasing, and surfing the web.

How to Use GParted

One of the tools available in the PartedMagic Toolset is GParted. It allows us to perform changes to disks and partitions. For example, we can use it to create a partition for a Linux system installation.

Let’s discuss how to use GParted to partition a disk.

DISCLAIMER: DO NOT try this on a Drive with valuable data. You will lose the data. We take no responsibility for any data loss that may occur due to instructions given in this tutorial. BACK UP YOUR DATA!

We will partition an existing disk and create a new partition that we can use to install a Linux distribution. We will not be formatting the partition. Unless you are an advanced Linux user, stick to Resizing a Partition.

After performing disk changes with GParted, perform a disk check to fix any errors that may keep the installation from booting up.

1. Start by booting up PartedMagic. Once you’re on the desktop, launch GParted by selecting Partition Editor on the desktop.


2. This will automatically launch GParted and list all the available disks. Select the target disk from the main menu. By default, GParted selects the first disk from the list of the connected devices. Choose the desired disk from the dropdown menu.


3. Once you have the desired disk selected, the system will reload the disk, displaying all the disk’s information such as Partitions, File system, Partition Tables, etc.
4. Now, select the partition you wish to resize and right-click. Select Resize/Move option. If you have multiple partitions, select the partition at the end, making it easy to reattach the partition if desired.


5. That will launch the Resize/Move /dev/sd_ dialog box.
6. At this point, you can specify the free space for the resized partition. For ease of use, use the colored indicator to resize the partition. Ensure to leave adequate free space on the existing partition.
7. Once you are satisfied with the size allocated to the partition, click on Resize, which will close the dialog box and navigate back to the GParted main window. Gparted will display changes performed to the disk but not applied.


8. Complete the specified operation to the disks by selecting the Apply option on the main menu. If you’re performing tasks on a disk with data, ensure you are comfortable with the changes before applying.


9. GParted will apply all the pending operations. It will display the progress as it occurs and shows the Applying Pending Operations complete dialog window.


10. To view the log of the recently performed operations, select View Details options.


11. Resizing partition creates an unallocated disk space you can to perform tasks install a new Linux OS. You can perform the partitioning while installing the OS or create them using GParted. For the sake of this tutorial, we will not create the Filesystem.

Formatting A Partition

1. Once we have created a partition, we can format it using GParted.
2. Right-click on the unallocated partition we created and selected NEW


3. You can either create a Primary or Extended Partition. For those using MBR, you cannot have more than three primary partitions, and you will have to stick to the Extended partition


4. Select the Label the File system such as DOS, EXT4, ETX3, etc.
5. Finally, select ADD and apply all the changes.

Conclusion

You can perform other tasks with PartedMagic like copying files using the file manager, cloning a disk, erasing a disk, encryption, erase traces, etc.

TO AVOID LOSING, BE CAREFUL WHILE WORKING WITH PARTED MAGIC DATA!

]]>
How to check the disk space in Ubuntu 20.04 and 20.10? https://linuxhint.com/check-the-disk-space-in-ubuntu-20-04-20-10-2/ Sun, 14 Feb 2021 04:25:25 +0000 https://linuxhint.com/?p=89882
Monitoring the disk space is a very important activity to perform on any device like mobile, laptop, personal desktop computer, and Linux server. For instance, if you want to install any new application or software on your device, then it is important to know about the disk space. By checking the disk space, you can get to know about the free and used space.

We can check the disk space from the terminal and graphical user interface based applications.

The commands discussed in this article are generic and can be used on other Linux distribution like Debian, Linux Mint, etc.

Checking the Disk Space from the Terminal

There are many useful commands for checking the disk space from the terminal. In this section, we will discuss the df and du commands.

Using the df command to check the disk space:

The df (Disk Filesystem) command comes pre-installed on Ubuntu 20.04, Ubuntu 20.10, and many other different Linux based operating system. The df command shows the information of various file systems. Moreover, we can use multiple options with it.

Let’s check the disk space using the df command as follows:

$ df

The df command shows the total used and available space. Moreover, it also shows the total used space in terms of percentage. In the above-given output, the system’s actual disk is /dev/sda5. The df command shows the disk information in 1k-blocks, and it is very difficult to interpret the output. We can use the -h option with the df command to display the disk space information in a human-readable way, as illustrated in the below image:

$ df -h

The df -h command shows the disk space in gigabytes. In the above-given output, it can be seen that in the in /dev/sda file system, the total size of the disk is 29 gigabytes, whereas the used and available space is 13 and 15 gigabytes, respectively.

Using the du command to check the disk space:

The du command stands for Disk Usage. It shows the disk information of every directory and subdirectory. Let’s execute the following du command:

$ du

The size of directories of subdirectories is displayed in 1k-block.

The –h options can also be used with the du command to display the disk information in a human-readable way, as shown below:

$ du -h

The du -h command shows the disk usage in kilobytes and megabytes.

Checking the Disk Space from the Graphical Applications

Checking the disk space using the graphical user interface based applications is very easy in Ubuntu 20.04 and 20.10. There are two graphical applications for checking disk space, i.e., Disk Usage Analyzer and Disks.

Using the disk usage analyzer application:

Open the ‘Application Menu’ and search for the disk usage analyzer application.

Click on the ‘Disk Usage Analyzer’ application icon to open it. You will see the actual disk with ‘Available’ and ‘Total space’. Click on the disk to see more details.

Using the disk utility:

The Disk GNOME utility comes pre-installed in Ubuntu 20.04 and 20.10. Search for ‘Disks’ in the ‘Application Menu’ and open it.

The ‘Disks’ utility shows the total size of the disk and the free disk space.

Conclusion:

Checking the disk space is a very important activity for smooth system usage. In Ubuntu 20.04, 20.10, and many Linux distributions, we can check the disk space from the command-line and graphically. This article explains the commands and graphical applications for checking the system disk usage.

]]>
Sfdisk Tutorials https://linuxhint.com/sfdisk-tutorials/ Tue, 12 Jan 2021 18:36:25 +0000 https://linuxhint.com/?p=85369 Partitioning is vital for system administration. This is the reason the partitioning software comes in so many variants. fdisk and cfdisk are made to be interactive. With parted, you can create everything with commands. Those are the most commonly used ones; sfdisk is not very common. It does have many features, but you can use it for scripts to a much higher degree. For a long time, sfdisk lagged behind on supporting GPT since version 2.26, it does support GPT.

UEFI

This program is still defaulting to MBR, so you have to explicitly state that you are using GPT.

Backing Up

Before you start working with your disk, you have to back up any of your important data to other media! This is an assumption that you must have a clear mind from the start. For making sure that you can restore your current state or implement it on another disk, you can dump the table.

$ sfdisk –dump /dev/sda > sda-tables.txt

The result goes, like text, straight to standard output. In the command above, the file is easy to read. You can also use this to put everything back on the disk. This is what it looks like.

label: gpt
label-id: C9247CFD-5AF7-4AB1-9F62-CDDDFCC12982
device: /dev/sda
unit: sectors
first-lba: 34
last-lba: 976773134
sector-size: 512
/dev/sda1 : start= 2048, size= 1021952, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
/dev/sda2 : start= 1024000, size= 614400, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,
name="EFI system partition"
/dev/sda3 : start= 1638400, size= 126093312, type=E6D6D379-F507-44C2-A23C-238F2A3DF928
/dev/sda9 : start= 623642624, size= 353130496, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
/dev/sda10 : start= 127731712, size= 303263744, type=E6D6D379-F507-44C2-A23C-238F2A3DF928
/dev/sda11 : start= 430995456, size= 192647168, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4

To bring it back or put it on a new disk, you use the restore option. More exactly, you just pipe this result into your next invocation of sfdisk.

$ sudo sfdisk /dev/sda < sda-tables.txt

Incidentally, this is a nice example of a correctly formatted command file for partitioning a disk. All parts are optional, this makes it possible to have another disk connected to the same machine and partition it the same. When you remove ‘dev/sdaX’ in the above file, you can partition any disk with the file.

Interactive Mode

During interactive mode, you need to know what you are doing. The opening display is sparse. You will see the type of disk and its size. When the disk is empty, you will NOT see the old situation, which makes it disconcerting to get started. Nevertheless, the procedure is strict. You have four values to put in for each partition separated by a comma, for default values, you just put the comma.

Before you start, create a label. This example is for a GPT partition.

$ echo "label: gpt" | sudo sfdisk /dev/sdc

This is the way Sfdisk is designed to run, but let’s start with interactive mode.

A simple partitioning runs.

$ sfdisk /dev/sdc
...
# The prompt changes to '>>>'
>>> ,350M, U
>>> ,10G, L
>>> ,,S
>>> write
# The result shows up. Confirm!

The data is written on the disk, and you can start formatting your partitions. As simple as this is, it is also error-prone. Using scripts is the main idea of sfdisk. Let’s go through the options and then the scripting language.

Setting disk label and partition labels

You can also use sfdisk with a command at a time. To do this, you use the parameters with dashes. Many of these commands, you can set with the script files. Setting the disk-label can be done in two ways, you saw one earlier in this tutorial.

$ sfdisk –label /dev/sdc gpt

This sets your disk to become a gpt disk. You have the option to stay with dos or more advisable, use the LegacyBIOSBootable flag for the gpt when you use hardware that does not support gpt. This is rare, so most likely, you will use this flag only for a memory stick that you want, to be able to boot even on old hardware.

You can also set labels for each partition. See the commands below.

$ sfdisk –part-label /dev/sdc 1 boot $ sfdisk –part-label /dev/sdc 2 home

Note the difference between part-label and disk-label. The disk-label only gives a supporting label for other software to use. The disk-label makes the whole disk, either gpt or dos.

Creating scripts

If you have chosen to use sfdisk, you probably have a reason to do so. Some of those reasons may be that you want to make many identical disks. Using fdisk, you can partition an entire disk with one command. Another reason may be that you want to make a new disk with the same scheme as the first one. The simple way to create a script is the dump command from earlier.

$ sfdisk –dump /dev/sdc

Using the output as a guide makes it easier to get started, just remember to check the documentation before doing anything rash. You can, for example, edit the file from before by removing the disk. In the example, the dump came from ‘/dev/sda’. If you remove that part, you still have a valid file.

start= 1024000, size= 614400, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,
name="EFI system partition"

All other parts of the file are also acceptable as commands. Copy in or take away what you want and pipe it into sfdisk.

$ sfdisk /dev/sdc < Disk-tables.txt

Be careful! All commands will be run immediately. Although it will show you the resulting table and ask you to confirm the destruction of the disk you are working with.

Conclusion

This has been a short foray into the power and weakness of using the sfdisk program for your new disks. As you can see from this cursory examination, you can use this program for many things, especially if you have planned before what you need to do. There is also a great degree of freedom in adding sfdisk to scripts.

]]>
Linux lsblk Command Tutorial For Beginners https://linuxhint.com/linux-lsblk-command-tutorial-for-beginners/ Sun, 03 Jan 2021 17:45:26 +0000 https://linuxhint.com/?p=84057 Wikipedia defines a device file or special file as an interface to a device driver that appears in a file system as if it were an ordinary file. Block devices include Hard disks, Flash drives, Optical media, etc.

This tutorial discusses how to use the lsblk command to display information about block devices in a system. The tool utilizes the /sys virtual filesystem to fetch information about the devices. However, it’s worth noting that the tool does not display the RAM disk in the output.

How to Install lsblk?

The lsblk command comes bundled as part of the utils-Linux package that has various low-level system tools. The utils-Linux package also contains tools such as fdisk, login daemon, all of which are essential for Linux system functionality.

To install the package on Ubuntu and Debian based distributions, use the command below:

sudo apt-get install util-linux

For Fedora and REHL family, use the following command:

sudo yum install util-linux-ng

How to Use lsblk?

Without arguments, the lsblk command will display a list of the block devices in a tree-format, as shown below:

sudo lsblk

The output from the lsblk command comes arranged in various columns. The following are the columns from the command and what they represent:

  1. NAME: The first column shows the device name.
  2. MAJ:MIN: The second column, respectively, indicates the major and minor device numbers.
  3. RM: This column displays Boolean values for removable and non-removable devices. 1 indicates that the device is removable, and 0 indicates that the device is non-removable.
  4. SIZE: This displays the device size in a readable format, i.e., In K, M, G, T, etc. You can use the lsblk –bytes /dev/<name> to display the size in bytes.
  5. RO: This column shows the read-only status of a device. 1 indicates the device is read-only, and 0 indicates not read-only.
  6. TYPE: This column shows the type of devices, such as disk, loopback device, partition, or LVM device.
  7. MOUNTPOINT: This displays the mount point on which the device is mounted.

Devices in lsblk output
Some of the devices you will see in the lsblk output are:

  1. Loopback devices: Loopback devices mainly contains ISO image, a disk image, a file system, or a logical volume image. Their main identification is using loop0, loop1, etc.
  2. CD-ROM: They are mainly indicated with sr0 and have properties such as RM of 1.
  3. Disk partitions: The main indicator used for Disk partitions is sda, followed by sda1, sda2…sda(n).

List all devices
The default lsblk command does not display all free devices. To do this, add the -a argument to the command as:

sudo lsblk -a

List format
The lsblk command will display the output in a tree-like format. However, you can change this by using the -l argument as:

sudo lsblk -l

Selective Columns
We can also use -o followed by the names of the columns we want to output for selective information as:

sudo -o NAME,TYPE,MOUNTPOINT

Device Permissions
To view device permissions and ownership, use the command:

sudo lsblk -m

Information about specific devices
To list information about a particular block device, specify the device name in the lsblk command as:

sudo lsblk /dev/sda1

List devices without the header
To remove the header from the output, use the -n flag in the command as:

sudo lsblk -n

Display SCSI Devices
To display SCSI devices only, use the -S flag in the command:

sudo lsblk -S

Conclusion

In this tutorial, we discussed how to work with the lsblk command to display information about block devices that are connected to the system. The only way to master working with this command is to experiment. Therefore, consider experimenting with the commands discussed in this tutorial.

]]>
Linux Parted Command Line Examples https://linuxhint.com/linux-parted-command-line-examples/ Tue, 08 Dec 2020 12:15:45 +0000 https://linuxhint.com/?p=80221 There are many partitioning tools available, in which most of them have an interface in the form of a list. With hot keys and some tinkering, you can get a disk partitioned pretty quickly. However, fdisk is not meant to be used inside scripts; sfdisk is meant for scripting. Your opinion on which is best may vary. Here, you can hear about how to run parted.

You can run parted, only from the command line but in two modes; command line and interactive. In interactive mode, you have a new shell with only parted commands, while in the command line, you enter a new command each time. There is also an -s option, so you can run many commands in one go.

Check Before

Before you begin anything, you should make sure that the disk is what you think it is. Use the list option to do this. Note that parted will only show the disks that your user has access to, so you may have to bee root to find your new shiny disk. Also, it shows all disks.

$ parted -l

The list, if you have a new disk, should look something like this:

Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table:
Disk Flags:
Number Start End Size File system Name Flags
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0
has been opened read-only.
Model: QEMU QEMU DVD-ROM (scsi)
Disk /dev/sr0: 599MB
Sector size (logical/physical): 2048B/2048B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
2 19.7MB 116MB 96.5MB primary esp

Notice that there are two disks, the CD and the new hard disk. Observant readers will notice that I am using a virtual machine to run these commands. If you want to print only your disk, you need to use the format below:

$ parted /dev/sda1 – print
[root@nixos:~]# parted /dev/sda -- print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
3 1049kB 537MB 536MB fat32 ESP boot, esp
1 537MB 19.3GB 18.8GB ext4 primary
2 19.3GB 21.5GB 2147MB primary

Labels

The labels, when using parted, designates the type of partition table you want to use. Make sure you have booted with a UEFI machine if you choose ‘gpt’. Your system will not boot if you get this wrong! To check what you have, print the firmware. Confusingly, when you format the disks with mkfs, you can put labels and a different concept.

$ ls sys/firmware

If it contains an efi line, you are good to go!

acpi dmi efi memmap qemu_fw_cfg

If you see what is below, you have to choose msdos. I am excluding Macs here because I have not yet experience using them.

acpi dmi memmap qemu_fw_cfg

Now that you are sure that you have a UEFI implementation on your machine, you can set the label.

$ parted /dev/sda – mklabel gpt

For the second case, msdos, you do the same but with another parameter.

$ parted /dev/sda – mklabel msdos

Now, you can start creating partitions!

Partitions

For the UEFI case, you need to put some space for the boot or ESP partition. This is where you can put all the booting stuff that UEFI/EFI supports. For this reason, you must leave space in front of the main partition. In this first command, we also add space for a swap partition. Look at the command below:

$ parted /dev/sda – mkpart primary 512MiB -8GiB

This command starts the partition at 512MiB and ends it at 8GiB before the end of the disk. Notice the ‘-‘ in front of the second term. For the second case, msdos, you do the same but starting closer to the beginning. The MBR is only up to a 1MiB, including the backup.

$ parted /dev/sda – mkpart primary 1MiB -8GiB

In both cases, your disk will fill everything in between the start and just before the end. This partition will fill the space between the start and end.

$ parted -l

To see what is happening to your disk. Do this between every step until you are confident with what happens.

On the rest of the disk, put your swap partition.

$ parted /dev/sda – mkpart primary linux-swap -8GiB 100%

Notice that the procedure does not need to know the size of the disk, as long as it is well over 8 gigabytes. Obviously, based on the size of your swap, you can set the amount of RAM in your case. In a virtual machine, you should probably put a maximum of 2GiB.

Finally, for the UEFI case only, create the UEFI System partition.

$ parted /dev/sda – mkpart ESP fat32 1MiB 512MiB

As you see in this command, you can set the file system for a partition when you create it. You can also set it after you created it.

Filling the Disk

You can fill the disk with parted without knowing its total size. There are many ways to do this, and you saw an example earlier where you put 100% to reach the end of the disk. Other ways to fill your disk is using s; for the sector, %; for the percentage, and chs; for the combined cylinder head and sector. The best part, in this case, is that you can be wrong about where to start, and parted will prompt you for the closest possible solution, and you can answer Yes.


Figure 1: Parted will give you a suggestion when you are wrong.

Setting Flags

In the UEFI case, you want to make sure the ESP is set to be just that by running parted.

$ parted /dev/sda – set 3 esp on

You set all flags this way.

Removing a Partition

Made a mistake? Changing strategy? You can remove partitions, one by one. You can change the number of the partition to choose the correct one.

$ parted /dev/sda – rm 1

Nothing adds there.

Rescue

You can also rescue your old disk using the rescue parameter. This works even when you have removed a partition by mistake.

$ parted /dev/sda – rescue 1MiB 20GiB

The action is slow, but it can help you recover from problems. When parted finds something, it will prompt you for action.

Conclusion

Parted is a very powerful way to partition your disk. You can choose to run a command at a time or open a shell.

]]>
How do I Mount and Unmount a File System in Linux? https://linuxhint.com/mount_unmount_file_system_linux/ Thu, 12 Nov 2020 09:42:13 +0000 https://linuxhint.com/?p=76499

We all know that Linux based systems rely heavily on files. These files are grouped together in the form of multiple file systems, and for accessing a particular file system with an added level of ease, you can attach that file system to any desired location till the time you wish to access that file system. Once you are done, you can simply detach that file system from that location. In this whole process, the attachment of a file system on a specific location is known as “mounting,” whereas the detachment of a file system from a specific location is known as “unmounting.”

Also, the location to which you attach a file system is formally known as a “mount point.”  There are multiple file systems available on a Linux operating system. Some of these file systems are mounted by default, and some of them are unmounted, which means that you can easily mount them on your own. In today’s article, we will be teaching you the methods of mounting and unmounting a file system in Linux Mint 20.

Method of Mounting a File System in Linux Mint 20

For mounting a file system in Linux Mint 20, you need to perform the following steps:

The “lsblk” command in Linux is used to display all the mounted and unmounted file systems. Before mounting a file system in Linux Mint 20, we would like to list down all the file systems by using the “lsblk” command in the manner shown below:

$ lsblk

Running this command will present all the file systems, i.e., the ones that are mounted as well as the ones that are available for mounting in a tree-like structure. From the following image, you can recognize the already mounted file systems by seeing their mount points as highlighted in the image. Rest of all, the file systems, i.e., the ones that do not have any mount point mentioned after their names, are available for mounting.

Now we will try to mount a file system in Linux Mint 20 by running the “mount” command in the manner shown below:

$ sudo mount NameOfFileSystem MountPoint

Here, you need to replace NameOfFileSystem with the exact name of the file system that you wish to mount and MountPoint with the location where you want to mount your desired file system. Also, in Linux, you cannot mount a file system without having the root user privileges, and since we were not logged into the root user account, that is why we have used the “sudo” keyword before the “mount” command. Otherwise, this command would have rendered an error message.

Since in our case, we attempted to mount a file system that was already mounted, i.e. /dev/sda1, that is why our terminal displayed the message that this file system has already been mounted on the specified mount point as shown in the following image. However, if we would have chosen a file system that was not mounted before, such as /dev/sda2, /dev/sda5, etc. then running the “mount” command would have mounted our file system on the specified mount point.

Method of Unmounting a File System in Linux Mint 20

For unmounting a file system in Linux Mint 20, you need to perform the following steps:

Now we will attempt to unmount the same file system that we tried to mount in the method shown above. For that, we need to run the “umount” command in our terminal in the manner shown below:

$ umount NameOfFileSystem

Here, you need to replace NameOfFileSystem with the exact name of the file system that you wish to unmount, which in our case was /dev/sda1, as shown in the image below:

Alternatively, we can also use the “umount” command in the following manner:

$ umount MountPoint

Here, you need to replace MountPoint with the location from where you wish to unmount a file system, which in our case was /boot/efi, as shown in the image below. Also, if running any of the above two commands render any error message, then it could possibly happen because of missing the “sudo” keyword. Therefore, for being on the safe side, you can try running these commands with the “sudo” keyword for providing root user privileges for unmounting a file system.

Running either of these commands will simply hand over the control back to you without displaying any output on the terminal. Being a user, you might find yourself in doubt whether your specified file system has been correctly unmounted or not. To verify this, you can try running the “lsblk” command once again. If your specified file system does not have a mount point any longer in the output of this command, then this will imply that our file system has been unmounted successfully. In the example that we demonstrated, we wished to unmount the /dev/sda1 file system from the /boot/efi mount point. The image shown below verifies that the /dev/sda1 file system has been unmounted successfully since its mount point is no longer there in the output of the “lsblk” command, as shown in the image below:

Conclusion

In this article, we walked you through the detailed methods of mounting and unmounting a file system in Linux Mint 20. By following these methods, you can conveniently mount any file system of your choice to any desired location whenever you wish to access that file system or, more precisely, the files within it easily. Similarly, you can also unmount any file system of your choice at any time when you feel like you do not need to access that file system anymore. Both these methods are extremely simple and allow you to get your work done just within a few minutes.

]]>
In Linux, How to Find the Size of Directory and Subdirectories? https://linuxhint.com/find_directory_size_linux/ Thu, 05 Nov 2020 19:37:43 +0000 https://linuxhint.com/?p=75658

Before installing any new program on your computer system, the first thing that every user needs to ensure is that the system has a sufficient amount of storage space to hold that program and run it without any performance issues. Similarly, whenever you intend to copy or download files in any of the directories of your computer system, you need to check if that directory has an adequate amount of storage space available or not.

There are multiple ways in which you can check the sizes of the directories and subdirectories within any operating system that is both GUI based and CLI based. However, since the Linux operating system mostly operates with CLI, therefore, its users are generally interested in looking for terminal-based methods of performing any task. Therefore, in today’s article, we will be talking about the methods of finding the sizes of directory and subdirectories in Linux Mint 20.

Methods of Finding the Size of Directory and Subdirectories in Linux Mint 20:

For finding the sizes of directory and subdirectories in Linux Mint 20, you can use any of the three methods described below:

Method # 1: Using the “ncdu” Command:

For using the “ncdu” command to find the sizes of directory and subdirectories in Linux Mint 20, you will be required to perform the following steps:

First, we have to launch the terminal in Linux through which we will execute the “ncdu” command as shown in the image below:


Since the “ncdu” command is not installed by default on the Linux systems, therefore, we will first install it by running the following command in the terminal:

$ sudo apt install ncdu


Before running this command, you must ensure that your Internet is working correctly, otherwise, this command will fail to execute. This command will take a few minutes to install the “ncdu” command and all the required dependencies successfully, after which your terminal will display the output shown in the image below:


Now when the “ncdu” command has been installed on our system, we can easily use it to find the sizes of directory and subdirectories by running it in the following manner:

$ ncdu

Here, we only wanted to find the sizes of the Home directory and its subdirectories that is why we did not have to mention any path. However, if you wish to find the size of any directory other than the Home directory, then its path must be mentioned after the “ncdu” command.


The sizes of the Home directory and its subdirectories are shown in the image below:

Method # 2: Using the “du” Command:

For using the “du” command to find the sizes of directory and subdirectories in Linux Mint 20, you will be required to perform the following steps:

Since the “du” command is already installed on the Linux systems, therefore, we do not need to install anything rather we can use this command straightaway in the manner shown below:

$ du –h

Again, if you wish to find the size of a directory other than the Home directory, then you must mention its path after the “du –h” command. Here, the “-h” flag is used to print the output in a human-readable format.


The sizes of the directories within the Home directory are shown in the following image:

Method # 3: Using the “tree” Command:

For using the “tree” command to find the sizes of directory and subdirectories in Linux Mint 20, you will be required to perform the following steps:

The “tree” command is also not installed in the Linux systems by default but it can easily be installed by running the command shown below:

$ sudo apt install tree


Before running this command, you must ensure that your Internet is working correctly, otherwise, this command will fail to execute. In our case, we already have installed the “tree” command on our system for some other purpose that is why our terminal displayed the following output after running this command:


Now we can run the “tree” command to find the sizes of the directory and subdirectories in the manner shown below:

$ tree –d –h

Again, the “-h” flag is used with the “tree –d” command to display the output in a human-readable format. Moreover, if you wish to find the size of any directory other than the Home directory, then you must mention its path right after the “tree –d –h” command.


The sizes of all the directories within the Home directory are shown in the following image:

Conclusion:

By using any of the three methods discussed in this article, one can easily find out the sizes of the directory and the subdirectories within it. Also, we have just demonstrated to you the examples in which we wanted to find out the sizes of the Home directory and the subdirectories within it. However, you can also find the sizes of any other directory and its subdirectories if you wish to, by using very simple methods. All you have to do is to provide the correct paths of those directories after the commands that are discussed above and you will be able to find the sizes of any directories of your Linux Mint 20 system.

Out of all the three methods that are discussed in this article, the first method is useful if you want to have a detailed report about the sizes of a directory and its subdirectories along with the total size. The second method is useful if you wish to have the sizes of even the subdirectories within the subdirectories whereas the third method is useful if you only intend to get the sizes of the first level subdirectories within a directory.

]]>
How to Format Disk Partitions on Linux https://linuxhint.com/disk_partitions_linux/ Sun, 01 Nov 2020 08:45:11 +0000 https://linuxhint.com/?p=75001 A partition is the storage space on a hard drive that has been designated for a certain purpose. In most cases, a storage device comes with a single partition. However, modern operating systems allow physical storage systems to be divided into multiple logical storage systems. Within the operating system, a partition may make it seem as if multiple drives are connected, as each partition comes with its own filesystem and storage capacity.

A partition must be formatted to prepare it for use, but a partition can also be formatted to erase all its data, to establish a different filesystem, or to fix errors. In this article, we will show you how to format disk partitions on Linux, assuming that you have already created the target partition.

Formatting disk partitions on Linux

Depending on the tools being used, the formatting process can be carried out in two ways. However, before you begin, make sure that there are no data in the partition that you are afraid to lose.

Formatting a partition using a GUI
This method is probably more usable for most Linux users because it provides a user interface and explains the process at each step. To show you how to carry out this process, we will use GParted: an open-source partition editor that offers an organized UI for managing disk partitions.

GParted allows you to resize, copy, and move partitions without data loss, and it allows you to rescue data from lost partitions. GParted does not come pre-installed with most Linux distros, but you can install it by using the appropriate installation command for your Linux distro.

For Debian/Ubuntu and derivatives:

$ sudo apt install gparted


For Fedora and derivatives:

$ su -c "yum install gparted"

For openSUSE, SUSE Linux and derivatives:

$ sudo zypper install gparted

For Arch Linux and derivatives:

$ sudo pacman -S gparted

GParted can also be used through live CD/USB regardless of your distro, and you can find the official GParted live ISO here. To do so, you must make a bootable USB flash drive. (Click here to learn how to make a bootable USB flash drive from an ISO.)

Now, launch GParted. GParted requires root permission to launch because it makes system-level changes.


The image below shows the main window of GParted. First, select the appropriate disk from the top-right corner. In our case, there’s only one disk connected.


The target partition to format is /dev/sda5. Right click on the target partition, then click “Format to” and select the target filesystem format. For Linux, ext3/ext4 is the most appropriate filesystem format. If you plan to use the partition with other operating systems, then it is better to use fat16/fat32. However, feel free to use a different filesystem formats if it suits your needs.


Follow the above steps for all your target partitions. Then, click the “Apply” button. By clicking “Apply,” you can make sure that your configuration is correct before your changes are finalized.


GParted will then ask you whether you are sure you want to run the tasks. Once you have confirmed that it is safe to proceed, click “Apply”.


Then, the operation will begin. Click “Close” once the process is completed.


Formatting a partition using CLI
This process is more complex than that using GParted. However, for experts and advanced users, this method might be more convenient.

First, we can list all existing partitions by using the following command:

$ lsblk


If you want to list all the non-formatted partitions, then run the lsblk command with the the -f flag, as follows:

$ lsblk -f


Here, our target partition is /dev/sda5, and as you can see, it is already mounted. You cannot format a partition that is already mounted. To unmount a partition, run the command below. Note that you can get the partition mount point from any of the lsblk commands.

$ sudo umount -v <mount_point >


Now, the partition is ready to be formatted. Before proceeding, make sure that there are no important data on the partition. Unlike GParted, the partition will be instantly formatted once the command is run.

Once you are ready, run the following command. Here, we use the mkfs tool to format the partition. The mkfs tool supports a wide variety of filesystems, including ext3, ext4, fat16, fat32, ntfs, apfs, and hfs. In this example, we will make an ext4 filesystem at /dev/sda5.

$ sudo mkfs -v -t <filesystem> <partition_label>


The above command can also be run in a different way. Here, the command will create an ext4 filesystem at /dev/sda5.

$ sudo mkfs.ext4 -v /dev/sda5


Voila! The partition is successfully formatted! To use it, you have to mount it again. Click here to learn how to use the Linux mount command to mount filesystems.

Final thoughts

Here, we have shown you how to use two convenient methods to format a disk partition. With the right tools and knowledge, disk partition formatting is not an inherently difficult task, but it is important for you to check to see if any important data will be lost in the process.

In addition, you should consider the stress this process may put on a drive. If you are using an SSD or a RAID with multiple HDD/SSD, then the amount of data written can negatively impact the life expectancy of the storage devices. Furthermore, while modern storage devices are fairly resilient, the older the storage device, the riskier the process.

]]>
What is LVM (Logical Volume Management), and what are its Benefits? https://linuxhint.com/whatis_logical_volume_management/ Wed, 21 Oct 2020 00:06:14 +0000 https://linuxhint.com/?p=72565 Logical Volume Management or LVM is a framework of the Linux operating system that has been introduced for the easier management of physical storage devices. The concept of logical volume management is very much similar to the concept of virtualization, i.e. you can create as many virtual storage volumes on top of a single storage device as you want. The logical storage volumes thus created can be expanded or shrunk according to your growing or reducing storage needs.

In today’s article, we will be learning about this framework in detail. We will show you the method of checking all the disk volumes that your LVM can manage. Finally, we will state some benefits of using LVM, followed by the conclusion of our article.

A Detailed Explanation on LVM:

As we have already mentioned that the concept of using LVM is very much similar to virtualization; therefore, its working is also more or less the same as virtualization. We will try to understand the working of LVM by creating an example scenario. Generally, we have a physical device that is divided into multiple partitions. All these partitions have a file system installed on them which can be used to manage these partitions.

However, we know that we can have multiple physical devices as well rather than having just a single physical device. In this case, each of these physical devices has its own partitions and the respective file systems. The management of all of these partitions can become a mess if handled as it is. This is where the concept of logical volume management comes into play. It allows you to aggregate all the partitions of different physical devices into a single logical volume group from where they can be managed centrally.

This volume group is a combination of used and unused disk space. From this volume group, you can extract multiple logical volumes depending upon your requirements. Moreover, the exact space allocated to each logical volume can also be increased or decreased depending upon your needs. Then you can have separate file systems for each of these logical volumes. In this way, you can manage the overall storage space more efficiently and can assign the unused space to all those users that ask for it without any problem.

Before logically partitioning your physical disk, you need to have the knowledge about all the volumes that your LVM can access and manage. Therefore, we will explain to you the method of checking all the volumes that LVM can manage in the following section of this article.

Note: We are using Linux Mint 20 to demonstrate this method.

Method of checking all the Volumes that LVM can manage:

For checking all the disk volumes that LVM can manage, you will need to run a disk scan which can be done in the following manner:

First, we will launch the terminal in our desired Linux distribution which in this case is Linux Mint 20. Its terminal is displayed in the image below:


Now we will run the disk scan command in the terminal in the following manner:

sudo lvmdiskscan

The disk scanning process requires root user privileges. For that, you must make use of the “sudo” keyword before this command. However, if you are already logged in to the root user account, then you are allowed to omit this keyword.


Once you run this command in your terminal, the details of all the disk volumes will appear on your screen as shown in the image below:

Benefits of LVM:

The following are some of the biggest advantages of using logical volume management or LVM:

  • It allows you to efficiently manage and utilize your physical disk space.
  • It is capable of creating such logical volumes whose capacity can be increased or decreased depending upon your requirements.
  • If you intend to keep backups of your data on multiple logical volumes, then this increases the availability of your data.
  • A new physical device can easily be added below the volume group with zero downtime and without any service disruption.
  • LVM allows you to partition a single physical device into multiple logical partitions as well as it also allows you to integrate multiple physical devices into a single volume group.

Conclusion:

This article provided you with a brief overview of logical volume management which is a very important framework of the Linux operating system. Without this framework, we have very limited options for managing our storage space. Also, these options fail to resolve the conflicts between the varying storage needs of multiple users. That is why LVM is considered as an essential component of the Linux based systems.

]]>
How to Check Disk Space in Linux from the Command Line https://linuxhint.com/check_disk_space_linux_command_line/ Sun, 18 Oct 2020 11:46:57 +0000 https://linuxhint.com/?p=72220 Disk space monitoring is considered a very important task when working with any device, whether it is a mobile phone, laptop, desktop, or even a tablet. It is important to ensure the proper functioning of any device by keeping an eye on the disk space. This helps you in identifying the programs or the applications that are using a large amount of space and informs you if you are about to run out of disk space.

Like every other operating system, Linux also provides multiple ways to keep track of the disk space on your device, including both CLI-based and GUI-based methods. In Linux, however, most operations are performed via the command line. Therefore, Linux users are more likely to be interested in methods of checking disk space via the command line. This is why our discussion today will revolve solely around methods for checking disk space in Linux from the command line.

Note: All the methods shown below have been tested in Linux Mint 20.

Checking the Disk Space in Linux from the Command Line

There are multiple ways to check the disk space in Linux; however, the most effective ones involving the command line interface have been presented below.

Method 1: Using the df Command

The df command stands for Disk Filesystem, and it is a built-in utility in the different flavors of the Linux operating system. The df command is used to monitor disk space utilization, as well as the total available space. To check the disk space using this utility, proceed as follows:

First, launch the terminal in Linux Mint 20 by clicking on its desktop icon, shown in the image below:

After launching the terminal in Linux Mint 20, execute the following command in the terminal:

$ df

Running this command will display the total space of the whole file system, the total amount of used space, as well as the available space, along with some other information, as shown in the following image:

Method 2: Using the df command with the -a Flag

The df command can also be used in conjunction with the -a flag, which is used to display the disk space of all the file systems (i.e., your actual file system and also the dummy ones). Perform the steps shown below to use the df command with the -a flag:

Launch the terminal in Linux Mint 20 and execute the command shown below:

$ df –a

The output of this command will be quite large, and you will have to scroll through your terminal to view the entire output. This is because the -a flag does not only print the disk space of a single file system; rather, it does so for all available file systems.

Method 3: Using the df command with the -h Flag:

Certain technical terms may not be easily understandable by a new user. For example, in the outputs of both the methods discussed above, you can see a column named “1K-blocks.” This column represents the total number of “1K-blocks” present in each file system. In other words, this is the size of the file system in bytes, which can be difficult to interpret and memorize. Basically, it is a technical way of representing the size of each file system, but this is not so intuitive for a layman. Therefore, the -h flag can be used with the df command to display the disk space in a more human-readable format. To make this happen, follow the steps provided below:

Launch the Linux Mint 20 terminal as explained above, and then execute the following command:

$ df –h

Running this command will display the disk space of your file system in a way that you will easily be able to interpret, i.e., the disk space will be displayed in megabytes (MBs), gigabytes (GBs), etc. You can see this output in the image below:

In the same manner, you can also use the -k and -m flags with the df command to check the disk space in Linux via the command line in kilobytes and megabytes, respectively. This can be done if you require the disk space in a specific unit for a specific purpose. By allowing this, the df command or utility provides you with the flexibility to check your disk space in whichever format you prefer.

Conclusion

This article showed you how to check the available disk space in a device using the command-line in Linux. All the three methods described above were a variation of the df command. You can easily use the df command to check the disk space in Linux from the command line by adjusting the flags according to your requirements. Or, you can simply use this command alone and without any flags. The output of this command will help you to see your current disk space usage and the amount of free space.

Apart from the use cases of the df command discussed in this article, this command can also be used to check the disk space of a specific file system; to know the total, available, and used inodes of a file system; to check the type of each file system; to filter out the file systems based on a particular type; and much more. However, all of these use cases are beyond the scope of this article. That is why we have only focused on the use cases of the df command that are directed towards checking the disk space. ]]> Check Directory Size in Linux https://linuxhint.com/check_directory_size_linux/ Thu, 25 Jun 2020 13:51:01 +0000 https://linuxhint.com/?p=62208 It is quite easy to check the size of directories and files using GUI. Getting the size of a directory using the command line can be much more difficult than when using GUI. With the ‘ls’ command, you can list the contents of a directory but you cannot see the exact space or directory size. Instead, you must explore more commands to get the exact size of the directory or file.

In this article, you will learn how to check directory size on Linux using the command line environment. All the commands demonstrated in this article were run on an Ubuntu 20.04 system. All methods and steps are performed in the terminal. You can quickly open the terminal window by typing Ctrl + Alt + t.

Following are some methods you may use to check directory size on Linux systems. We will explore these methods one by one:

Method 1: Check Directory Size Using du Command

The default command used to check the size of the directory is known as the ‘du’ command, which stands for disk usage. The du command is installed on most Linux distributions. Using the du command, you can view the current directory size of your system, as follows:

$ du

The above command displays a list of the home directory contents. The numbers displayed to the left show the sizes, in kilobytes, of each object.

Using the -h option, you can also display the output in more descriptive form, as follows:

$ du –h

The above command displays the space in the kilo, mega, and Gigabytes with numbers.

To find the size of the specific directory, use the following syntax:

$ du –h /directory-path

You will need to run the above command as the sudo user, because some directories require certain permissions access particular directory content.

Use the following command to check the directory size of the /var directory:

$ sudo du –h /var

With the -hc option, you can display the size of the specific directory in human-readable form, as follows:

$ sudo du -hc /var

You can also change the subdirectory path depth using the max-depth option. For example, if you only wanted to display the top directory, then you would need to set the max-depth=0, as follows:

$ sudo du –hc ––max-depth=0 /var

Similarly, to retrieve the top directory with one layer of subdirectory, then you will set max-depth=1.

$ sudo du –hc ––max-depth=1 /var

If you want to explore more commands related to du, then you can use the following command:

$ man du

Method 2: Check Directory Size Using tree Command

The tree command is used to display directories, subdirectories, and files in the tree format. You can make this command more useful by inputting flags and options for customization. The tree command does not come already installed on most of the Linux systems. You can install this command using the apt package manager, as follows:

$ sudo apt install tree

To display the current directory, subdirectories and files use the following command on the terminal:

$ tree -d -h

With the tree command, you may also retrieve the content of a specific directory using the following syntax:

$ tree /directory-path

To list the content of /var directory, you will use the following command:

$ tree /var

After completing the command, it will display the total number of directories and subdirectories.

To learn more about the tree command, use the following command:

$ man tree

Method 3: Check Directory Size Using ncdu Command

The NCurses Disk Usage, abbreviated ‘ncdu,’ is also used to check the directory size. ncdu is not installed by default on most Linux systems. You will need to install this command using the command line through the apt package manager, as follows:

$ sudo apt install ncdu

Using ncdu, you can view an interactive display of your system disk usage. Execute the following to try out this command:

$ ncdu

The upper top left corner displays the current directory being viewed. The left column displays directory size in the numerical value, where the # signs indicate the size next to each directory. Using the arrow keys, you can navigate between these lines. Here, the purpose of the right arrow is to browse the directory, and the purpose of the left arrow is to move you back.

With the ncdu command, you may also target a particular directory, as follows:

$ ncdu /var

To quit the ncdu interface, press ‘q,’ and for help, press ‘?’.

In this article, you learned how to check directory size of using the terminal command line on Ubuntu 20.04 Linux systems through three different methods. You may explore more commands related to the tree, ncdu, and du commands using the terminal. These commands can be used on all Linux distributions. If you have any questions or suggestions, feel free to comment below.

]]>
5 Best Linux File Systems https://linuxhint.com/best_linux_file_systems_5/ Thu, 19 Mar 2020 05:29:52 +0000 https://linuxhint.com/?p=56762 File Systems are very important for every operating system and Linux and its distributions are no exception. Most of the recent Linux distributions use Ext4 file system which is modern and upgraded version of older Ext3 and Ext2 file systems. Reason behind most of the Linux distributions use Ext4 file systems is that it is one of the most stable and flexible file systems out there.

Many of you might have heard about BtrFS and it becoming default file system for Linux distributions. BtrFS is still under development and has long road to cover. Choosing the best file system for Linux and its distributions might be difficult task. Data safety and security is so important in this world, so finding the reliable and stable file system for Linux is important to avoid data loss and corruption. So today in this article I’m going to give you round-up of best file systems for Linux and its distributions.

Ext4

There should be no surprise that Ext4 tops the list of best Linux file systems. Ext stands for Extended file system and it was first developed especially for Linux and its distributions. Ext4 is upgrade to Ext3 and Ext2 file systems and comes with new features such as less fragmentation, larger volumes and files and improved flash memory life with the help of delayed memory allocation.

As mentioned earlier, Ext4 is one of the most modern file systems and default one in recent Linux and its various distributions.

ReiserFS

If you’re looking for a file system which will help you store huge number of small files then ReiserFS is the best alternative file system for you. It offers compact file allocation and small files along with metadata for preventing using large file system blocks. When first introduced in 2001 and then upgraded in 2004 it was a major competitor to Ext file systems until further development stalled by developers of ReiserFS.

Since this file system stopped receiving active support from developers, this paved the way for file systems like BtrFS which could become next big thing in Linux file system world.

BtrFS

BtrFS initially developed and designed by Oracle stands for B-Tree File System. Many experts think BtrFS is a long-term solution as compared to Ext4 File system and they are spot on thanks to features like drive pooling, snapshots, ability to do online defragmentation and also transparent compression. As mentioned earlier in the article, many think BtrFS is going to replace Ext4 as a default file system on many Linux distributions as well as enterprise server.

Many BtrFS fans call it as Butter FS or Better FS, tells how much they love working on this File System. As it is still undergoing development phase, you might find it unstable but still love it thanks to its unique features. Many of you might have heard of TRIM especially those who own SSD. Keeping Solid State Drives (SSD) healthy on Linux is necessary, TRIM helps you wipe out unused blocks which is very important. What I liked most about this File System is its snapshot feature.

XFS

Initially developed for SGI IRX operating system in 1994 by Silicon Graphics and then in 2001 it was ported to Linux operating system. I find it almost similar to Ext4 File System because various features of XFS match with Ext4 in many ways. Some of its features are file fragmentation with delayed allocation; it offers really great performance while dealing large files. Like BtrFS it doesn’t offers snapshot feature which is very popular.

If you’re going to work with small files then I would recommend not to use this File System as its performance is worst in case of small files. But when it comes to large files it has to be the most reliable as compared to competitors. XFS also supports SSD features, which is great for modern Linux machines.

F2FS

F2FS is File System which is specially recommended for power user like system administrators or developers. It was initially designed and developed by Samsung. You need to first configure and tweak Linux kernel before using this File System on Linux and its distributions. It takes great deal of hard work and patience to setup F2FS on Linux.

It deals with the flash memory and that is how modern day SSD’s store the data. Pro users will like working on Linux with F2FS. Even though this is not the straight forward File System as other listed here, it is worth a shot because once you get used to it, you can do whole lot of things with this on Linux.

So these are the 5 Best File Systems which you can use on top of Linux and its distributions like Ubuntu. There are some other File System which are not listed here and many of you might be using them then share your thoughts at @LinuxHint and @SwapTirthakar.

]]>
How to List Files Ordered by Size in Linux https://linuxhint.com/list_files_ordered_by_size/ Fri, 21 Feb 2020 10:07:30 +0000 https://linuxhint.com/?p=55412 The present article briefly explains how to list or display files and directories ordered by size. This can be easily achieved with the command ls (list). Before sorting the files, in order to explain each option applied let’s do a long listing which will print file sizes, among more information, without sorting it (in the second screenshot I explain how to sort), this is achieved by adding the -l (lowercase -l for long listing) as shown below:

# ls -l

The first line displays the entire size of the directory you are listing files in. When adding the -l option the output will display file permissions in the first column, the hard links, the owner, the group, the size in bytes, month, day and time and finally the filename.

If you want to sort this output according to file size, from bigger to smaller you need to add the -S (Sort) option.

# ls -lS

As you can see the output lists the files and directories sorted by size, but in bytes which is not very human friendly (1 Byte is 0.000001 MB in decimal and 0.00000095367432 MB in binary).

To print the output in a human friendly way you only need to add the -h (human friendly) option:

# ls -lSh

As you can see in the output above, now file sizes are shown in GB, MB, KB and Bytes.
Yet you are only seeing regular files without hidden files, if you want to include hidden files in the output you need to add the option -a (all) as shown below:

# ls -lSha

As you can see hidden files (starting with a dot) are printed too now.

Additional tips:

The following additional tips will help you to limit the output to a specific unit size different than bytes. The problem of this option is the output is never exact when the file is smaller or bigger than an exact number of that unit.

If you want to print in a specific size unit only instructing ls to display all files with the closest size in that unit only you can achieve it, for example, to print all files in MB you can add –block-size= options specifying MB with M as shown in the syntax and screenshot below:

# ls -lS --block-size=M

As you can see now the size is in MB only, the bigger file is 115 MB, the second 69 MB, etc. Files with KB or Bytes size won’t be printed accurately, they will be shown as 1 MB which is the closest size can be printed if limited to MB.
The rest of the output remains exactly the same.

Using the same option (–block-size=) you can display the output in GB size instead of MB or bytes, the syntaxis is the same, replace the M for a G as in the example below:

# ls -lS --block-size=G

You can also print the size in KB units by replacing the M or G for a K:

# ls -lS --block-size=K

All the examples above will list files and directories sorted by size in the unit you want, with the clarified problem mentioned above the output wont be accurate for files which don’t match a exact unit size. Additionally, these examples didn’t include hidden files (which start with a .). To do so, you will need to add the option -a (all) which will display hidden files too, therefore, to print files sorted by size by bytes run:

# ls -laS

As you can see now hidden files, starting with a . (dot) are printed, such as .xsession-errors, .ICEauthority, etc.

If you want to print files and directories sorted by size in MB including hidden files run:

# ls -laS --block-size=M

To print or display all files and directories sorted by size shown in GB including hidden files run:

# ls -laS --block-size=G

Similarly to previous commands, to print files and directories ordered by size shown in KB including hidden files run:

# ls -laS --block-size=K

Conclusion:

The command ls brings a lot of functionalities which help us to manage files and print information on them. Another example could be the use of ls to list files by date (with -lt options).

I hope you found this tutorial on How to list all files ordered by size in Linux useful. Keep following LinuxHint for more tips and updates on Linux and networking.

]]>
How to Use the Linux stat command https://linuxhint.com/linux_stat_command/ Fri, 21 Feb 2020 10:00:25 +0000 https://linuxhint.com/?p=55429 The stat command is a useful utility that reports info about a certain file or filesystem. It comes pre-installed with almost all the Linux systems out there, so no need to worry about installation.

Let’s check out how to use stat to get all the important data you need for a specific file/filesystem.

Why stat command

Sometimes, you may want to know some key details about a certain file/filesystem, for example, the file’s size, access permissions, inode number, time of last access/modification etc. Agreed, you can check out a lot of details about a file using ls. However, stat offers far more in-depth about the target file/filesystem.

When should you use stat? Whenever you need those extra information. For a quick comparison, let’s check out the file info of file1.txt. To better understand the output, learn more about the lh command.

$ ls -lh file1.txt

Now, let’s check out what stat has to offer.

$ stat file1.txt

That’s a lot of information! You don’t need all of them in everyday life but in some specific situation, stat comes in super handy.

Linux Stat Usage: Location

Most of the Linux commands run from the /usr/bin directory.

$ which stat

Linux Stat Usage: Checking file/filesystem info

We’ve already seen this in action, right? The command structure goes like this.

$ stat <option> <file_filesystem>

To see the in-depth info of a file/filesystem, run this command. In this case, it’s my trusty file1.txt.

$ stat file1.txt

There are TONS of information about the specific file, right? All the information presented is labelled. Depending on your requirement, pick up the important one. From my experience, the most used ones are file permissions, inode and Uid and/or Gid.

Now, let’s check out an example with a filesystem. In this case, it’ll be the mount point of the filesystem. For example, this command will show the information of the root filesystem.

$ stat /

In Linux, (almost) every single thing is a file. Any filesystem is a file itself, so the output won’t be any different.

Linux Stat Usage: Terse form

When you’re running stat normally, all the information is printed in a human-readable structure. Want a short and simple version of the output? Add the “-t” argument.

$ stat -t /

The terse format is a special format that uses a predefined list of stat format specifiers.

Linux Stat Usage: Custom format

This is another interesting usage of the stat command where you can design the desired output structure of stat. To do this task, stat offers a LONG list of available format specifiers.

Here’s how the command looks like.

$ stat --format=<format_specifiers> <file>

For example, the “%A” specifier returns the file/filesystem permissions in a human-readable format.

$ stat --format=%A file1.txt

The “%U” specifier returns the owner of the file/filesystem.

$ stat --format=%U /

To get the size of the file, use the “%s” format specifier.

$ stat --format=%s file1.txt

Want the file type? Use “%F”.

$ stat --format=%F file1.txt

For the inode number, use “%i”.

$ stat --format=%i file1.txt

Now, let’s combine all of them together into a single command line. It will look like this.

$ stat --format="%A %U %s" /

There are tons of other format specifiers that stat support. All of them are listed in the man page of stats.

Now, as we’ve previously seen the “-t” or “–terse” argument, it’s a predefined value for the following arguments.

$ stat --format="%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C"
 <file_filesystem>

As for the “–terse –file-system” argument, the predefined value is as follows.

$ stat --format="%n %i %l %t %s %S %b %f %a %c %d" <file_filesystem>

These outputs are very useful if you’re using stat in any sort of script, especially in bash scripts. New to bash script? Check out the beginner’s guide to bash scripting.

Printf vs format

The stat command supports –printf argument that functions basically the same manner as –format. However, the main difference between them is how the output is produced.

Let’s get it clear with an example. Here, I’m running stat with both arguments with the same format specifier.

$ stat --format="%A %U %s" file1.txt
$ stat --printf="%A %U %s" file1.txt

As we can see, the –format argument adds a new line after the output ends. However, the –printf doesn’t. To make sure that there’s a new line after the output, you have to add “\n” at the end of the format specifier string.

$ stat --printf="%A %U %s\n" file1.txt

Differentiate between file and link

In certain situations, you might be actually working with a link. However, by default, stat doesn’t differentiate between a link and a physical file. There’s a dedicated argument to get around this issue. Just pass the “-L” argument.

$ stat -L <file_filesystem>

Final thoughts

The stat tool is a pretty straightforward one. All of its functions are managed by simple arguments and flags. Once you know which one to use, you can reap the most benefit out of it.

To learn further in-depth about the stat command, I highly recommend checking out the man page. It contains all the supported format specifiers and some additional arguments that you may find interesting.

$ man stat

Enjoy!

]]>
List Files Sorted by Size in a Directory Tree on Linux https://linuxhint.com/list_files_sorted_size_linux/ Tue, 11 Feb 2020 11:31:07 +0000 https://linuxhint.com/?p=54951 Most file systems sort the files in alphabetical order of names. A user might, however, have some other preferences and wish to sort the files on the basis of their size. This might make the listing information more useful and productive. When it comes to file listing and sorting in Linux, we trust the ls command and would like to enable you to use it in a way that it perfectly customizes the output according to your preferences.

In this article, we will explain how you can list files, sorted by size, in a directory tree. This includes directory listing both in the graphical user interface and the command line.

We have performed the procedures and commands mentioned in this article on a Debian 10 Buster system. You can, however, replicate these steps on most Linux distros.

Sorting Files in a File Manager

The file manager that you are using has a default preference, mostly alphabetical, to sort files and folders in a directory. You can, however, set custom preferences so that it sorts your files based on their size. Nautilus is the default file manager for Linux Debian and Ubuntu. This is how you can sort the files, size-wise, on Nautilus in Debian.

Method1: Open the File Manager and then click on the downward arrow located in the top bar, as follows:

This will present a list of options on the basis of which you can sort the files and folders in a directory. Click on the Size option in order to sort the files on the basis of their decreasing size.

Method 2: You can also set the Preferences of your file manager in a way that it displays files and folders based on a given criterion. Click on the options button from the top bar as follows:

Then, click on the Preferences option that will open the Preferences view with the ‘Views’ tab open. Click on the List Columns tab that lets you set the order of information that appears in the list view of the File Manager.

Select the Size option and drag it to the top so that this criterion is the top preference to sort your files and folders.

Sorting Files in the Command Line

The ls command in Linux lets you list files and their information based on the criteria you specify in the form of various flags and their options. If no  criteria is specified, the ls command sorts all entries alphabetically. Here, we will use the ls command, with different flags, to make it list files and folders on the basis of their size.

You can open the Debian command line, the Terminal, by searching for it from the Application Launcher search as follows:

You can then use the -S option as follows to sort the files on the basis of size, largest first:

$ ls -S

Another option is to use the sort flag with the ls command using the following syntax:

$ ls  --sort=WORD

Where ‘WORD’ can be used to specify the following options:

  • none-instead of the -U flag
  • size- instead of the -S flag
  • time- instead of the -t flag
  • version- instead of the -v flag
  • extension- instead of the -X flag

Now that we have to sort our files on the basis of size, we will replace WORD with size as follows:

$ ls --sort=size

Here are a few options that will make the size sorting make more useful for you:

Print a long listing format

In order to print a list of size-sorted files in a lengthwise format, you can use the -l flag with the already described -S flag:

$ ls -Sl

You can see in the above image how the list is not displayed in a lengthwise format.

Reverse print a size-sorted list

If you want the ls command to print the lowest sized files first, you can use the -r flag along with the -S flag:

$ ls -Slr

The above command prints a lengthwise, revere size-sorted list of files and folders in a directory.

Print a more human-readable list of sorted files

The -h flag, when used with -S and -l, prints sizes like like 1K 234M 2G etc. which are more human readable. Here is how you can sort the files size-wise and make the list more human readable:

$ ls -Slh

Exclude directories when printing a size-sorted list

If you only want to list the files in a directory, and not the folders, sorted by size, you can make use of the grep command as follows:

$ ls -lS | grep -v '^d'

You can see that this command only lists files in a directory along with all the other information.

These were various ways through which you can not only sort the files on the basis of size, but also customize the listing some more by using some other commonly used flags. So whether you are on the Linux UI or the command line, you can now make the file listing much more informative.

]]>
List Disks on Ubuntu https://linuxhint.com/list_disks_ubuntu/ Tue, 14 Jan 2020 13:40:02 +0000 https://linuxhint.com/?p=53423 In this article, I am going to show you how to list disks (i.e. SSDs, HDDs, USB thumb drives) attached on your computer from Ubuntu. So, let’s get started.

Using lsblk Command:

You can list all the attached disks on your computer from Ubuntu using the lsblk command as follows:

$ sudo lsblk

The ones with the TYPE disk are the physically attached disks on your computer.

The ones with the TYPE part are the partitions of the disks.

The lsblk command without any filter shows a lot of loop devices that are used by the Ubuntu snap apps. It is annoying I know. Luckily, you can easily get rid of them from the list.

You can use the -e7 option with lsblk to hide the loop devices.

$ sudo lsblk -e7

Now that the loop devices are gone, the list looks much clearer now.

You can also do the same thing with grep.

$ sudo lsblk | grep -v '^loop'

You can also hide the CD/DVD rom device from the list. Just add the -e11 option along with -e7 option.

$ sudo lsblk -e7 -e11

You can also list the attached disks on your computer, and their vendor and model information using lsblk as follows:

$ sudo lsblk --scsi

As you can see, all the attached disks on my computer are listed. Their vendor and model information are also listed.

Using fdisk Command:

You can also list all the attached disks on your computer from Ubuntu using the fdisk command as follows:

$ sudo fdisk -l

fdisk shows all the disks, their total size, their partitions, size of each partitions etc. But sadly, the list is too long and there is no easy way to hide the loop devices.

But if you know the device name (i.e. sda, sdb) of your disk, you can find more information about it very easily with fdisk.

$ sudo fdisk -l /dev/sdb

You can also find information about more than one disk at the same time with fdisk. Just put the disks paths as fdisk arguments one after the other.

$ sudo fdisk -l /dev/sda /dev/sdb

Using parted Command:

You can use parted to do the same thing. parted may not be installed by default on Ubuntu. But you can easily install it with the following command:

$ sudo apt install parted -y

You can get a fdisk like output for all the storage disks attached on your computer with parted as follows:

$ sudo parted -l

As you can see, the output format is the same as in fdisk, but parted does not list the annoying loop devices. That’s a good thing.

Using GNOME Disks App:

If you don’t like to use the command line, then there are many graphical programs you can use to list disks on Ubuntu.

GNOME Disks is the default partitioning app on GNOME desktop environment. You can use it to list the disks attached on your computer.

First, open GNOME Disks from the Application Menu.

GNOME Disks should be opened. On the left side, you will see all the attached storage devices/disks on your computer. To find more information about the disk, click to select the disk. The disk size, partitions and other information about the disk should be listed on the right side.

Using GParted App:

If you don’t like the command line, then you can also use the GParted Partition Editor app to list all the attached disks on your computer.

GParted is not installed by default on Ubuntu. But you can easily install GParted with the following command:

$ sudo apt install gparted -y

GParted should be installed.

Now, you can start GParted from the Application Menu.

In order to use GParted, you will need superuser privileges. Type in your login password and click on Authenticate.

GParted should start. You can select a disk from the dropdown menu at the top right corner of GParted.

As you can see, all the disks attached on my computer is listed in the dropdown menu. Just select a disk from the list.

The partitions of your selected disk should be displayed.

So, that’s how you list all the disks attached on your computer from Ubuntu.

I’ve shown some of the common methods in this article. If you know of listing disks any other way on Ubuntu, be sure to let me know.

Thanks for reading this article.

]]>
How to do data recovery from hard drive https://linuxhint.com/data_recovery_hard_drive_linux/ Tue, 14 Jan 2020 04:32:10 +0000 https://linuxhint.com/?p=53392 This tutorial shows how to recover data from storage devices in Linux. In this case the data will be recovered from a SanDisk USB pendrive of 32 GB, yet  the process shown in this tutorial is the same as for a regular hard disk. This tutorial will focus on two of the most popular file carving tools, Foremost and PhotoRect, both described in the File Carving Tools article. Both of them will be explained from the installation process on Debian 10 Buster to data recovery.

Data Recovery From Hard Drive with Foremost:

To begin lets see the connected storage devices by using the command lsblk, on the console run:

# lsblk

Lsblk will show all available storage devices and partitions, including swap and optical devices, in this case I want the sdb device.

Note: to learn more on the lsblk command read How to List all Linux Disk Devices.

As you can see the 32 GB USB pendrive was called sdb and that’s the device I’ll work on.

Data Recovery from USB drive with Foremost:

To begin data recovery from a USB drive start by installing Foremost using the APT package manager on Debian or based Linux distributions by running:

# apt install foremost

Once installed you can display the man page to check all available options:

# man foremost

From the man page we understand the flag -i is to determine an input file, from which Foremost will start working. It is usually aimed to work with images such as these produced by tools like dd or Encase. To launch Foremost in the simplest way without additional flags run the following command replacing /sdb for the device ID you want to recover data from.
Run:

# foremost -i /dev/sdb

Where sdb put the correct device.
Once executed the carving process will look like:

Note: you can also specify partitions like for example /dev/sdb1.

When the process ends run ls to confirm the creation of a new directory called output:

# ls

As you can see the directory output exists, to see the recovered files enter it using the command cd (Change Directory) and then run ls:

# cd output
# ls

Inside you’ll see directories for all file types Foremost managed to recover, additionally you’ll see a file called audit.txt with a report on carved files.

You can check what files were found inside each directory by running ls <directory>:

# ls jpg/

You can also browse all recovered files through a graphical file manager:

Data Recovery From Hard Drive with PhotoRec:

PhotoRect is together with Foremost the most popular file carving or data recovery tool both for professional forensics and domestic use. While Foremost does a smarter recovery showing a faster performance, PhotoRec’s brute force shows better results when carving files. This section shows how to carry out data recovery from hard drive using PhotoRec.

To begin on Debian and based Linux distributions install photorec by running:

# apt install testdisk

PhotoRec man page is almost empty, Photorec is pretty simple to use and only needs to be executed, a didactic friendly interface similar to the one of CFDISK will show up to guide you during the whole process.

Once installed run it by calling the program:

# photorec

Remember to run PhotoRec with enough permissions to access the device to be carved.

On the first screen you need to select the source disk or image from which PhotoRec needs to recover the data. In this case I’m selecting the device /dev/sdb as shown in the image below:

In this step you need to select the partition from which you want to recover the data.
If partitions aren’t found and listed before proceeding with a search using the keyboard arrows move to File Opt to explore the available options as shown in the image below:

As you can see within File Opt you can increase the result accuracy you want by specifying the type of files you are looking for. Select the type of files you want and then press b to continue, or Quit to go back.

Once back in the previous screen select Search and press Enter to continue to begin the data recovery process.

At this stage Foremost will ask what type of filesystem the device has or used to have, in this case it was FAT or NTFS, select the proper filesystem, even if it’s currently broken and press ENTER.

Finally PhotoRec will ask where you want to save the files, I just left the Desktop but you can create a dedicated folder for it, after choosing the destination press C to continue.

The process will start and may last some minutes or hours depending on the size.

At the end of the process PhotoRect will notify the creation of a directory with the recovered files, in this case recup_dir* inside the Desktop previously selected as destination.

Like with Foremost you can list all files from the console:

Or you can browse files using your preferred graphical file manager:

Conclusion on data recovery from hard drive with PhotoRec and Foremost:

Both tools lead the file carving market, both tools allow to recover any type of files, Foremost supports carving jpg, gif, png, bmp, avi, exe, mpg, wav, riff, wmv, mov, pdf, ole, doc, zip, rar, htm, and cpp and more. Both tools are compatible with disk images like dd or for Encase. While PhotoRec relays on brute force providing a deeper carving, Foremost focuses on block headers and footers working faster. Both tools are included in the most popular forensic suites and OS distributions such as Deft/Deft Zero live or CAINE which were described at https://linuxhint.com/live_forensics_tools/.

Using PhotoRec or Foremost brings the possibility to apply high level forensics tools even for domestic use,  the mentioned tools have not complex flags and options to add the launching them.

I hope you found this tutorial on How to Data Recovery from Hard Drive useful. Keep following LinuxHint for more tips and updates on Linux and networking.

]]>
How to List all Linux Disk Devices https://linuxhint.com/list_linux_disk_devices/ Thu, 09 Jan 2020 21:52:40 +0000 https://linuxhint.com/?p=53212

How to list disk devices in Linux with lsblk

The proper way to list block devices such as hard disks is using the command lsblk.

# lsblk -a

Note: the -a flag forces lsblk to list also empty devices.

In the first column you can see all the connected devices, in the image above you can see 3 devices: sda, sdc and sdd. Lets see some information on each.

Lsblk, The column NAME:

The NAME column shows the name of each disk device and partition. As you can see in the extract of the column below the disk SDA contains 4 partitions: sda1, sda2, sda3 and sda4. You can ignore the “_crypt_” for now.

The second listed disk device is sdc with only one partition, sdc1.

The second listed disk device is sdd with only one partition, sdd1.

The last listed device is the DVD or CDROM:

Lsblk, The column MAJ:MIN:

Following the column NAME there is the column MAJ:MIN is the internal identificator for each device the kernel uses according to the device type.

Lsblk, The column RM:

The column RM shows if the device is removable (where 1 yes and 0 no), in this case last 2 disk are external and the last device is the DVD.

Lsblk, The column SIZE:

The column SIZE displays the size of each of the disk devices listed.

Lsblk, The column RO:

The column RO means Read Only, being 1 positive and 0 negative, if 0 then devices are not read only, if 1, they are read-only.

Lsblk, The column TYPE:

The column TYPE displays the type of device such as devices, partitions, optical devices,  encrypted partitions and more.

Lsblk, The column MOUNTPOINT:

The column MOUNTPOINT shows the mount point of each device or partition, the next image shows the partitions of the device sda.

The screenshot below shows an efi partition belonging to sda1, the encrypted sda2 root partition, the sda3 for boot and an encrypted sda4 SWAP.

The flag -l will show the devices and partition as a list rather than as tree:

# lsblk -l

The flag -f instructs lsblk to inform the filesystem of each partition:

# lsblk -f

Additional flags for the command lsblk are available and explained in the man page which you can also access at https://linux.die.net/man/8/lsblk.

Listing disk devices in Linux with alternative methods: fdisk and fstab

While the correct way to list disk devices in Linux is using the command lsblk, other options are available.
The first one to try is fdisk, to list all block devices with fdisk run:

# sudo fdisk -l

How to show free and used space in Linux disk devices

Additionally to listing your storage devices you may need to show the free and used space, this can be achieved using the command df.

As you see in the screenshot below, the availability of space of all disks formerly listed is now displayed.

The command df allows you to easily check each partition identified both by its device name or its mountpoint, yet, the -h option makes it easier to be readed by humans.

# df -h

For example the image above shows the partition /dev/sda2_crypt which is the root mount point / has 150 GB available and 64 GB used.

]]>
Linux Commands for Disk Space https://linuxhint.com/linux_commands_disk_space/ Thu, 09 Jan 2020 21:47:19 +0000 https://linuxhint.com/?p=53233 This tutorial shows Linux commands for disk space information gathering. The commands explained here are df and du, additionally the tutorial shows how to list the largest files in your system

Linux Command for Disk Space information

The df command in Linux systems shows information on the space used and available by disk devices. The first part of this tutorial focuses on showing different options to display information on the storage devices usage.

To begin to print space information on the connected devices run df without flags:

As you see the first column shows the device or partition, the second column shows information on the blocks, then the used and available space followed by the percentage and the last column is the mount point.

We can improve the output by adding the -h flag making it readable by humans.
Run the command df with the -h flag:

# df -h

You can print all sizes in MB by replacing the h for m like the next example:

# df -m

The -T flag instructs df to print the filesystem type of each partition under the new column TYPE, to try it run:

# df -T

Note this is a big case T.

To instruct df to print information on all filesystems use the flag -a (all):

# df -a

You can instruct df to print information on a specific type of filesystem by adding the flag -t (low case) followed by the filesystem type:

# df -t ext4

You can get additional information on the df command on its man page or online at https://linux.die.net/man/1/df.

Linux Linux du Command for Disk Space information

Additionally to the command df to check disk space information on Linux there is the command du (Disk Usage). It is also simple to use, to try it run it without flags:

# du

The last line shows the total amount of space used, over 60 GB, to print the result in human friendly output like with df add the flag -h.

# du -h

With the command du you can specify the location you want to get information about. Just specify a mount point,filesystem, directories or files after the flags, in the next example

# du -h /boot

In the following example I use du to print information on the space used by a simple directory:

You can get additional information on the du command on its man page or online at https://linux.die.net/man/1/du.

Show largest files in Linux

The commands df and du mentioned before are great to show the disk usage by each filesystem, device, partition, directory or file.  But if you want the largest files in your Linux to be listed you can run:

# find / -printf '%s %p\n'| sort -nr | head -10

The command above will print the 10 biggest files in your system, if you want to display a different number of results replace the number 10, you also change the root volume (/) if you want to show the largest files of a different location.

The following example shows a listing of the 5 largest files within the /usr/ directory:

# find /usr -printf '%s %p\n'| sort -nr | head -5

I hope you found this brief article on Linux Command for Disk Space useful, thank you for reading it.

]]>
Linux df Command https://linuxhint.com/linux_df_command/ Mon, 19 Aug 2019 18:48:58 +0000 https://linuxhint.com/?p=45397 There are plenty of tools out there that offer seamless methods of checking the disk space utilization. These tools are extremely useful in keeping your disk consumption at check. We may not feel like it but disk space is a valuable resource for our systems.

Linux is a legendary platform with a number of tools built-in within the ecosystem. Of course, there are other 3rd-party tools for checking the disk space utilization. However, there’s also a powerful built-in tool for doing the job – df.

The term “df” stands for “disk filesystem”. Using this tool, you can get a full summary of disk space usage of a particular filesystem. There plenty of scenarios where this can be useful. Let’s jump into the usage, tips, and tricks of “df”!

Location

Almost all the built-in tools are located under the “/usr/bin” directory. “df” is no exception.

which df

Filesystem disk usage

When we run “df” just by itself, it reports the present filesystems on the disk(s) along with other valuable information like their mount points, usage percentage, used and available space and the total number of blocks.

df

If you’re confused with the output, you can always add the “-h” flag. Essentially, this flag tells “df” to print the output in “human-readable” format.

df -h

Display info of all file systems

The previous command is, most of the times, going to be all you need to check out the disk usage of different filesystems. However, if there are dummy file systems present and you want their stats as well, there’s a different flag to use. For this purpose, pass the “-a” flag.

df -a

Again, for a human-readable format, add the “-h” flag.

df -ah

Or,

df -a -h

Disk usage of a specific filesystem

Are you just interested in one particular filesystem?

In the previous examples, “df” would show the disk usage of all the filesystems in the system. For checking out the disk usage of a specific filesystem, use the “-T” flag.

df -T /home

df -T /

I recommend using the “-h” flag for a better understanding of the output.

df -hT <filesystem>

Display result in MB/GB

Displaying the disk usage stats in MB (megabyte) format is quite helpful to many of us. If you want “df” to output the result in MB, just use the “-m” flag with whatever command you’re throwing at it.

df -m

As we can see, the output only shows numbers. These numbers are integers and represent the MB count of each file system.

However, when working with some BIG filesystems, the result in MB is going to be pretty much useless, right? In that case, we have the “-h” option. Yes, it’s the same “human-readable” flag we know and love.

df -h

The benefit is, it will show in GB if the size is too big; otherwise, it’ll show as MB or KB.

Inodes info

When running “df”, it doesn’t show the inode info of that filesystem. If you need the inode info, use the flag “-i”.

df -hi

File system type

Every single filesystem in the ecosystem of Linux has to have a certain type. In cases, the filesystem can be extremely crucial to determine the scope of performing a number of actions. For this purpose, use the “-T” flag.

df -T

Specific filesystems

Sometimes, you might want to check the condition of certain filesystems only. For example, how about just the info of all the ext4 filesystems? If your goal is something like that, then use the “-t” flag followed by the filesystem type.

For example, let’s check out the information of all the ext4.

df -at ext4

Here, “df” will report all the filesystems that match the type ext4.

To understand the output better, here’s how I like to modify the command.

df -aTt ext4

df -aTt tmpfs

Now, the filesystem type is noticeable and easily understandable of the effect of the “-t” flag.

Excluding certain filesystems

In the previous example, we only printed the information about a certain filesystem type, right? How about excluding a certain type and printing about every other filesystem? Yes, “df” also allows such operation. In this case, the flag is “-x” followed by the filesystem type to be excluded.

df -ax ext4

To understand the effect of this flag, use it with “-a” and “-T”.

df -aTx ext4

Local filesystems

If your system is highly modified and depends on remote filesystems as well, then “df” will also show all of them whenever you’re running any of these commands. If you want local and remote filesystems at the same time, then run the “df” commands as normal. However, if you’re interested in just the local filesystems, then just add the “-l” flag.

df -aTl

“df” version

This is just a trivial thing. If you’re interested in the version of “df”, run the following command.

df --version

As the output says, it’s a part of the GNU coreutils. So, any system with this toolkit will have “df” preinstalled.

Final thoughts

There’s not so much thing going on with “df”. It’s a really simplistic yet powerful tool that’s able to meet the needs of almost all the things anyone is about to do with their filesystems. If you’re interested in all the available options and in-depth guide, run the following commands.

df --help

man df

info df

Enjoy!

]]>
Linux Mount Command https://linuxhint.com/linux_mount_command/ Wed, 14 Aug 2019 07:45:11 +0000 https://linuxhint.com/?p=45099 In the Linux ecosystem, mounting is one of the major operations that the system relies on. In fact, the filesystem of Linux is solely dependent on the “mount” mechanism.

Mounting mechanism

Before jumping deeper into the “mount” command, let’s have a look what it actually is. In Linux, mounting is the process of attaching an additional filesystem on top of the currently accessible filesystem on the computer.

A quick note on the filesystem: it’s a hierarchy of directories that systems use to organize file(s) and folder(s) on a storage media. Filesystem exists in every single storage solution: USB flash drive, CD-ROM, HDD, SSD, and even floppy disks! In the case of UNIX/Linux and similar systems, the filesystem starts with the root directory (noted as “/”). Under “root”, all the other child filesystems exist.

Using mount

Because of the mounting mechanism at the core of the system, “mount” plays an important role in shaping the Linux ecosystem. This command offers a ton of utility for numerous situations. Whenever you’re running “mount” to mount a filesystem, it contacts with the kernel to complete the operation.

In this article, I will try to cover important and interesting ones as much as possible. However, it’s completely up to you and your creativity to use “mount” to your advantage.

“mount” location

Run this command.

which mount

It’s located in the “/usr/bin” directory. This directory is generally the home of almost all the commands you’ll ever run in the history of your Linux.

Basics

Here’s how the very basic structure of running “mount” commands looks like.

mount --source <source> --target <target>

While using “–source” and “–target” flags aren’t so common, I strongly recommend people doing so for having a clear distinction on the purpose of the command.

In order to mount a certain filesystem, “mount” needs the source and the target. For example, I grabbed the Ubuntu installation ISO and I wish to mount this file. Run the following commands.

sudo mkdir /mnt/ubuntu_iso

sudo mount -o loop -t iso9660 --source ~/Desktop/ubuntu-19.04-desktop-amd64.iso
 --target /mnt/ubuntu_iso

In this series of commands, we created a directory where the ISO file will be mounted. Then, we told “mount” to use that directory for mounting the ISO in that directory.

If you end up with an error like “failed to setup loop device”, then check if the command is correctly typed. In other cases, make sure the loop module of the kernel is running.

lsmod | grep loop

No output in this step means the module isn’t running. Start it using “modprobe”.

sudo modprobe loop

The command should run just fine now.

Listing all the mounts

When running “mount’ just by itself without any additional parameters, it will return the list of all the currently mounted filesystems in the computer’s storage.

mount

Or,

mount -l

Mount version

mount -V

This will display the software version of “mount”.

Filesystem info

If you have a certain type of filesystem in mind, you can use “mount” to check what mounts use this filesystem.

mount -t <filesystem_type>

For example, this command will list all the filesystems that use “ext4”.

mount -l -t ext4

Using /etc/fstab

This is a special system configuration file that contains the filesystems for the OS. While it’s now easier to mount devices and filesystems, at the early ages, fstab was the only option to tell the computer to check for any specific filesystem and mount it automatically.

It’s located on “/etc/fstab”.

bat /etc/fstab

Bat is a superior alternative for cat with additional features like syntax highlighting and formatting.

As mentioned in the file, fstab follows the following structure for the table.

<filesystem> <mount_point> <type> <options>
<dump> <pass>

In the list, there’s an entry by default (the system’s HDD). Let’s explain it part by part.

This part is the filesystem that will be mounted. It’s set by the UUID.

Next is the mount point. In this case, it’ll be mounted as the root.

Now, the filesystem type. It uses the ext4 journaling file system.

These are the options that mount will follow when mounting the filesystem.

The next values are for “dump” and “pass” respectively.

In this article, we’re not going deeper into the fstab craziness. If you’re interested, feel free to search the internet for suitable guides and explanations with additional tips and tricks.

Mounting a device

In almost all the modern Linux distros these days will automatically mount any storage device you connect to it automatically, for example, USB flash drives. However, if it didn’t mount or auto-mount is disabled, you have to mount it manually.

At first, check if the device is being recognized by the system.

fdisk -l

Create a suitable directory for mounting the device.

sudo mkdir /run/media/usb

Time to finally mount the drive into the folder.

sudo mount --source /dev/sdb1 --target /run/media/usb

Note: If you’re willing to mount storage with some unorthodox filesystems like exFAT, you need the appropriate drivers. In the case of Ubuntu, it’s “exfat-utils” and “exfat-fuse”. In the case of Arch and other Arch-based distros, it’s “exfat-utils”. If you’re using anything other than that, make sure to check for suitable exFAT solution for your system.

Unmounting any mount

Once you’ve mounted, the filesystem will be present there forever. In the case of core filesystems, it’s important that they remain mounted. However, removal devices must be unmounted first and then removed. Otherwise, it might cause issues like data loss, data corruption, and other damages. I accidentally destroyed a USB flash drive of mine.

To unmount any filesystem, use the “unmount” command.

sudo umount -v <target>

Final thoughts

It’s always great to consult the man and info pages for in-depth guides on any tool. “mount” is no different.

man mount

info mount

Enjoy!

]]>
How to Measure Your Disk Activity for Linux Web Server https://linuxhint.com/disk_activity_web_server/ Mon, 12 Aug 2019 07:29:04 +0000 https://linuxhint.com/?p=44980 The era of the digital century has begun and everyone seems to be busy with the technologies. Businesses are striving harder for achieving maximum excellence by providing the best services to their potential customers. Now imagine a scene where the website operates at a very slow pace and takes longer time to load the entire site. The business admins want to figure out the exact reason behind having a slow website.

In this article, we will be looking at some crucial tips that are essential to speed your website load time by measuring your disk activity.

What is the Reason behind a slow website?

The user needs to send a request in order to access any information from the server. At the server-side, the information resides on the files which are stored in Hard Disk Drives (HDDs) or more preferably to the disks. Then, the disk starts rotating to get to the required disk sector for reading the user requests. Meanwhile, when the processor waits for the file to revert back, the process leads to waiting time about a few milliseconds.

Such an access time adds up to the processing of the heavy data servers as they have to proceed innumerable data requests and therefore, it becomes a performance bottleneck for retrieving the data from the storage device. Hence, for preventing the app performance from the disk latency, it becomes important to keep an eye on your disk usage along with the I/O to check out the best core functionalities of your app. Let us begin with some simple terms.

What is Disk I/O?

Disk I/O includes all of the read and write operations and defines the speed at which the data transfer needs to take place between the RAM and hard disk drive. In basic terms, Disk I/O is used to measure an active disk time and is used as a measure of performance to distinguish between the storage devices, for instance – HDDs, SSDs, and SAN. This allows you to monitor the Read and Write operations on your system and set the limits to get alerted every time you reach the max level.

Monitor disk I/O on Linux servers

Using IOSTAT and IOTOP commands

At first, you need to type the top command in your terminal for checking the server load and if the outcomes are low, then go for wa status to know more about the Read and Write IOPS in your hard disk. If the output is positive, then check I/O activity in the Linux box by using iostat or iotop commands.  You can monitor the disk utilization with the help of an individual process by making use of the iotop command whereas the iostat allows monitoring your I/O device load time. Sometimes, it is used to evaluate the balance of the disk activities.

It is obvious on your Linux loop devices that the stats provided help you clutter the display when you only want to focus on disks. Take a look at the command given below that does not provide -p option for monitoring the disks.

The iotop command is the top-utility while looking at the disk I/O by gathering all the usage information through the Linux kernel and get you an idea of the most demanding terms in your disk I/O. Look at the given example to check out when the loop time has been set to 5 seconds. The display will get updated by itself and overwrites the output.

atop and ioping commands

The ioping command is an altogether variant type of tool, but it can report disk latency by letting you know how long it takes for a disk to respond your requests and help in identifying the disk problems.

The atop command is similar to the top that provides a lot of information on system performance by including some of the stats on your disk activity.

Also, you can easily manage the disk stats, it can happen by making use of the command in a similar way.

dtstat command

This command offers stats on a lot more than just disk activity. Thus, if you wish to view your disk activity, it is recommended to make use of the -d option. You will get a continuous list of disk read/write measurements as shown down until you stop it getting displayed with a ^c. Also, it should be noted here that after every subsequent row, you will need to report the disk activity within the following time interval where the default is just set to one second.

Wrap Up

Here, we come to the end of the article. We hope one of these commands will tell you what you need to know when it comes to disk performance. Frequent use of these commands will allow you to get an accurate insight behind the specific busy or slow disk which you are supposed to check. Till then – Keep Learning!

]]>
Linux dd Command https://linuxhint.com/linux_dd_command/ Sun, 11 Aug 2019 08:13:23 +0000 https://linuxhint.com/?p=44855 “dd” is a linux CLI tool. Now, the current version of “dd” is a part of the GNU coreutils, is the one that every single UNIX/Linux system features. The primary purpose of this tool is to convert and copy files. However, it’s quite different than other file copying tools like “cp”.

dd features

“dd” can be used for various purposes:

  • Using “dd”, it’s possible to directly read and/or write from/to different files provided that the function is already implemented in the respected drivers.
  • It’s super useful for purposes like backing up the boot sector, obtaining random data etc.
  • Data conversion, for example, converting ASCII to EBCDIC encoding.

dd usage

Here are some of the most common and interesting usage of “dd”. Of course, “dd” is far more capable than these things. If you’re interested, I always recommend checking out other in-depth resources on “dd”.

Location

which dd

As the output indicates, whenever running “dd”, it launches from “/usr/bin/dd”.

Basic usage

Here’s the structure that “dd” follows.

dd if=<source> of=<destination> <options>

For example, let’s create a file with random data. There are some built-in special files in Linux that appear as normal files like “/dev/zero” that produces a continuous stream of NULL, “/dev/random” that produces continuous random data.

dd if=/dev/urandom of=~/Desktop/random.txt bs=1M count=5

 

The very first options are self-explanatory. It means to use “/dev/urandom” as a source of data and “~/Desktop/random.txt” as the destination. What are the other options?

Here, “bs” stands for “block size”. When dd is writing data, it writes in blocks. Using this option, the block size can be defined. In this case, the value “1M” says that the block size is 1 megabyte.

“count” decides the number of blocks to be written. If not fixed, “dd” will continue the writing process unless the input stream ends. In this case, “/dev/urandom” will continue generating data infinitely, so this option was paramount in this example.

Data backup

Using this method, “dd” can be used to dump the data of an entire drive! All you need is tell the drive as the source.

dd if=<source> of=<backup_location>

If you’re going for such actions, make sure that your source isn’t a directory. “dd” has no idea how to process a directory, so things won’t work.

“dd” only knows how to work with files. So, if you’re in need of backing up a directory, use tar to archive it first, then use “dd” to transfer it on a file.

tar cvJf demo.tar.xz DemoDir/

dd if=demo.tar.xz of=~/Desktop/backup.img

 

In the next example, we’ll be performing a very sensitive operation: backing up the MBR! Now, if your system is using MBR (Master Boot Record), then it’s located at the first 512 bytes of the system disk: 466 bytes for the bootloader, others for the partition table.

Run this command for backing up the MBR record.

dd if=/dev/sda of=~/Desktop/mbr.img bs=512 count=1

Data restore

For any backup, the way of restoring the data is necessary. In the case of “dd”, the restoring process is a bit different than any other tools. You have to re-write the backup file on a similar folder/partition/device.

For example, I have this “backup.img” file containing the “demo.tar.xz” file. To extract it, I used the following command.

dd if=backup.img of=demo.tar.xz

Again, make sure that you’re writing the output to a file. “dd” is no good with directories, remember?

Similarly, if “dd” was used to create a backup of a partition, then restoring it would require the following command.

dd if=<backup_file> of=<target_device>

For example, how about restoring the MBR we backed up earlier?

dd if=mbr.img of=/dev/sda

“dd” options

At some point in this guide, you faced some “dd” options like “bs” and “count”, right? Well, there are more of them. Here’s a shortlist on what they are and how to use them.

  • obs: Determines the size of data to be written at a time. The default value is 512 bytes.

  • cbs: Determines the size of data to be converted at a time.

  • ibs: Determines the size of data to be read at a time.
  • count: Copy only N blocks

  • seek: Skip N blocks at the start of the output

  • skip: Skip N blocks at the start of the input

conv=ascii: Converts the file input from EBCDIC to ASCII

conv=ebcdic: Converts the file input from ASCII to EBCDIC

conv=ibm: converts the file input from ASCII to alternate EBCDIC

conv=lcase: converts file input from Uppercase to lowercase

conv=ucase: converts file input from Lowercase to uppercase

conv=swab: Swap every input pair

Additional options:

  • nocreat: Don’t create the output file
  • notruc: Don’t truncate the output file
  • noerror: Continue the operation, even after facing error
  • fdatasync: Write data to the physical storage before the process finishes
  • fsync: Similar to fdatasync, but also writes the metadata
  • iflag: Tweak the operation based on various flags. Available flags include: append to Append data to the output

Additional options:

  • directory: Facing a directory will fail the operation
  • dsync: Synchronized I/O for data
  • sync: Similar to dsync but includes metadata
  • nocache: Requests for dropping cache.
  • nofollow: Don’t follow any symlink

Additional Options:

  • count_bytes: Similar to “count=N”
  • seek_bytes: Similar to “seek=N”
  • skip_bytes: Similar to “skip=N”

As you’ve seen, it’s possible to stack multiple flags and options in a single “dd” command to tweak the operation behavior.

dd if=demo.txt of=demo1.txt bs=10 count=100 conv=ebcdic
iflag=append,nocache,nofollow,sync

Final thoughts

The workflow of “dd” is pretty simple. However, for “dd” to truly shine, it’s up to you. There are tons of ways creative ways “dd” can be used to perform clever interactions.

For in-depth information about “dd” and all its options, consult the man and info page.

man dd
]]>
Soft (Symbolic) Link vs Hard Link https://linuxhint.com/soft_link_vs_hard_link/ Thu, 16 May 2019 12:27:18 +0000 https://linuxhint.com/?p=40365 Symbolic links (also called soft links) and Hard Links are a resource to access files or directories from any location. This article will describe the differences between symbolic links and hard links.

Hard Links

Hard links are not a file containing the path to the original file but mirror copies of the original file they point to. A file and it’s hard links aren’t associated by the name or path but by the inode which stores information on the file, like it’s location, creation date, permissions and other attributes. Each inode number is unique within a filesystem preventing hard links from working between different partitions or systems. Hard links can’t be used to link directories.

In contrast to soft links, hard links contain the information they link to so if the original file is removed you can still access it’s data.

In all Linux systems both hard and symbolic links are created with the command “ln”. To get started first create a file called LinuxHintFile and a directory called LinuxHintDirectory as shown in the image below:

touch LinuxHintFile
rmkdir LinuxHintDirectory
ls
root@LinuxHint:/home/linuxhint#

To create a hard link called Link2LHFile  to the file LinuxHintFile run in the terminal:

ln LinuxHintFile Link2LHFile

After checking with ls we see the hard link we just created, if you type ls -i you’ll see the files’ inodes, you can confirm the link we just created has the same inode number than the original file, type:

ls -i

As we said above, we can’t use hard links for directories nor links to other volumes or filesystems, we’ll link LinuxHintDirectory with a soft link.

You can find hard links within the working directory by running:

find -type f -links +1

You can also find files by inodes, for this run:

find . -inum <InodeNumber>

As you see it found both the original file and the hard link sharing the same inode number.

 

Soft Links

In contrast to hard links, soft links are not copies of the original file, they contain the path to the original file, because of this if the original file is removed the soft link or symbolic link will point to no file becoming a broken link, or an orphaned link, which means if you loss the source file, if you delete or move it the symbolic link will loss access to the information, while with the hard link the information remains despite the source file removal because it is a full and exact copy of that file.

Also in contrast to hard links symbolic links don’t share the same inode with the original file, that’s why symbolic link can cross volumes and filesystems while hard links can’t. Symbolic links can be used to link directories while with hard links that’s not possible.

Symbolic links are also created with the program ln, but for the link to be symbolic/soft we need to specify the parameter -s, to create a symbolic link to the directory LinuxHintDirectory called LHDLink run:

ln -s LinuxHintDirectory LHDLink

And then run ‘ls -i’ to check it was properly created:

As we see in this case the symbolic link was created, but it has a different inode number than the original file.

We can list symbolic links within a directory with the command “ls -l

In the image above we can see in the permissions of the first file the prefix “l” for symbolic links, the end of the line also shows to what file the symbolic link points.

I hope you found this article useful to understand differences between hard links and symbolic links and how to create them. Keep following LinuxHint for more tips and updates on Linux.

]]>