Category Archives: Bash

How to Display Your “Uptime” using Bash

Bash is a powerful scripting tool and comes in handy on more than one occasion. Here is a fun one: Write your server uptime to a text file so that you can display it on a webpage.

#!/bin/sh

# get the uptime data
days=$(uptime | awk '{print $3}' | sed 's/,//g')
hours=$(uptime | awk '{print $5}' | sed 's/,//g')
label=$(uptime | awk '{print $4}')

if [ "$days" = 1 ]; then
day_label='day'
else
day_label='days'
fi

#format labels
if [ $hours = 1 ]; then
hour_label='hour'
else
hour_label='hours'
fi

#format output
if [ "$label" = 'mins,' ]; then
echo 'This server has been on for '$days minutes'' > /var/www/uptime.txt
elif [[ "$label" = 'day,' || "$label" = 'days,' ]]; then
echo 'This server has been on for '$days $day_label, $hours $hour_label'' > /var/www/uptime.txt
elif [ "$label" = '2' ]; then
echo 'This server has been on for '$days hours'' > /var/www/uptime.txt
fi

My Top 10 Ubuntu Aliases

aka: The Ultimate List of Ubuntu Aliases

Okay, maybe this isn’t the ultimate list, and maybe it’s more than 10, but this is my list of essential, must-have aliases for Ubuntu (or any version of Linux). Aliases are “shortcuts”. For example, if you’re at the command prompt and want to exit, you normally type exit. But I have an alias for the letter e to exit. I cut my typing by 75%!! Do this all throughout your workflow and you’ll seriously wonder how you ever lived without it. Backup your aliases.

By adding these aliases, I have turned 277 characters of typing down to just 35. That’s a reduction of over 87%!!! Finally, I can leave work at 10am. :)

If you want to use an alias in Ubuntu Linux, insert these commands (or any other commands that speed up your workflow) at the bottom of your .profile or .bashrc files:

    alias sp=’sudo pico’
    alias la=’ls -alh’
    alias m=’mutt’
    alias e=’exit’
    alias cdw=’cd /var/www/’

      alias cdb=’cd /var/www/brockangelo/’
      alias cda=’cd /var/www/angeloarchive/’ (if you host multiple websites, replace the “a” with a website identifier. aka, photoshopsamurai = ps)
      alias cdps=’cd /var/www/photoshopsamurai/’

    alias cdn=’cd /usr/local/nagios/etc/’
    alias nr=’sudo /etc/init.d/nagios restart’
    alias ar=’sudo /etc/init.d/apache2 restart’
    alias coffee=’cat > /var/www/brockangelo/coffees.txt’ (I used to have a coffee-counter on my website and I would update the number of coffees my espresso machine had made – you can use this to replace the contents of a text file quickly)
    alias s=’sudo’
    alias aa=’/home/brock/scripts/add_alias.sh’ (I created a shell script to add new aliases by just typing aa – leave a comment if you’d like to download the script)

And here is the code for the shell script:

    #!/bin/bash
    echo Enter the shortcut, or alias, you want to use:
    read SHORTTEXT
    echo Now enter what text you want it to replace:
    read LONGTEXT
    echo “alias $SHORTTEXT=’$LONGTEXT’” >> ~/.profile
    echo “alias $SHORTTEXT=’$LONGTEXT’ was added to your profile. Changes effective after logout/in.”

Delete Comment Spam using a Cron Job

Delete Comment Spam using a Cron Job

WordPress catches all of the comment spam, but it doesn’t seem to delete it often enough. If you have a high traffic site, this can become megabytes in your database. Bleh. I don’t have a high traffic site, but I sync my databases off-site everyday and spam wastes my bandwidth. So I delete all the comments marked as spam automatically each day using a scheduled cron job so that I never even have to see it. Could I possibly delete a real comment? Yes. Does the convenience of never even knowing about comment spam outweigh the risk? You bet.

Create a bash script in your scripts folder by typing this at the command prompt:

sudo pico /home/brockangelo/scripts/del_spam.sh

sudo: elevate privileges
pico: use your editor of choice
/home…/scripts/: wherever you keep all your bash scripts
del_spam.sh: name it something obvious

By the way, bash scripts are as easily made as windows batch files. You just put in the code and put a .sh at the end. Inside “del_spam.sh” add the following:

mysql -u username -ppassword -e "delete from wp_comments where comment_approved='spam';" brockangelo

Where “brockangelo” is the name of the database. Repeat this line for each WordPress site you host. Then you need to set this up as a cron job and schedule it for once a night. Or, you can download my plugin that deletes spam daily. Delete Spam Daily plugin.

By the way, the username and password format in that command you see above looks wrong, but it is typed correctly. You put “-u (space) username” then you squish the “-p” and “password” together like this “-u username -ppassword”.

Create Aliases in Ubuntu

How to Create an Alias in Ubuntu

If you run the same commands in Linux (or Ubuntu) all the time, you can add what is called an “alias” to your user account. For example, I regularly type:

ls -alh

So I setup an alias so that I can just type la. (also, see my Top 10 Aliases list) To do this, simply edit your “profile”. Bash is the program that you typically use when you are in a command shell, so it may be called your “bash profile”. Your profile is in your Home folder and stores all of your preferences. Just edit the “.profile” or the “.bash_profile” if it exists.

cd ~
pico edit .profile

Add the following code to the bottom:

alias la='ls -alh'

Now when you are typing at the command prompt, you just type la and it acts like you typed ls -alh. Pretty handy.