How to maintain Linux hard drives? Common commands for hard drive maintenance
In Linux systems, hard drives are the core components for storing all data and system files. This article will provide you with a detailed introduction to common commands and techniques for Linux hard drive maintenance, helping you better manage and maintain your hard drives.
Rendering...
In Linux systems, hard drives are the core components for storing all data and system files. Regular maintenance and monitoring of hard drives not only ensure stable system operation and prevent performance degradation due to insufficient space but also allow for timely detection of potential hardware issues, thereby effectively preventing data loss. This article will provide a detailed introduction to common commands and techniques for Linux hard drive maintenance, helping you better manage and maintain your hard drives.
## Introduction: The Importance of Hard Drive Maintenance
Hard drive maintenance is more than just cleaning up junk files; it also includes monitoring hard drive health, checking file system integrity, optimizing I/O performance, and more. A healthy hard drive is the foundation for efficient Linux system operation. Neglecting hard drive maintenance can lead to:
* **System Performance Degradation**: Insufficient hard drive space or excessive fragmentation can severely slow down the system.
* **Application Crashes**: Some applications require temporary space, and insufficient space can prevent them from functioning correctly.
* **Risk of Data Loss**: Hard drive failures often show warning signs; timely detection can prevent permanent data loss.
* **System Instability**: File system errors can cause system crashes or prevent it from booting.
## I. Viewing Basic Hard Drive Information
Before performing any maintenance operations, understanding the physical and logical structure of the hard drive is the first step.
### 1. Listing Block Device Information: `lsblk`
The `lsblk` command is used to list all available block devices (such as hard drives, partitions, USB drives, etc.) and their relationships.
```bash
lsblk
```
**Example Output:**
```
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 8G 0 part [SWAP]
└─sda3 8:3 0 191.9G 0 part /
sr0 11:0 1 1024M 0 rom
```
* **`NAME`**: Device name (e.g., `sda` represents the first SCSI hard drive, `sda1` represents the first partition of `sda`).
* **`SIZE`**: Size of the device or partition.
* **`TYPE`**: Device type (`disk` for hard drive, `part` for partition, `rom` for optical drive).
* **`MOUNTPOINTS`**: Mount point of the device.
### 2. Viewing Partition Information: `fdisk -l` or `parted -l`
These commands are used to view hard drive partition table information. `fdisk` is suitable for MBR and GPT partition tables, while `parted` is more powerful and supports more partition types.
```bash
sudo fdisk -l
# Or
sudo parted -l
```
**Tip:** These commands require root privileges to view all hard drive information.
### 3. Viewing Mounted File Systems: `mount`
When the `mount` command is used without any arguments, it displays all currently mounted file systems in the system. This is very useful for understanding which partitions are in use and their mount options.
```bash
mount
```
**Example Output:**
```
/dev/sda3 on / type ext4 (rw,relatime,errors=remount-ro)
/dev/sda1 on /boot type ext4 (rw,relatime)
/dev/sdb1 on /data type xfs (rw,relatime,attr2,inode64,noquota)
```
## II. Viewing Hard Drive Usage
Understanding the overall usage of the hard drive is the first step in maintenance.
### 1. Viewing Overall File System Usage: `df -h`
The `df` (disk free) command is used to display disk space usage of file systems. The `-h` option displays sizes in a human-readable format (e.g., G, M).
```bash
df -h
```
**Example Output:**
```
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 797M 1.8M 795M 1% /run
/dev/sda3 192G 85G 100G 46% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 976M 180M 729M 20% /boot
tmpfs 797M 12K 797M 1% /run/user/1000
```
* **`Filesystem`**: File system name.
* **`Size`**: Total size of the file system.
* **`Used`**: Space used.
* **`Avail`**: Available space.
* **`Use%`**: Percentage of space used.
* **`Mounted on`**: Mount point of the file system.
**Pay close attention to the `Use%` column. If the usage rate of a mount point is close to 100%, it needs to be addressed immediately.**
### 2. Viewing Inode Usage: `df -i`
In addition to disk space, file systems also manage inodes (index nodes). Each file or directory occupies an inode. If inodes are exhausted, new files cannot be created even if there is sufficient disk space.
```bash
df -i
```
**Example Output:**
```
Filesystem Inodes IUsed IFree IUse% Mounted on
udev 999550 427 999123 1% /dev
tmpfs 199887 706 199181 1% /run
/dev/sda3 12582912 378900 12204012 4% /
tmpfs 999550 1 999549 1% /dev/shm
tmpfs 199887 3 199884 1% /run/lock
/dev/sda1 256K 336 256K 1% /boot
tmpfs 199887 16 199871 1% /run/user/1000
```
**Also pay attention to `IUse%`. If it is too high, it may indicate the presence of a large number of small files.**
## III. Finding Large Files and Folders
When `df -h` shows that a partition is running out of space, the next step is to find out which files or folders are occupying a large amount of space.
### 1. Viewing the Size of Each Subdirectory in a Specified Directory: `du -h --max-depth=1`
The `du` (disk usage) command is used to estimate the disk space usage of files or directories. Combined with `-h` (human-readable) and `--max-depth=1` (display only first-level subdirectories), it can quickly pinpoint issues.
```bash
sudo du -h --max-depth=1 /path/to/directory
# Example: View the size of each first-level subdirectory in the root directory
sudo du -h --max-depth=1 /
```
**Example Output:**
```
4.0K /srv
16K /lost+found
4.0K /mnt
4.0K /cdrom
1.8G /opt
1.1G /home
8.0K /media
1.2G /root
4.0K /snap
1.4G /boot
1.3G /etc
1.6G /bin
2.0G /lib
1.7G /sbin
2.0G /dev
1.9G /run
2.0G /sys
1.9G /tmp
1.9G /proc
2.0G /usr
2.0G /var
2.0G /lib64
2.0G /initrd.img
2.0G /vmlinuz
192G /
```
Through this command, you can drill down layer by layer to find the directories that occupy the most space.
### 2. Finding the Largest Files/Directories in the Current Directory: `du -sh * | sort -rh`
This combined command lists the size of all files and subdirectories in the current directory and sorts them in descending order.
```bash
sudo du -sh * | sort -rh
```
* `du -sh *`: Estimates the total size of all files and subdirectories in the current directory and displays it in a human-readable format.
* `sort -rh`: The `sort` command is used for sorting. `-r` means reverse sort (descending order), and `-h` means sort by human-readable numbers.
**Example Output:**
```
85G var
1.1G home
1.0G usr
900M opt
500M root
...
```
### 3. Finding Files Larger Than a Specific Size: `find`
The `find` command is powerful and can find files based on various criteria. For example, to find all files larger than 1GB in the `/var` directory:
```bash
sudo find /var -type f -size +1G -print0 | xargs -0 du -h | sort -rh
```
* `find /var -type f -size +1G`: Finds files of type file (`-type f`) and size greater than 1GB (`-size +1G`) in the `/var` directory.
* `-print0`: Outputs with a null character as a delimiter, preventing issues caused by spaces or special characters in filenames.
* `xargs -0 du -h`: Takes the output of `find` as input to `du -h` to calculate the size of each file.
* `sort -rh`: Sorts again to display the largest files.
---
## IV. Monitoring Hard Drive I/O Performance
Hard drive I/O performance directly affects system responsiveness. Monitoring I/O can help you identify processes that are performing excessive disk read/write operations.
### 1. Real-time Monitoring of Process I/O: `iotop`
The `iotop` tool can display in real-time which processes are performing heavy disk read/write operations, similar to how the `top` command monitors CPU and memory.
```bash
sudo iotop
```
**Note:** `iotop` usually needs to be installed separately.
* Debian/Ubuntu: `sudo apt install iotop`
* CentOS/RHEL: `sudo yum install iotop` or `sudo dnf install iotop`
### 2. Statistics I/O: `iostat`
The `iostat` command (part of the `sysstat` package) is used to report CPU statistics and device I/O statistics.
```bash
sudo iostat -x 1 5
```
* `-x`: Displays extended statistics.
* `1 5`: Refreshes every 1 second, displaying 5 times in total.
**Example Output (partial):**
```
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
```
* `r/s`, `w/s`: Read/write requests per second.
* `rkB/s`, `wkB/s`: Read/write data per second (KB).
* `%util`: Device utilization. If it is close to 100%, it may indicate an I/O bottleneck.
---
## V. Cleaning Up Hard Drive Space
Cleaning up hard drive space is an important part of daily maintenance.
### 1. Cleaning Package Manager Cache
Package managers (like APT, YUM, DNF) download package installation files to a local cache when installing software. These cached files can occupy a significant amount of space.
* **Debian/Ubuntu (APT):**
```bash
sudo apt clean # Cleans all downloaded package files
sudo apt autoclean # Cleans package files that are no longer needed
```
* **CentOS/RHEL (YUM/DNF):**
```bash
sudo yum clean all # Cleans all cache files
# Or for DNF
sudo dnf clean all # Cleans all cache files
```
### 2. Cleaning Log Files
System and application log files are usually stored in the `/var/log` directory and can become very large after prolonged operation.
* **Understanding `logrotate`**: Linux systems typically use the `logrotate` tool to automatically manage log files, performing rotation, compression, and deletion periodically. You can check the configuration files in `/etc/logrotate.conf` and `/etc/logrotate.d/`.
* **Manually Cleaning Old Logs**: If `logrotate` is not configured properly or logs grow too quickly, manual cleaning may be necessary.
**Warning:** Before deleting log files, ensure you understand their content and importance. Deleting log files that are currently being written to can cause problems.
```bash
# Find and delete all old compressed log files ending with .gz
sudo find /var/log -type f -name "*.gz" -delete
# Find and delete all old log files ending with .log.1
sudo find /var/log -type f -name "*.log.1" -delete
# Truncate a specific large log file (clears content without deleting the file itself)
sudo truncate -s 0 /var/log/syslog
```
### 3. Cleaning Temporary Files
The system and applications create temporary files, usually stored in the `/tmp` and `/var/tmp` directories.
* Files in the `/tmp` directory are typically cleared after a system reboot.
* Files in the `/var/tmp` directory are not cleared after a system reboot but are cleaned periodically.
* **Manual Cleaning:**
**Warning:** Be very careful when cleaning the `/tmp` directory, as running programs may be using files within it. It's best to perform this during a system maintenance window or after a reboot.
```bash
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
```
### 4. Cleaning Large Files in User Home Directories
User home directories (`/home/username`) are often major space consumers, especially for downloads, videos, and images. Using the `du` and `find` commands introduced earlier can help you locate and clean these files.
```bash
# Find files larger than 500MB in the current user's home directory
find ~/ -type f -size +500M -print0 | xargs -0 du -h | sort -rh
```
---
## VI. Checking Hard Drive Health Status
In addition to space and performance, the physical health of the hard drive is also crucial. S.M.A.R.T. (Self-Monitoring, Analysis and Reporting Technology) technology can help us monitor the health status of hard drives.
### 1. Using `smartctl` to Check S.M.A.R.T. Information
The `smartctl` command is part of the `smartmontools` package and is used to interact with S.M.A.R.T.-enabled hard drives.
**Installing `smartmontools`:**
* Debian/Ubuntu: `sudo apt install smartmontools`
* CentOS/RHEL: `sudo yum install smartmontools` or `sudo dnf install smartmontools`
**Checking Hard Drive Health Status:**
```bash
sudo smartctl -a /dev/sda
```
Replace `/dev/sda` with your hard drive device name (which can be found using `lsblk`).
**Example Output (key information):**
```
SMART overall-health self-assessment test result: PASSED
...
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 000 Pre-fail Always - 0
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 0
197 Current_Pending_Sector 0x0032 100 100 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0030 100 100 000 Old_age Always - 0
```
**Explanation of Key Metrics:**
* **`SMART overall-health self-assessment test result: PASSED`**: This is the most important metric, indicating that the hard drive has passed its self-test. If it shows `FAILED`, the hard drive may have serious problems.
* **`Reallocated_Sector_Ct` (ID 5)**: Reallocated Sector Count. When a hard drive detects bad sectors, it marks them and replaces them with spare sectors. If this value is non-zero and continuously increasing, it indicates that the hard drive is developing physical bad sectors.
* **`Current_Pending_Sector` (ID 197)**: Current Pending Sector Count. Indicates sectors that are marked as "unstable" and are awaiting reallocation. If this value is non-zero, it requires close attention.
* **`Offline_Uncorrectable` (ID 198)**: Offline Uncorrectable Sector Count. Indicates sectors that cannot be read or written and cannot be corrected by ECC. A non-zero value here usually means the hard drive already has unrecoverable bad sectors.
**If you find these key metrics to be abnormal, back up your data immediately and consider replacing the hard drive.**
## VII. Regular Maintenance and Backup
* **Regular Checks**: It is recommended to check hard drive usage and S.M.A.R.T. information at least once a month.
* **Data Backup**: Any hard drive can fail at any time. **Regularly backing up important data is the ultimate safeguard against data loss.** You can use commands like `rsync`, `tar`, or professional backup tools for backups.
* **File System Check**: For non-root partitions, you can check file system integrity using the `fsck` command after unmounting. For the root partition, it is usually checked automatically during system startup.Comments
Please login to view and post comments
Go to Login