Vi Exit

[problem]

Exiting a file in vi.

[/problem]

[solution]

There are quite a few ways to exit a file in vi. Here are some of my favorites.

[/solution]

[example]

The standard: :wq

Force, in the case of read-only: :wq!

Abort updates: :q!

Reset the file without writing: :e!

Quick save and exit: 😡

Lazy one, without using a colon: ZZ

Going to another file: n filename

Go back to the first file, when you’v edited with wildcard or list of files: :rew

[/example]

[reference]

[tags]vi, Unix Coding School[/tags]

[/reference]

Environmental Variables

[problem]

You want to set something once and have it remembered between UNIX settings.

[/problem]

[solution]

Set some variables. Simplest way to set environmental variables, is via the shell rc or profile scripts.

[/solution]

[example]

For example if your shell is zsh, use .zprofile or .zshrc. For bash it is .bashrc or .profile.

To invoke env vars from cron, use something like this:


30 * * * * zsh -c "(source $HOME/.zprofile; cmd ... ")

To source your .zprofile before command invocation. Additionally you can use a dot to source a script containing your vars:


. scriptname

[/example]

[reference]

[tags]UNIX, variables, shell scripting, Unix Coding School[/tags]

[/reference]

Counting Column Contents

[problem]

You want to count the contents of a column. Accumulatively totaling each number in a given column.

[/problem]

[solution]

To total up the contents of a given column, use following awk code:

[/solution]

[example]


nawk -v col=# ' { tot+=$col } END { print tot } '

In this example, do a du of the current directory and then total column one (1). Notice awk receives a variable on the column to sum up.


du -ks * | awk -v col=1 ' { tot+=$col } END { print tot } '
11168

[/example]

[reference]

[tags]UNIX, awk, nawk, gawk, Unix Coding School[/tags]

[/reference]

Regular Expression Matching NOT

[problem]

You want to find a line, not matching the pattern.

[/problem]

[solution]

I find this invaluable whilst editing crons. Or in vi – like this: [^x] where x equals the character you want to ignore.

[/solution]

[example]

For example after performing a crontab -e, wanting to skip comments.


crontab -e
/^[^#]

Also with sed – substituting tags, etc:


sed 's#]*>##g' filename

This says edit your crontab, match the next line that has a beginning, immediately followed by a character that is not a comment.

After performing this once, just type n – for the next match (i.e. next line that does not start with a comment).

[/example]

[reference]

[tags]UNIX, vi, regular expression, Unix Coding School[/tags]

[/reference]

Finding Disk Space Hogs

[problem]

Common admin task, to find what is using all the disk space.

[/problem]

[solution]

Simpliest thing to do, in a known directory – for example /var/log – is to run du -ks *.

That will show all files and directories, in the current directory along with their disk space in kilobytes.

Then cd down the top ones and rerun. You can also pump the output through sort (-n on Solaris and +n on Linux).

[/solution]

[example]


cd /var/log
du -ks * | sort -n # Linux
cd httpd
du -ks * | sort -n

With some flavours of UNIX, du shows actual physical disk space being used – where as df shows disk space being reserved. This is only really noticeable when you remove a file, that is still being written too. du shows the current directory is only using x kb and df still says you are at 100%. 🙂 You need to find and kill the process.

Another useful way of finding largest files, is find command – something like this:


find / -xdev -type f -a size +20000 -ls

This says find files over 10MB (20 thousand 512 byte blocks), do not traverse file systems mounted on this one (-xdev). You can also say -mount to stop find traversing these file systems. That way df and find tally – just re-run it for var, etc as required.

Again this can be pumped through to sort:


find / -xdev -type f -a -size +20000 -ls | sort -k7 -n # linux
find / -xdev -type f -a -size +20000 -ls | sort +6n # Solaris - starts field count from zero

[/example]

[reference]

[tags]UNIX, df, du, Solaris, Unix Coding School[/tags]

[/reference]

Display Directory Usage As Percentage

[problem]

How many times have you been given the job, to clear down space in a UNIX file system?

Ever wanted to see the percentage, on a directory by directory basis.

[/problem]

[solution]

Here is a simple bit of code, to display the current directory’s percentage of over all space available

[/solution]

[example]


(du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '

Here is a run through, checking /usr:


[root@bree usr]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
37%

[root@bree usr]# cd java

[root@bree java]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
4%

[root@bree usr]# cd ../X11R6
[root@bree X11R6]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
2%

[root@bree X11R6]# cd ../lib
[root@bree lib]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
15%

[root@bree local]# cd ../share
[root@bree share]# (du -ks . | tr 'n' ' '; (df -k . | tail -1)) | gawk ' { printf("%0.0f%n",($1/$4)*100); } '
13%

[/example]

[reference]

[tags]UNIX, Admin, Unix Coding School[/tags]

[/reference]

Duplicate Line Finder

[problem]

Have you ever duplicated a cron entry, or just wanted to find duplicate lines in a file.

[/problem]

[solution]

Here is a simple one liner, which shows all lines which occur more than once in a file or standard output.

[/solution]

[example]


sort cronfile.223 | uniq -c | awk ' $1 != 1 '

So to check that cron directly – just do this:


crontab -l | uniq -c | awk ' $1 != 1 '

[/example]

[reference]

[tags]UNIX, awk, script, uniq, Unix Coding School[/tags]

[/reference]

Legato Networker Restore

[problem]

You want to perform a restore from a legato networker backup server.
Also called NSR backup system

[/problem]

[solution]

To restore from an NSR (networker) backup system, just run recover.

It is best if you cd into the required directory first.

[/solution]

[example]


cd /var/www/httpd
recover

It will list your last back ups with dir. You can type help for more info.

Or just add, then when done – type recover.

dir shows if the tape containing your backup, is online or not.

[/example]

[reference]

[tags]NSR, Netwoker, Net backup, Legato, Unix Coding School[/tags]

[/reference]

Check NSR Legato Networker Backups

[problem]

You want to view the back ups performed via Legato Networker NSR.

[/problem]

[solution]

Use mminfo – media manager for NSR. It can show which filesystems were backed up,
when they were backed up, to which tapes and the retension period.

[/solution]

[example]


mminfo -ot -s SERVER -c CLIENT -q 'savetime>=last week' -r 'volume,client,savetime(18),ssid,name,written,pool'


mminfo -ot -s SERVER -c CLIENT -q 'savetime>=last week' -r 'volume,client,savetime(18),ssid,name,written,pool,ssretent'

[/example]

[reference]

For more information on available options, do a man mminfo

[tags]NSR, Backups, Legato, mminfo, Unix Coding School[/tags]

[/reference]

RISC System 6000 Boot LEDs

[problem]

Your RS6000 ain’t booting. 🙂 Or you are just interested what the LEDs mean.

[/problem]

[solution]

See Example

[/solution]

[example]

  1. Power On [LED 100 – 199]
  2. BIST [LED 100 – 199]
  3. POST [LED 201 – 298]
  4. Load Kernel [LED 299]
  5. Configuration [LED 500 – 999]
  6. INIT [LED 553]

init than loads /etc/inittab services – then /sbin/rc.boot (phase 3 of system boot),
/etc/rc (startup shell script), subsystems (cron, qdaemon, tcpip), getty (enable terminals)

[/example]

[reference]

[tags]RISC System bootup LEDs, AIX startup sequence, Unix Coding School[/tags]

[/reference]