grep surrounding context

Updated: 16 July 2023

Find tell in trans.sql and show 3 lines of context surrounding the result

grep -C 3 tell trans.sql

Show 3 context lines before and 2 lines after matches

grep -B 3 -A 2 bar index.php

Show --only-matching parts of the matching line

grep -o foo index.php


Updated: 11 January 2023

Show last 3 lines of a file

tail -n 3 /my/file.cs


Updated: 07 May 2023

Pipe command to primary (middle mouse button) clipboard

cmd | xclip

Pipe command to ‘clipboard’ clipboard, for pasting into another application

cmd | xclip -sel clip

Copy contents of file to clipboard, for pasting into another application

xclip -sel clip file.txt


Updated: 23 April 2023

Show a calendar in the terminal

ncal -Cy

Show the calendar for year 1956

ncal -Cy 1956


Updated: 21 May 2023

Append contents of all matching files to sum.log

cat *.log >> sum.log

Overwrite sum.log with contents of all matching files

cat *.log > sum.log


Updated: 20 May 2023

A console file manager with VI key bindings.

Basic file management. Select file and then

:rename <newname>

Hidden files

:set show_hidden true|false


cw – set a completely new file name.
A – to append to extension.
a – to append to filename, before the extension.


Updated: 15 February 2024

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.

By default, sed reads a line from the input stream, removes the newline character and places it in the pattern space, then continues to process all commands in order. Commands without addresses affect all lines. Commands with addresses affect matching lines.

When the end of the script is reached, unless the -n option is in use, the contents of the pattern space are printed out, adding back any trailing newline. Then the cycle restarts with the next input line.

Unless special commands (like D) are used, the pattern space is deleted every two cycles. The hold space, on the other hand, keeps its data between cycles (see commands h, H, x, g, G to move data between both buffers).


Find and replace

sed -i -e 's/few/asd/g' hello.txt

-e the script to be executed.
-i edit file in place

Delete lines which contain the match

sed '/pattern/d' my-file

Delete blank lines

sed -i '/^\s*$/d' migration.php

Change lines

sed -i 's/^INSERT INTO.*$/-- INSERT was here/g' doctrine_migration.php

Find all named files and add a new line to each before line 11

find . -name "index.html" -exec sed -i 11a\ '<link rel="icon" href="/favicon.jpg">' {} \;

Target the last character of the last line of a file

sed '$s/}$/foo}/' Version20201006122408.php

Find and replace a string containing a backslash character

$ echo "example\.com" | sed 's|example\\\.com|mydomain\\\.com|'

Remove all trailing whitespace

sed -i -e 's/[ \t]*$//' messy_file.php


Updated: 20 April 2023

Open file scrolled to the end:

less +G app.log
  • + run a command when the file is opened
  • G jump to end