Search for a string in files and save the result

Search all files in this directory and all subdirectories containing a specific string and save the result in a file + 25 characters before and after the file This helps me very often when I have to search huge amounts of data and the terminal window can’t handle the result.

grep -r -o -P '.{0,25}searchstring.{0,25}' ./ > ~/search_result.txt

-r = recursive
-o = only matching, just the fitting line
-P = use Perl regular expressions
./ = in the actual directory

The > saves all output into a specifc file, in this case, its inside your homedirectory ~

SSH-AGENT – Einfache SSH Loginverwaltung

Der SSH Agent ist ein Hilfsprogramm, das die Identitätsschlüssel des Benutzers und seine Passphrasen verfolgt. Der Agent kann sich dann mit den Schlüsseln bei anderen Servern anmelden, ohne dass der Benutzer erneut ein Passwort oder eine Passphrase eingeben muss. Dies implementiert eine Form des Single Sign-On (SSO).

Continue reading SSH-AGENT – Einfache SSH Loginverwaltung

VIM Editor Shortcuts

Some shortcuts for working with VIM, an open source text editor for the console. Available for Linux, Windows & Mac.

Smart movements

  • * and # search for the word under the cursor forward/backward.
  • w to the next word
  • W to the next space-separated word
  • b / e to the begin/end of the current word. (B / E for space separated only)
  • gg / G jump to the begin/end of the file.
  • % jump to the matching { .. } or ( .. ), etc..
  • { / } jump to next paragraph.
  • '. jump back to last edited line.
  • g; jump back to last edited position.

Continue reading VIM Editor Shortcuts

Check If Service Is Running

Simple bash snippet to check if a service is running, if not -> send a mail to root. In this snippet



#!/bin/sh
SERVICE=apache2;

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running, try to start it."
sudo service $SERVICE start
echo "$SERVICE is not running!" | mail -s "$SERVICE down" root
fi

Windows shell: rename all files to lowercase

Alle Files in einem Verzeichnis klein zu schreiben ist sogar recht einfach, da wir alles über die “dir”-Ausgabe pippen können.

for /f "Tokens=*" %f in ('dir /l/b/a-d') do (rename "%f" "%f")

Was bedeutet was?

for /f – Für jede einzelne Zeile
“Tokens=*” – Zwinge jede Ausgabe in eine einzelne Zeile
%f in (…) – %f das ist der Variablenname, in dem Fall der Filename, der von “dir” übergeben wird.
dir – listet alle Dateien in einem Verzeichnis auf
/l – (Parameter von dir) alles klein schreiben.
/b – (Parameter von dir) nur die Filename zeigen, kein Größen, Datum etc. (sonst würde das mit in den neuen Namen fließen)
/a-d – (Parameter von dir) Keine Verzeichnisse auflisten (a = attribute / – ist die Verneinung und d bedeutet Verzeichnis (Directory)).
rename “%f” “%f”- Benenne den Namen mit sich selbst um, da wir vorher schon das “dir” gefiltert haben, erübrigt sich hier ein weiteres Eingreifen.

Continue reading Windows shell: rename all files to lowercase