Linux: Shell Script for Removing Duplicate Files

So when it comes to removing files, this little script is the berries – it finds your duplicate files and proceeds to build a shell script to remove them all, called rem-duplicates.sh.

In here you will find all the lines to remove the duplicates commented out with a “#” – you then uncomment the ones to be removed, save and run the script. Simples!

#!/bin/bash
OUTF=rem-duplicates.sh;
echo "#! /bin/sh" > $OUTF;
find "$@" -type f -printf "%s\n" | sort -n | uniq -d | xargs -I@@ -n1 find "$@" -type f -size @@c -exec md5sum {} \; |
sort --key=1,32 | uniq -w 32 -d --all-repeated=separate | sed -r 's/^[0-9a-f]*( )*//;s/([^a-zA-Z0-9./_-])/\\\1/g;s/(.+)/#rm \1/' >> $OUTF;
chmod a+x $OUTF;
ls -l $OUTF

All credit here: Unix shell script for removing duplicate files.

Linux: Add a New HDD to a VM Without Rebooting

Add a New HDD to a VM Without Rebooting

This article describes how to add a new HDD to a VM without rebooting – handy if you need consistency of service and don’t have a redundant solution.

 

Solution:

Add a hard disk as usual through the VM settings. Check dmesg to see if the kernel has detected it automatically, if not:

echo "- - -" > /sys/class/scsi_host/host0/scan
fdisk -l

You should see something like:

Disk identifier: 0x00004fff

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    30076927    15037440   83  Linux
/dev/sda2        30078974    31455231      688129    5  Extended
/dev/sda5        30078976    31455231      688128   82  Linux swap / Solaris

Disk /dev/sdc: 429.5 GB, 429496729600 bytes
255 heads, 63 sectors/track, 52216 cylinders, total 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdc doesn't contain a valid partition table

/dev/sdc is the disk you just added, all you need to do is “fdisk /dev/sdc” to partition it and “mkfs.ext3 /dev/sdc1” to format the partition.

Job done.

Linux: Kill a Process from the Command Line Without Knowing the PID

So we have a script – myscript.sh – which is running on a linux box and we want to kill it quickly with the minimum of fuss:

root@linux# kill $(pgrep <script_name>)

Here is the explanation:

We use “pgrep” to grep the running process list and returns a process’s PID :

[root@linux]# pgrep myscript.sh
20104

And combine it with the kill command to make a one-liner:

[root@linux]# kill $(pgrep myscript.sh)
[root@linux]#

More info on pgrep and kill on the man pages:
http://linux.die.net/man/1/pgrep

http://linux.die.net/man/2/kill