Unix Tips

anim-bulbglow.gif (80393 bytes)

Acacia is happy to provide programming tips. Help others by adding your own tips to this page.

The tips shown here are for information only and should be tested thoroughly before use in any production environment.

Last updated 18:07:39-20/12/2008

Click here to add your own tips.

Click to search the Acacia site
Click to search using Google

Unix Tips

Perl (Practical Extraction and Reporting Language) is a useful Unix and Windows programming language that is suited well to data migrations and text processing.

Tip Details
ADSM - Restore a file
To restore a file from ADSM backup:
dsmc res -pick -subdir=yes "/usr/home/*"
Analyse Core Dumps
To debug a core dump -

gdb application_name core.<PID>
thread apply all (shows all thread activity)
thread apply all where (shows where it failed)

Automated file clean up.
Use the following command in your crontab file to automatically cleanup files older than 7 days.

find /var/spool/mylogfiles -mtime +7 -exec rm {} \;
Automatic proxy config for Internet Explorer
To set up an automatic configuration script on your network, create a file called proxy.pac in the root of you web server containing the following:

function FindProxyForURL(url, host)
if ((dnsDomainLevels(host) == 0) ||
dnsFomainIs(host, ".mydomain.com.au") ||
isInNet(host, "", "") ||
shExpMatch(host, "192.16.60.*"))
return "DIRECT";
return "PROXY";

Now just check the "Use automatic configuration script" and provide the URL of the above file.
Backspace Key Problems
To fix your terminal backspace key:
1. stty erase [space] [backspace key] , or
2. stty erase [space] [CTRL v] [space] [CRTL h]

Note that the [CRTL v] sequence is used to specify that the next character
is a control key, in this case [CRTL h] or backspace.  Often used in vi and
at the shell prompt.
Build a Linux system
Create boot floppy for installation
mount /dev/cdrom
cd /mnt/cdrom/images
dd if=boot.img of=/dev/fd0 bs=1440k
Reboot to install Redhat
Login as root
cd /etc
mv securetty securetty.save
vi hosts.allow
vi hosts.deny
chkconfig iptables off
chkconfig sshd off
chkconfig lpd off
vi /etc/xinetd.d/telnet
disable = no
ps -ef | grep xinet
kill -hup xinet
vi /etc/aliases
root: root@zues.mydomain.com.au
Burn CD from ISO Image
Copy your ISO image into a location such as /data5/linux

# If you want to check your image, you can
# mount it as follows (optional)
# Create a directory to mount the image
mkdir /mnt/iso

# Mount the image (not req for burn)
mount -t iso9660 -o loop /data5/linux/yarrow-i386-disc1.iso /mnt/iso

# Find the ID of your burner for the cdrecord parameter
cdrecord -scanbus

# Burn the iso image
cdrecord -v -eject speed=6 dev=0,0,0 /data5/linux/yarrow-i386-disc1.iso

# Sit back and wait

Capturing shell output from a script
exec > operations.log 2>&1
echo "Hi there"

All output to stdout and stderr (2) is captured to the log file without any other redirection commands.
cd ~
Change to your home directory.
cd -
Change to your previous directory
Check for unresolved dynamic library
Use the following command to check shared object libraries used by an application.  Any problems will be reported in the output such as missing library files etc.

ldd a.out
Check Running Process Operations
Get the process ID <PID> using:
ps -ef | grep processname

Then list what the process is doing except seek operations using:
truss -t!seek <PID>
Clean up old files
find /var/spool/ -name \*.log -mtime +7 -exec rm {} \;
C++ Makefile Example
Geoff Collett
CFLAGS= -I. -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/pub
lic -I$(ORACLE_HOME)/network/public -I/opt/SUNW/spro/WS6/include/CC -I./

LDFLAGS= -L./ -L$(ORACLE_HOME)/lib/ -L/usr/ccs/lib -L/usr/lib -lclntsh $(ORACLE_HOME)/lib/scorept.o $(ORACLE_HOME)/lib/sscoreed.
o $(ORACLE_HOME)/rdbms/lib/kpudfo.o -lclntsh $(ORACLE_HOME)/lib/nautab.o $(ORACLE_HOME)/lib/naeet.o $(ORACLE_HOME)/lib/naect.o $
(ORACLE_HOME)/lib/naedhs.o -lmm -L./ -lsocket -lnsl

main: testsvr.cpp objapi.h
CC -w -o testsvr $(LDFLAGS) $(OBJS) $(CFLAGS) testsvr.cpp libobjapi.a
CFLAGS= -I. -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/pub
lic -I$(ORACLE_HOME)/network/public -I/opt/SUNW/spro/WS6/include/CC -I./

LDFLAGS= -L./ -L$(ORACLE_HOME)/lib/ -L/usr/ccs/lib -L/usr/lib -lclntsh $(ORACLE_HOME)/lib/scorept.o $(ORACLE_HOME)/lib/sscoreed.
o $(ORACLE_HOME)/rdbms/lib/kpudfo.o -lclntsh $(ORACLE_HOME)/lib/nautab.o $(ORACLE_HOME)/lib/naeet.o $(ORACLE_HOME)/lib/naect.o $
(ORACLE_HOME)/lib/naedhs.o -lmm -L./ -lsocket -lnsl

main: testsvr.cpp objapi.h
CC -w -o testsvr $(LDFLAGS) $(OBJS) $(CFLAGS) testsvr.cpp libobjapi.a
Configure X Windows on Fedora 4
Use the configuration tool.  Note that it has a bug for dual head configurations.

Hit Ctrl-Alt-F1 (Ctrl-Alt-F7 to switch back to X) to display a console and login to that.
Edit /etc/X11/xorg.conf

Restart X using:
init 3
init 5

Note for dual AGP cards, you need to type
and note the first column showing the address of each card.
Add a line such as the following to the "Device" section of the xorg.conf file.
BusID "2:0:0"

If all else fails, view the log file at /var/log/Xorg.0.log

Control core dump location on Solaris
Try these alternatives:

coreadm –i /corefilesystem/obj/%n.%f.%p" .
coreadm –e process
vi /etc/coreadm.conf
coreadm -p /corefilesystem/obj/%n.%f.%p $$
Core dump info
file core.1234

This will tell you the name of the executable that dumped core.

strings core.1234

This will show readable text in the core dump
Count file handles used by a process
/usr/proc/bin/pfiles [PID] | grep "ino:" | wc -l
Counting open file handles
/usr/proc/bin/pfiles <PID> | grep "ino:" | wc -l

Counts the number of open file handles for the <PID>.  Note that this routine may only display correct results up to 2048 file handles.
Count TCP connections from a PC
netstat -an | \
grep 80 | \
awk '{print $2;}' | \
sort | \
uniq -c | \
sort | \
Create a file system
To create a linux file system, add the drive.  On the primary IDE controller, devices are /dev/hda then /dev/hdb.  The secondary IDE has /dev/hdc and /dev/hdd.

Use hdparm to check that the device was detected and dmesg | less to check that the drivers loaded.  You may need to add boot parameters to /etc/lilo.conf as follows:

append="hdc=idc-scsi" etc...

Create a partition using fdisk
Create a file system using mkfs -t ext3 /dev/hdx
Create a mount point using mkdir /mnt/bigdisk
Edit /etc/fstab to ensure that it mounts correctly using the following syntax:

/dev/hdx1 /mnt/bigdisk ext3 defaults 1 2

Check the file system using fsck /dev/hdx1
Use a line in your crontab file such as

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

to run an application every 5 min and dump all stdout and stderr output (2) to the bit bucket.
CVS Add Directory
To import a whole directory into CVS:
cd [DIR]
cvs im a/b/c [TAGHEAD] [TAGNAME}
CVS - Add Single File
To add a single file to CVS:
cd [DIR]
cvs add [filename]
cvs commit
Debugging DNS
To debug problems with DNS (named daemon):
rndc -? for options
rndc status
rndc trace 9
tail -f /var/named/named.run
df -kd
Show the disk space used (k = in kB, d = same file system) in the current file system from the current directory downwards.
df -k
Show the disk space used and available on all file systems.
Discover Linux devices and settings
To find PCI devices
lspci (or cat /proc/pci)
To list all devices
To see USB devices and hubs
To see loaded device drivers
lsmod (or cat /proc/modules)
To see mounted drives
df -k (also cat /proc/mounts)
To see details of your CPU
cat /proc/cpuinfo
To see your current boot command line
cat /proc/cmdline
To list devices using DMA
cat /proc/dma
To list available devices
ls /proc/ide
To show configured interrupts
cat /proc/interrupts
To show configured device memory settings
cat /proc/iomem
To show configured device IO ports
cat /proc/ioports
To see the current load average
cat /proc/loadavg
To see overview of installed memory usage
cat /proc/meminfo
To see drive partitions
cat /proc/partitions
To see the current IO statistics
cat /proc/stat
To determine the swap device
cat /proc/swaps
To see tty drivers
cat /proc/tty/drivers
To see how long your system has been running (secs)
cat /proc/uptime (or uptime command)
To see your version of linux
uname -a (or cat /proc/version)
To list your video devices
cat /proc/video/dev
hdparm To list hard drive details

Display text in large files
grep -n "search string" largefile
-- returns 112322 This is the text
cat largefile | sed -n '112322,112333p'
Dual LAN Cards
To setup 2 identical lan cards in the one system, boot on a dos boot disk, load your test.exe to allocate known io and irq to the cards,boot and have a look at the log using dmesg then edit /etc/modules.conf
alias eth0 ne
alias eth1 ne
alias usb-controller usb-uhci
options eth0 -o ne-0 io=0x300 irq=3
options eth1 -o ne-1 io=0x340 irq=12

Now ifup eth0
ifup eth1
or try
modprobe ne eth0 io=300 irq=10
<ESC> k
Use all the normal editing keystrokes in the Korn shell.  This one will recall the previous command.  Use all the normal editing keystrokes in the Korn shell.  This one will recall the previous command. 
Use <ESC> / <search string> to find a previously executed command.
Ethereal Packet Capture
Use the following filter to capture traffic between servers on a port 53:

iphost and dst and dsp port 53
Find details of installed PCI devices on Linux
Find details of installed PCI devices on Linux using the following commands:

lspci -vvv

cat /proc/pci
Find the Unix host name
Enter the "hostname" command to show the name of the host that you are using.
Forced Core Dump
To force a core dump of a running application -

gcore <PID>

This pauses the process for a moment while the memory image is dumped to core.<PID> and then it continues running.
Hex dump
Dump a file in hex using
od -x file.bin

Show as octal backslash notation
od -c file.bin
High Capacity Floppies
Geoff C
You can format a floppy to higher or lower than normal capacity with Linux as follows:

# To see options
cat /etc/fdprm

# To set fd0 format to 1760kB (half of 3520)
setfdprm /dev/fd0 3520 22 2 80 0 0x1b 0x00 0xcf 0x6c

# To format the floppy using these settings
fdformat /dev/fd0
If then tests
wc -l
if [ $n -lt 3 ]
echo "Less than 3 lines in file"
echo "3 or more lines in file"
Bash script...

n=cat testfile
Import directory into CVS
To import an entire directory into a specific location in a cvs repository (a/b/c)

cd __target_dir___
cvs im a/b/c KEY HEAD

To add a single file

cd __target_dir____
cvs add __filename__
cvs commit
Kill many named processes
To kill many processes called <name>

kill 'ps -ef | grep <name> | grep -v grep | awk '{ print $2 }''
Looping in ksh
DIRS='ls *.log'
for DIR in $DIRS
echo "Processing $DIR"
ls -l
Show the contents of the current directory in long format sorted by file size.
Monitor memory, CPU and I/O statistics
vmstat 1




Mount disk image
In order to mount and view a disk image:

mkdir /mnt/tmp
mount -o loop /tmp/boot.img /mnt/tmp
ls -ltr /mnt/tmp
Mount windows disk on unix
# Mount windows share on Unix system
smbmount //MYWINNT/Docs /mnt/unixdirname -o username=windozeuser,password="fred"
# Copy document from Unix to Windows
cp /tmp/obj/published/A444 /mnt/unixdirname
# Unmount windows box
umount /mnt/unixdirname
netstat -an | grep 8030
Show what is happening on TCPIP port 8030.
Network Port Scanning
If you have a Linux system, you can easily locate all computers on your network using nmap as follows:
nmap -sP 192.16.60.*
Network timeouts
To set the TCP/IP network timeout:

/usr/sbin/ndd -set /dev/tcp tcp_fin_wait_2_flush_interval 30000
/usr/sbin/ndd -set /dev/tcp tcp_time_wait_interval 30000
Show the DNS name of the IP address.
Operating system settings
ulimit -a will show the current file handle and memory limits per process.  To change the limits, alter the /etc/system file or place entries in the script file used to start the application.
Instead of using

ps -ef | grep <name> | grep -v grep | awk '{print $2}'

you could try

pgrep <name>

pgrep will return the PIDs of the processes that match the selection criteria
Reset root password
In order to reset the root password on a linux system that you have physical access to:
- reboot
- Ctrl-X (at the kernel selection screen)
- linux 1 (to boot to single user mode)
- passwd root
- reboot
Running jobs from cron
Cron jobs have no initial environment.  The best way to run a task is to have cron call a job.ksh file containing the following that will establish the environment and log the output.
. ~/.profile
exec > $0.log 2>&1
cd /mydir
./myjob ./myini.ini
Secure Copy
scp fromuser@fromhost:frompath touser@tohost:topath
Set file handle limit
ulimit -Sn 8192
Show commands executing in a shell script
set -x
Show CPU Details on Solaris
kstat -n cpu_info0
SIGHUP 1 --> hangup
SIGINT 2 --> interrupt (rubout)
SIGQUIT 3 --> quit (ASCII FS)
SIGILL 4 --> illegal instruction (not reset when caught)
SIGTRAP 5 --> trace trap (not reset when caught)
SIGIOT 6 --> IOT instruction
SIGABRT 6 --> used by abort, replace SIGIOT in the future
SIGEMT 7 --> EMT instruction
SIGFPE 8 --> floating point exception
SIGKILL 9 --> kill (cannot be caught or ignored)
SIGBUS 10 --> bus error
SIGSEGV 11 --> segmentation violation
SIGSYS 12 --> bad argument to system call
SIGPIPE 13 --> write on a pipe with no one to read it
SIGALRM 14 --> alarm clock
SIGTERM 15 --> software termination signal from kill
SIGUSR1 16 --> user defined signal 1
SIGUSR2 17 --> user defined signal 2
SIGCLD 18 --> child status change
SIGCHLD 18 --> child status change alias (POSIX)
SIGPWR 19 --> power-fail restart
SIGWINCH 20 --> window size change
SIGURG 21 --> urgent socket condition
SIGPOLL 22 --> pollable event occured
SIGIO SIGPOLL --> socket I/O possible (SIGPOLL alias)
SIGSTOP 23 --> stop (cannot be caught or ignored)
SIGTSTP 24 --> user stop requested from tty
SIGCONT 25 --> stopped process has been continued
SIGTTIN 26 --> background tty read attempted
SIGTTOU 27 --> background tty write attempted
SIGVTALRM 28 --> virtual timer expired
SIGPROF 29 --> profiling timer expired
SIGXCPU 30 --> exceeded cpu limit
SIGXFSZ 31 --> exceeded file size limit
SIGWAITING 32 --> process's lwps are blocked
SIGLWP 33 --> special signal used by thread library
SIGFREEZE 34 --> special signal used by CPR
SIGTHAW 35 --> special signal used by CPR
SIGCANCEL 36 --> thread cancellation signal used by libthread
SIGLOST 37 --> resource lost (eg, record-lock lost)
SMTP and POP Syntax
If you want to debug your mail server, you can use telnet as follows.

For POP use:
telnet yourpophostname 110
user geoff
pass xxxxxx
retr msg#
top msg# #lines
dele msg#

telnet yoursmtphost 25
helo mydomain.com
rcpt from geoff@mydomain.com
rcpt to tony@otherdomain.com
This is the message and ends with the following line.

Solaris process details
pgrep -f searchitem -l
Solaris Processor Groups
# Query the current processor sets
echo "List of currently active processor sets"
psrset -i

# Create a processor set for this application
psrset -c

# Determine the number of the created set
GROUP='psrset -i | grep empty | head -1 | awk '{print $4;}'
| cut -d":" -f1'

# Add CPUs to the set
echo "Adding processors to set [$GROUP]"
psrset -a $GROUP 0
psrset -a $GROUP 1

# Launch application on this processor set
psrset -e $GROUP ./myapp ......
TCP/IP Timeouts
Use the following settings to set the network timeout to 30 sec on Solaris

/usr/sbin/ndd -set /dev/tcp tcp_fin_wait_2_flush_interval 30000
/usr/sbin/ndd -set /dev/tcp tcp_time_wait_interval 30000
See http://director.enhydra.org/software/documentation/README-TUNING.html
Tivolli Backup and Restore
Once you are in the dsmc interface you can type help, or I have been using the command:
res <filename> -fromnode=dogmatix -sub=yes "<output file spec>"
where <filename> is the fully qualified path of the file to be restored and
<output file spec> is the fully qualified name of the output file.  Note that the output file spec is in double quotes.

res -fromnode=dogmatix -sub=yes "/<dirname>/*" "/<output dirname>/"
where <dirname> is the name of the directory to be restored and <output dirname> is the name of the output directory.
N.B. It is important to have the quotes and the "/" in place to ensure the command works.
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here
Translate reserved characters
To remove ascii 240 characters from a file use:

cat sourcefile | tr '\240' ' ' > filteredfile
Unix O/S version information
uname -a
Use ksh with history
export VISUAL

exec ksh
Using Acacia Tips RSS Feed
Go to http://www.acacialt.com.au/cgi/showtip.cgi?tipid=1 to show tip 1. 
Change the tip number to see other tips or hook up our news feed at http://www.acacialt.com.au/cgi/rss.cgi
more about RSS at http://www.acacialt.com.au/Projects/rss.htm
VI Configuration
To set 2 char tabs and auto indent for vi, add a .exrc file to your home directory that contains:

"Default tab for 2
set ts=2
set ai
set sw=2

Note that comments start with double quotes.
view filename
This is a read-only version of the popular vi utility. This is a read-only version of the popular vi utility. 
Use it to ensure that you do not change the source file accidentally.
Virtual memory size of a process
ps -o vsz -p <PID>
vncview on Linux
When using tightvnc on linux to attach to a screen locked Windows system that is running vncserver, you cannot hit Ctrl-Alt-Del to get a login prompt.  Try also holding the shift key.  This works.
who am i
Show who you are logged in as.
X-Display Setup
To display the output of an X terminal session such as xload, xterm or xcalc, determine your PCs IP address using ipconfig.
On the Unix box, type the following:
export DISPLAY=
xload &
X Windows
Start X windows using "startx" or "X -xf86config myxf86config-4.file"

Disable X windows from starting automatically by commenting out the following line in /etc/inittab
"x:5:respawn:/etc/X11/prefdm -nodaemon"

Stop X windows by changing to init level 3
init 3

Email me to get a sample XF86Config-4 file that supports multiple cards and monitors on Linx

Click here to add your own tips.