Shaun Abram
Technology and Leadership Blog
Don’t use “kill -9”
In the past, any time I wanted to stop an errant process on unix, I just used “kill -9”. By default. Without thinking about it much.
Then a colleague commented to me that you should never use kill -9. It terminates the process with no chance to shutdown in an orderly manner, and so can leave things in a bad state, such as corrupting files. “But what else am I supposed to do!?” I naively asked.
There are of course many other options for the kill command (see links below), but here are some alternatives you can try, in the order you may want to try them.
Tags: bash, commandline, kill, unix
alternative to tail -f that allows scrolling: less +F
You can use less +F to start less in its “forward forever” mode. In this mode, less will behave like tail -f, ignoring the ends of files and providing a steady stream of text.
When you want to scroll, press Ctrl-c. To re-enter forward forever mode, press F.
From http://unix.stackexchange.com/questions/81628/is-there-an-alternative-to-tail-f-that-has-convenient-scrolling
sed
sed (stream editor) is an simple but incredibly versatile command line tool that parses and transforms text. It is line-oriented in that it reads the text line by line, transforms it, and outputs the result.
For example, this sed command would replace all occurrences of the text “white” with “black”:
sed s/white/black/g
sed reads text on a line by line basis and performs an operation on it, usually extracting or replacing text snippets. In this case, the s prefix means substitute, and the g suffix means global. Other example usages include:
find
“find” is a unix command-line tool for locating files (and directories). The results can be displayed, passed to another command (e.g. grep, ls etc, see more below), or the find command has its own limited set of actions that can be performed too, such as delete.
Find allows you to specify all manner of search criteria such as name, location, size, permissions, modify date etc. Using regex expressions with those criteria makes it more flexible still.
See the full find manual here.
Tags: bash, find, grep, tools, unix
awk
I think of awk as a tool for searching, manipulating and reporting on text files, but it is in fact an entire programming language. Its basic function is to search files for lines that contain certain patterns, and perform specified actions on that line.
The name awk comes simply from the initials of its designers Aho, Weinberger and Kernighan.
The basic format of an awk command is:
awk pattern { action } file
Every line in ‘file’ matching the ‘pattern’ will have the ‘action’ performed. Either the pattern or action are optional, but not both.
No pattern means every line is actioned.
No action defaults to print.
Tags: awk, bash, grep, tools, unix
grep
“grep” is a unix command line tool to search a file (or files) for lines containing a match to the given pattern (often a regular expression). Its name comes from the ed command g/re/p for globally search a regular expression and print (1). See the grep manual.
The basic syntax is:
grep [options] pattern [files]
Example:
grep -rl --include=*.java "MySearchString" .
Find files in Unix
I frequently end up searching an entire directory for an elusive file and I can never remember the exact command structure, so posting here:
find /dir/to/search -name "filename.ext" 2>/dev/null
And wildcards are allowed. e.g.
find . -name "filename.*" 2>/dev/null
The latter command searches the current directory AND all sub directories.
The ‘2>/dev/null’ avoids those annoying “find: cannot read dir …: Permission denied” errors.
You can even simplify it by creating your own find.sh script that takes the file name as a parameter:
#!/bin/bash
echo "Searching for files called $1 in current dir and all sub-dirs"
find . -name "$1" 2>/dev/null
Meaning you just need to call, for example:
find filename.*
Also, if you want to search for files containing specific text, try
find /dir/to/search -exec grep -il "txtToSearchFor" {} \;
The “-il” means ignore case and print only the names of files with matching lines (as opposed to the line contents).
Update 4 May 2014: I have added variations of these scripts to my scripts repo on Github. Specifically findf (find files) and findt (find text in files);
See also: