Linux: Counting the number of lines inside multiple files

Posted by & filed under BASH, Programming.

Recently I needed to recursively count the number of lines of code in each of the specific file types. In this instance I wanted to count the number of lines of code in my PHP files. The below command worked flawlessly. In addition to breaking down the line count in each directory, it gives a overall total at the end as well.

find . -name '*.php' | xargs wc -l

Pipe Viewer

Posted by & filed under BASH, Programming, Server Admin.

pv – Pipe Viewer – is a terminal-based tool for monitoring the progress of data through a pipeline. It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

Linux: Find files modified between dates

Posted by & filed under BASH, Linux.

I found a handy technique to find files modified between a specific date. In essence, we touch two temp files, setting the modified dates to the range we want to find:

touch temp -t 201104141130
touch ntemp -t 201104261630

Note the date is yyymmddtime.

Then we run the find command:

find /path/to/folder/ -cnewer temp -and ! -cnewer ntemp


Recursive directory search for string

Posted by & filed under BASH, Linux.

The following command will search for a string within files in a directory recursively.

sudo find . -type f -print0 | xargs -0 grep -n 'STRING TO SEARCH FOR'

Stream audio over SSH

Posted by & filed under BASH, Linux.

Ok this is sweet:

ssh “cat /Torrents/Complete/15\ years\ of\ Essential\ Mix\ Sasha\ \&\ Digweed/Essential\ Mix\ 137\ -\ Sasha\ -\ Live\ @\ radio\ 1\ on\ tour\ from\ Sanctuary\ -\ [30.06.1996].mp3” | mpg123 –

And here is a more advanced version to find multiple files (unworking)
ssh “find /Torrents/Complete -wholename \*Essential\*.mp3 -print0 | xargs -0 cat” | mpg123 –

this finds fines in /Torrents/Complete and pipes them to mpg123

Monitoring a Linux process by PID and sending a e-mail notification upon failure

Posted by & filed under BASH, Email, Linux, Programming.

One of my clients needs their vendor to be alerted when their Backup Exec service crashes. I wrote the following quick-n-dirty batch script intended to be ran as a cron job.

# Process Monitor
# Send e-mail alerts when service goes down
# -------------------------------------------------------------------------
# Author: Nathan Riley
# -------------------------------------------------------------------------
SUBJECT="Backup Exec Agent Failure"

#path to pgrep command

# Daemon name,

# find daemon pid

if [ $? -ne 0 ] # if daemon not running
# Generate email message body
echo "This is servername at location. The Backup Exec service is no longer running." > $EMAILMESSAGE
# send email alert
/usr/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

And the cron line would be something like this:

*/5 * * * * root /root/  >/dev/null 2>&1