Command line tools are awesome

Linux have a lot of small tools, that only does one thing. But do it really well (compare to Windows, that has a lot of large tools that does everything rather badly).

This is really just a note to myself. These tools are really useful but they are not (yet) second nature to me. I often find myself in the situation, where I know there is a tool for something, that I have used several times before. But simply can’t remember what it was.

grep. Searches files for lines matching a regular expression. Useful parameters (or at least parameters I have a regular use for):

-c returns a count of the lines matching.
-n returns the linenumber (in the file) of the matching line.

tail. returns the last part of a file

-n 6 returns the last 6 lines of the file (standard 10)

head. Returns the first part of a file

-n 6 returns the first 6 lines of the file (standard 10)

cut. Removes sections of lines in a file (or other input)

-d x. Splits the line at x. Use ‘ ‘ for space
-f 1. Select the first field.

wc. Counts stuff in files.

-l. counts the lines in the file (or other input)
-w count the words in the file (or other input)

|. Piping. Takes the result of the command in front of it, and pass it to the command after it (and that is the direction. If you find examples on Stackoverflow that will only give the desired result if the direction is reversed, don’t be surprised if it does not work…)

cat. Prints one or more files to standard output (your screen).

But if we print to another output, eg with “>”, we can concatenate several files.

find. Searches for files. “find .” finds everything. Pipe it to grep to search for something specific. eg “find . | grep ‘acta'” to find all files containing the string “acta”.

-print prints the complete filename.
-print0 prints the complete filename even if it includes a newline.