Ten Good Unix Habits

June 22, 2010

IBM’s DeveloperWorks has 10 Good Unix Habits, which apply to GNU/Linux at least as much as to Unix.

I would expect that most experienced admins can second-guess the content to 5-7 of these 10 points, just from the title (for example, item 1 is a reference to “mkdir -p”, plus another related syntax available to Bash users). I would be surprised if you knew all ten:

1. Make directory trees in a single swipe.
2. Change the path; do not move the archive.
3. Combine your commands with control operators.
4. Quote variables with caution.
5. Use escape sequences to manage long input.
6. Group your commands together in a list.
7. Use xargs outside of find .
8. Know when grep should do the counting — and when it should step aside.
9. Match certain fields in output, not just lines.
10. Stop piping cats.

How many did you get?


Unix / Linux Training Courses in the UK

May 11, 2010

After a few customers requesting it, my consultancy firm, SGP IT, is planning to run some technical training courses this Summer; in the Manchester area initially, though any location is possible.

Now would be a very good time to get in touch (training@sgpit.com) as things are at a very early stage and very fluid – if you can bring a few people along, we can even run a bespoke course for you, and tailor everything to your need.

Depending on subject, duration, location and so on, it should be possible to run the first few courses for as little as £250 – £300 per person per day – much less than the £400 – £500 or so you’d pay for a corporate course where you all get is a trainer who has no experience of the actual situation you face at work, and who delivers powerpoint slides to you, then doles out the free mousepads and t-shirts at the end of the course.

None of us have been overly impressed by many of the available training courses – we are hoping to redefine how personal IT training can be delivered. Here’s how:

The kind of training session I would envisage us providing, would involve a fairly small class size (certainly fewer than 6 people), allowing us to focus on your current issues, and tailor the course around the needs, interests and skills of the attendees. The courses are likely to be between 2 and 5 days, most being 2-3 day courses.

Of course, there will be no corners cut – we will insist on great location and facilities, free internet access, PCs for all candidates (preinstalled with Linux, Solaris, *BSD, you name it – contact us before the course and we’ll build the PC to suit you), tons of good quality course notes, including certificates and the obligatory full VAT receipts, of course. I’m sure that we can find a few freebies to throw in, too!

If you have specific queries or concerns that you would like to be addressed in the course, let us know up-front, and we can find a way to work it in to the course.

If any of this sounds vaguely interesting, please do get in touch (training@sgpit.com) and we can mold things around your requirements.


Use of pipes, and other nifty tricks

December 18, 2009

http://www.tuxradar.com/content/command-line-tricks-smart-geeks has some useful tricks. A lot of it is presented as being bash-specific, but isn’t. Also, a lot seems Linux-specific, but isn’t. Lots of useful info for all Unix/Linux admins here. These hints go on and on; hardly any of them are the generic stuff you often see on Ubuntu forums, stumbleupon, and so on.


Flushing Cache to Disk under Linux

November 4, 2009

There are lots of well-written articles, such as this [westnet.com] and especially this [kerneltrap] on Page Cacheing and pdflush, but RackerHacker (although the title says “reads”, it really seems to address lots of small writes) summarises it very well:

vm.dirty_ratio – The highest % of your memory that can be used to hold dirty data. If you set this to a low value, the kernel will flush small writes to the disk more often. Higher values allow the small writes to stack up in memory. They’ll go to the disk in bigger chunks.

vm.dirty_background_ratio – The lowest % of your memory where pdflush is told to stop when it is writing dirty data. You’ll want to keep this set as low as possible.


find, locate, whereis, which, type

September 16, 2009

I suspect that most Linux admins know 3 or 4 of these five commands, and regularly use 2 or 3 of them.

linuxhaxor has a useful introduction to all five, with the most common uses for each of them.

Note that locate requires a regular run of updatedb – the article says that “The database is automatically created and updated daily” which is true for most distributions, but it depends on your cron setup – you can update the locate db as frequently as you wish. Another thing to note about locate is that it will not use the (normally root-generated) database to tell you (as a non-privileged user) about files which you would not otherwise know about.


get the width of the terminal

September 8, 2009

A quick and easy way to get the width of your terminal is the command stty size. I have used it with diff like this:

diff -y -W `stty size | cut -d” ” -f2` –suppress-common-lines oldfile newfile

Note: This stty option is not available on Solaris, however, if you have it installed, the /usr/openwin/bin/resize command sets the COLUMNS variable.

update: This post originally said “width of your Linux terminal” but as noted in the comments, this feature of stty is also available in *BSD implementations, even though it is not part of the POSIX standard. So you should expect this to work on GNU and BSD systems (eg, most GNU/Linux distros, most *BSDs, including OSX) but not on all POSIX-compliant systems (eg, Solaris). I would assume that AIX, HPUX, SCO, the other “traditional” UNIX systems would also not support this, though I have not (yet) tested any of them. YMMV.


tty, pts, and all of that…

July 19, 2009

The TTY Demystified tells you everything you need to know (and quite possibly more) about the *nix tty history. (If you do need to know more than this, you are probably already writing your own *nix kernel, or at least libc)

Hopefully that should give some insight into the terminfo, and other obscure details of the *nix kernel and its obsession with piping characters from one place to another.


Linux Command Directory

May 16, 2009

I just found this page on the OReilly website – a Linux Command Directory

Click on any of the 687 commands below to get a description and list of available options. All links in the command summaries point to the online version of the book on Safari Bookshelf.

It doesn’t cover everything (what could?) but it could be a useful page to bookmark.


awk one-liners

April 1, 2009

I have previously plugged the great list of sed 1-liners at http://sed.sourceforge.net/sed1line.txt.

Here is a similar (if shorter) list of handy awk 1-liners:

http://www.sap-basis-abap.com/unix/awk-one-liner-tips.htm:

Print column1, column5 and column7 of a data file or output of any columns list

awk '{print $1, $5, $7}' data_file

cat file_name |awk '{print $1 $5 $7}'

ls –al |awk '{print $1, $5, $7}' -- Prints file_permissions,size and date

List all files names whose file size greater than zero.

ls –al |awk '$5 > 0 {print $9}'

List all files whose file size equal to 512bytes.

ls –al |awk '$5 == 512 {print $9}'

print all lines

awk '{print }' file_name

awk '{print 0}' file_name

Number of lines in a file


awk ' END {print NR}' file_name

Number of columns in each row of a file

awk '{print NF}' file_name

Sort the output of file and eliminate duplicate rows

awk '{print $1, $5, $7}' |sort –u

List all file names whose file size is greater than 512bytes and owner is “oracle”

ls –al |awk '$3 == "oracle" && $5 > 512 {print $9}'

List all file names whose owner could be either “oracle” or “root”

ls –al |awk '$3 == "oracle" || $3 == "root" {print $9}'

list all the files whose owner is not “oracle

ls –al |awk '$3 != "oracle" {print $9}'

List all lines which has at least one or more characters

awk 'NF > 0 {print }' file_name

List all lines longer that 50 characters

awk 'length($0) > 50 {print }' file_name

List first two columns

awk '{print $1, $2}' file_name

Swap first two columns of a file and print

awk '{temp = $1; $1 = $2; $2 = temp; print }' file_name

Replace first column as “ORACLE” in a data file

awk '{$1 = "ORACLE"; print }' data_file

Remove first column values in a data file

awk '{$1 =""; print }' data_file

Calculate total size of a directory in Mb

ls –al |awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'

Calculate total size of a directory including sub directories in Mb

ls –lR |awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'


Find largest file in a directory including sub directories

ls –lR |awk '{print $5 "\t" $9}' |sort –n |tail -1


Saving, Escaping, and Restoring positional parameters in a POSIX shell

March 21, 2009

http://www.seanius.net/blog/2009/03/saving-and-restoring-positional-params/ has a really good post about how “shift” loses the original command-line arguments, discusses various seemingly-obvious ways of restoring them, explores why they fail for certain types of input, and provides what seems to be a working solution:

one way you could try to store/save them would be to save the original copy of $@ and use eval set — later on to change it back to the saved variable

… but as it turns out, that doesn’t work with spaces, or quotes. Excellent, really concise description of the problem(s) and solution(s).


Follow

Get every new post delivered to your Inbox.