Find and remove files

I regularly use the find command in scripts to clean directories on my servers.
The common way to use find to do this is to write something like:

find ./my_dir -name 'cache-*' -exec rm -rf \{\} \;

It works fine but it always outputs messages like this:

find: ./mydir/cache-001: No such file or director

which can be annoying when ran in a crontab.

I just found recently that using the option ‘-delete’ with find just fix this problem.

find ./my_dir -name 'cache-*' -delete

 

 

This entry was written by CharlyBr, posted on February 4, 2010 at 11:36 am, filed under Command line and tagged . Leave a comment or view the discussion at the permalink.

Quickly check hosts with ping

I needed a script for a quick health check of a bunch of servers.
This is how I did it using the ping command:

for((i=1;i<42;i++)); do
    ping -c 1 -W 3 host${i}.domain.com &> /dev/null
    if [ $? -ne 0 ] ; then
        echo "host${i} is down"
    else
        echo "host${i} is up"
    fi
done

You can also use netcat and check a specific port:

netcat -z -w 2  host${i}.domain.com 80 &> /dev/null

This entry was written by CharlyBr, posted on August 14, 2009 at 1:46 pm, filed under Command line and tagged , , . Leave a comment or view the discussion at the permalink.

Set Terminal tab title in Mac OS X

Picture 2

I wrote previously a how-to to set your iTerm tab title.

I finally found a tool to do the same thing with the default Mac OS X Terminal.

Check it out here, it works perfectly for me!

This entry was written by CharlyBr, posted on August 11, 2009 at 2:12 pm, filed under Command line and tagged , . Leave a comment or view the discussion at the permalink.

Finding the total size of a set of files with awk

If you need to sum the total size of files in a directory or matching a pattern, an easy solution is to use awk.

I needed to calculate this total for a set of javascript files, I used this command line:

$ find App/ -name '*.js' -exec ls -l \{\} \; | awk '{sum+=$5} END {print sum}'
1929403

For a human readable result, you can divide your result and use printf to format it:

$ find App/ -name '*.js' -exec ls -l \{\} \; | awk '{sum+=$5} END {printf("%.2fM\n", sum/1024/1024)}'
1.84M

This entry was written by CharlyBr, posted on April 17, 2009 at 11:12 am, filed under Command line and tagged . Leave a comment or view the discussion at the permalink.

Memory usage by group of processes

While monitoring a http/php server, I needed to do some statistics about php-cgi memory usage.

Playing with memory_limit in PHP, we wanted to know the average memory usage per php-cgi process. This is easily calculated with our best friend awk.

First, get the number of php running processes:

# ps aux | grep php-cgi | grep -v grep | wc -l
126

Then, use awk to calculate the average memory usage for these processes:

# ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s/126;}'
33987.8

The number used in the calculation is the field RSS given by ps. The ps manual page says:

rss: resident set size, the non-swapped physical memory that a task has used (in kiloBytes)

You can also calculate the total memory used by all php-cgi processes:

# ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s;}'
4302028

If you need to watch the trend of this average memory usage, a little shell loop does the trick:

# while [ 1 ]; do ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s/126;}'; sleep 2; done
34401.3
34405.1
34408.4
34409.4
34414.2
34417

This entry was written by CharlyBr, posted on March 13, 2009 at 4:14 pm, filed under Benchmarks, Command line, Monitoring and tagged , , , . Leave a comment or view the discussion at the permalink.

The unix touch command

This post is a quick ref on the linux touch command. All the examples have been tested on Linux.

This command is used to update the access and modification times of files.

  • touch’s syntax
touch [option] file_name(s)
touch file1 file2 file3
  • Here some examples:
# touch /tmp/file
# ls -l /tmp/file
rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:13 /tmp/file
  • Update access and modification time to current time:
# ls -l /tmp/file
rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:13 /tmp/file
# touch /tmp/file
# ls -l /tmp/file
rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:14 /tmp/file
  • Update access and modification time to a specified timestamp ([[CC]YY]MMDDhhmm[.ss] format):
# touch -t 09091842 /tmp/file
# ls -l /tmp/file
-rw-r--r-- 1 charlybr charlybr 0 Sep  9 18:42 /tmp/file
  • Update access and modification time to a specified date
# touch -d '9 Sep' /tmp/file
# ls -l /tmp/file
-rw-r--r-- 1 charlybr charlybr 0 Sep  9 00:00 /tmp/file
# touch -d '9 Sep 2008 13:14' /tmp/file
# ls -l /tmp/file
-rw-r--r-- 1 charlybr charlybr 0 Sep  9 13:14 /tmp/file

Links

This entry was written by CharlyBr, posted on September 11, 2008 at 7:30 am, filed under Command line and tagged , . Leave a comment or view the discussion at the permalink.

Retrieve file from svn

Sometimes you need to retrieve a single file from SVN without doing a checkout on the repository.

SVN provides the svn cat to output the content of a file. You can redirect the output to get the file as :

svn cat https://svn.mydomain.com/project/folder/file.ext > file.ext

You can also use a simple shell script to do the work :

$ cat svnget
#!/bin/sh

if [ -z "$1" ]; then
        echo
        echo "Usage : $0 <SVN-URL>"
        echo
        exit
fi

fn=`basename $1`
svn cat $1 > $fn

This entry was written by CharlyBr, posted on May 23, 2008 at 7:51 am, filed under Command line and tagged . Leave a comment or view the discussion at the permalink.

Automatically set title on iTerm tabs

If like me you use iTerm for your terminal sessions, this is the tips to dynamically set the tab title.

As I’m using the bash shell, the tab title can be automatically set with the PROMPT_COMMAND variable. You can set this variable in /etc/profile or in your .bashrc

Mine is like this :

export PROMPT_COMMAND=’echo -ne “\033]0;${USER}@${HOSTNAME%%.*}\007″‘

The content of the variable is executed each time bash prints your prompt. Here echo is executed with the escape sequence to set the term title.

More informations

Some readings about this subject :

This entry was written by CharlyBr, posted on May 7, 2008 at 2:10 pm, filed under Command line and tagged , . Leave a comment or view the discussion at the permalink.

Command line history

As read here, this is my command line history on my MacBook :

$ history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

240 ssh
62 cd
55 scp
33 for
28 vim
25 ping
17 history
13 ls
4 sudo
3 telnet

This entry was written by CharlyBr, posted on May 2, 2008 at 5:54 pm, filed under Command line and tagged . Leave a comment or view the discussion at the permalink.