In Bash, you can get a list of files filtered by a range or choice of characters in the filename by using character expansion. For example, to get a listing of just log files in /var/log ending in .1, .2, .3 and .4, use this command:
$cd /var/log
$ls -l *.[1-4]
Or to just see the files ending in .1 and .4, use:
$ls -l *.{1,4}
From the Mac OS X Terminal application, you can open the Finder to any directory you wish by typing:
$open <directory name here>
For example, to open up the current directory in the Finder, type:
$open .
or the apps directory:
$open /Applications
$ps axww
The a option displays all process information other than your own.
The x option displays process information for those with no terminal; in other words, daemons and such.
The ww option tells ps to display all process information regardless of the window size. Info will be wrapped to the next line and not cut off like normal.
Ps is useful for getting information about what and how processes are running on your system. But normally, ps truncates each line at the width of the terminal window. The ww option prevents the truncation; very useful for when you know a certain string exists in the process line and you want to pass it to grep.
My neighbors have banded together to implement a traffic calming program on our streets. Read the about the goals and progress of our efforts on the Old Greenwich Traffic Calming site.
Sometimes you need to know what services are listening to what ports on your unix box. Two utilities that can get you some of this port and process information are netstat and lsof.
With netstat, you can find out which services are listening under which ports:
# netstat -na
The n option prevents ip to hostname lookups.
The a option adds to the output the sockets used by server processes.
Part of the output of the above netstat command will look like this (foreign ips have been x'd out to protect the innocent) :
$ netstat -na
Active Internet connections (including servers)
Bash supports arithmetic but it does not support floating point arithmetic.
For example
bash$ echo $((1 + 1))
2
bash $ echo $((1 + 1.1))
-bash: 1 + 1.1: syntax error in expression (error token is ".1")
Bash will treat the floating point number as a string.
However, the utility bc does handle floating point arithmetic and can
be called from within bash scripts.
Here we call it from the command line:
bash$ echo "1+1" | bc
2
bash$ echo "1+1.1" | bc
2.1
When using a unix terminal, sometimes you make typos and end up creating files starting with a dash "-"
For example, instead of typing
$touch ./h-ello.txt
if you ended up typing
$touch ./-hello.txt
this would create a file named -hello.txt
Trying to delete a file with a name starting with a dash will cause an error in the bash shell:
$rm -hello.txt
rm: invalid option -- h
Try `rm --help' for more information.
To get around the dash in the filename, use a double-dash "--" before the rm arguments to delete the file starting with a dash.
Logfiles can often contain information that is not immediately helpful while diagnosing a problem. For example, we may be only interested in finding what files are being currently requested via the apache log files. A typical log line would look like this:
1.2.3.4 - - [16/May/2007:07:49:50 -0700] "GET /index.php?main_page=product_info&manufacturers_id=6481&products_id=107321 HTTP/1.1" 200 5614 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
Counting the elements in this line separated by spaces, we identify that the elements of the log line are:
I've recently had problems with a webapp running in tomcat trying to use a stale jdbc connection via the apache commons database connection pools library (dbcp). I fixed this problem by adding a validation query to the configuration in the webapp's context.xml file. The documentation
for dbcp options is here:
http://jakarta.apache.org/commons/dbcp/configuration.html
The change involves adding the testOnBorrow parameter explicitely to the
configuration (although by default this is set to true). However, for the testOnBorrow setting to work, you have to specify a select sql query for dbcp to validate against the database before the connection is handed to the user code. In my case, I reused the basic select from the dbcp examples 'select 1' - use the validationQuery parameter to specify the sql query.