Category Archives: Linux

Ubuntu Server – Delete empty directories except parent directory

I needed a bash script to delete empty directories, excluding the parent directory. The following does this:

find /home/alex/target mindepth 1 -maxdepth 1 -type d -empty -print0 | xargs -0 rm -R

And the crontab that does this every few hours

0 */12 1-31/2 * * find /home/alex/target -mindepth 1 -maxdepth 1 -type d -empty -print0 | xargs -0 rm -R >/dev/null 2>&1

Ubuntu Server and Rsync – SSH into remote server and download latest files except files with a .part extension and email output

After reading through a bunch of troubleshooting threads, the following:

  1. Runs a shell  script via cron
  2. SSH into a remote server using a SSH key for a password
  3. Executes Rysnc with the options to delete source files after download, and skip files with the extension .part

Bash Script:


/usr/bin/rsync -havzPe 'ssh -i /home/alex/.ssh/id_rsa' --rsync path='/usr/bin/rsync' --remove-source-files --exclude='*.part' --stats bob@ /home/alex | /usr/bin/mail -s 'Rsync Output' root

Link Explain Shell

Crontab consisted of the following:

0 */2 * * * /home/alex/ >/dev/null 2>&1

This runs the shell script every 2 hours.  This was created using a this crontab generator.

Generating a SSH key for passwordless SSH login and exporting it remotely is explained here.

Generate the keys on your host server:

ssh-keygen -t dsa

Export the keys to the target server:

ssh-copy-id -i ~/.ssh/ ross@remotehost [or enter ip address instead of hostname, e.g."remotehost"]

Configure email server so mail command works.

Digital Ocean’s Guide to PostFix


Ubuntu Server – Adding a new Hard Disk

First find the drive mounting point, usually located in /dev. In this example, /dev/sdd is the new disk.

sudo fdisk -l

Create a new partition on the drive

sudo fdisk /dev/sdd

Once in fdisk, press the following

  • d – to delete any partitions you want removed
  • n – create a new partition
  • defaults for partition number, start and end sectors on disk
  • w – write partition changes

Next format the disk

sudo mkfs /dev/sdd1 -t ext4

Create a mount point for the disk

cd /mnt
sudo mkdir sdd1
sudo chmod 777 sdd1
sudo chown  currentuser -R sdd1

Mount the disk

sudo mount /dev/sdd1 /mnt/sdd1 -t ext4

Edit fstab file

sudo vi /etc/fstab

add the line

/dev/sdd1 /mnt/sdd1 ext4 defaults 0 0

save the file and then

sudo mount -a







Installing and Configuring Monit on Ubuntu Server

Run the following to install:

sudo apt-get install monit

To configure, open the file

sudo nano /etc/monit/monitrc

I added the following to the file to monitor the user and system CPU usage. Not sure if this will work yet.

check system localhost    
    start program = "/opt/zimbra/bin/zmcontrol start"
        as uid zimbra and gid zimbra
    stop program = "/opt/zimbra/bin/zmcontrol stop"
        as uid zimbra and gid zimbra            
    if cpu usage (system) > 99% for 5 cycles then restart    
    if cpu usage (user) > 99% for 5 cycles then restart

Setup the Http server so you can check status remotely

 set httpd port 2812
    use address ipaddress
    allow user:pw

If you have csf installed, make sure you update your csf config file to open up port 2812

vi /etc/csf/csf.conf

and then to reload csf

csf -r

Reload monit

monit reload

Check monit status

monit status



Ubuntu – Archiving a directory and Encrypting the File using 7zip and OpenSSL

First archive the directory using 7zip.

7z a filename.7z targetdirectory

Next, encrypt the file using OpenSSL. Executing the command will ask the user for a password

openssl aes-256-cbc -in filename.7z -out file.enc

You can decrypt the file using the following command. Executing the command will ask the user for a password.

openssl aes-256-cbc -d -in file.enc -out filename.7z


Ubuntu Change Boot Order for a Service

Use the update-rc.d command.

First remove the link fron the /etc/init.d directory to the rc.d directories

sudo update-rc.d -f my_old_script remove

Then change the boot order. The following updates the scripts boot order to 98.

sudo update-rc.d my_script defaults 98

This command can also be used to remove or add a service during boot.