Speed Up Sysadmin Tasks: Fast File Deletion, iSCSI Detection, and Group Management
This article shares practical Linux and vSphere techniques—including using rsync for rapid bulk deletions, scanning SCSI devices without reboot, safeguarding rm with shell parameter expansion, mounting remote filesystems via sshfs, and managing user groups with gpasswd—to boost everyday operations efficiency.
Requirements
During daily operations we often encounter scenarios such as quickly deleting large numbers of files, detecting newly added iSCSI disks in vSphere without a reboot, preventing accidental rm deletions, mounting filesystems across Linux servers rapidly, and adding users to secondary groups efficiently.
How to quickly delete massive files on Linux?
How to make vSphere recognize a newly added iSCSI disk without restarting?
How to protect rm from accidental deletions?
How to mount between different Linux servers quickly?
How to add a user to supplementary groups swiftly?
1. How to quickly delete massive files on Linux?
Using
rmon many small files or large files can be slow and may leave disk space unreleased because each entry is unlinked individually. Replacing
rmwith
rsyncdramatically speeds up the process by swapping the directory with an empty one.
Key rsync delete options:
<code>rsync --help | grep delete
--del an alias for --delete-during
--delete delete files that don't exist on the sending side
--delete-before receiver deletes before transfer (default)
--delete-during receiver deletes during transfer, not before
--delete-after receiver deletes after transfer, not before
--delete-excluded also delete excluded files on the receiving side
--ignore-errors delete even if there are I/O errors
--max-delete=NUM don't delete more than NUM files</code>Fast directory deletion
<code>1. mkdir -p /del_tmp
2. Target directory to clear: /del_dest
3. rsync --delete-before -a -H -v --progress --stats ./del_tmp/ ./del_dest/</code>Fast file deletion
Note: When source and destination types differ, errors occur; when both are files (f) the command empties the file content instead of deleting; when both are directories (d) it removes all files inside, leaving an empty directory.
2. Detecting a newly added iSCSI disk in vSphere without reboot
Two methods are provided.
Method 1
Write a command to
/proc/scsi/scsito add the new device (assuming it appears as “Direct‑Access”):
<code>echo 'scsi add-single-device 2 0 1 0' > /proc/scsi/scsi</code>Parameters:
HOST – host adapter ID (0 is first, new disk uses 2)
Channel – SCSI channel on the host (00 for both old and new)
ID – SCSI device ID, starts at 00; new disk is 01
Method 2
<code># Refresh SCSI; repeat for each host
# If there are 3 hosts, run three times
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan</code>Either method allows the system to recognize the new disk without a reboot.
3. Preventing accidental rm deletions
Leverage Bash parameter expansion to supply a safe default when a variable is empty:
<code># Format: ${parameter:-default}
# Example command
rm -rf ${dest:-test}
# If $dest is unset or empty, the command becomes rm -rf test (safe).
# If $dest is set, it deletes the specified path.
# Improved safe version
rm -rf /${dest:-test}</code>4. Fast mounting between Linux servers
While NFS is common,
sshfsprovides a quick SSH‑based mount.
<code># Install SSHFS
yum install sshfs
# Create mount point
mkdir /mnt/data
# Mount remote directory
sshfs [email protected]:/home/test/ /mnt/data
# With key authentication
sshfs -o IdentityFile=~/.ssh/id_rsa [email protected]:/home/test/ /mnt/data</code>This method is simpler than configuring NFS.
5. Adding a user to supplementary groups efficiently
Using
usermodoverwrites existing groups. The
gpasswdcommand can add a user to a group without needing to know current memberships.
<code># Add test1 to test2
gpasswd -a test1 test2
# Add test1 to test3
gpasswd -a test1 test3</code>This is handy when a user must belong to multiple groups for shared file permissions.
Conclusion
Standard operations can solve most issues, but exploring alternative commands and tools can greatly improve efficiency and reduce manual effort in daily sysadmin work.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.