Archive for the ‘Command line’ Category

 

CharlyBr on Feb 4th, 2010Find 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 [...]

CharlyBr on Aug 14th, 2009Quickly 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} [...]

CharlyBr on Aug 11th, 2009Set Terminal tab title in Mac OS X

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!

CharlyBr on Apr 17th, 2009Finding 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 [...]

CharlyBr on Mar 13th, 2009Memory 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 [...]

CharlyBr on Sep 11th, 2008The 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 [...]

CharlyBr on May 23rd, 2008Retrieve 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 :
$ [...]

CharlyBr on May 7th, 2008Automatically 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 [...]

CharlyBr on May 2nd, 2008Command 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