Autres
AideEnLigne
CahierDeBrouillon
Présentation
Administration

MesLectures
[Journal d'Ophelia]
[Blog Larnac]
[Traitement texte en ligne]
[Kim Khal]

Informations
[Rue 89]
[Tele libre]

DNS etc
[Robtex]
Logins
Votre ID: 111
Nom:
Login utilisateur
Mot de passe éditeur

ShellTricks

+Bash redirections
  • both stdout and stderr file dirlist: ls > dirlist 2>&1
  • redirecting input from myfile to descriptor n: n<myfile
  • appending: n>>file
  • redirecting stdout and stderr: &>word or >&word
  • appending stdout and stderr: &>>word
  • close stdin: 1<&- close stdout: 2<&-
  • duplicating input file descriptor n is a copy of word: n<&word
  • duplicating output file descriptor n is a copy of word: n>&word
  • movin input file descriptor: n<&digit- (from digit to n)
  • movin output file descriptor: n>&digit- (from digit to n)
  • opening myfile descriptor for read and write: n<>myfile

  • Exemple
 exec 1<&- (close stdout)
 exec 2<&- (close stderr)
 exec 1<>$logfile  (moving stdin to logfile; read write from )
 exec 2>&1 (stderr redirected to stdout)

+check shell syntax on line
+shell trap

 #!/usr/bin/bash
 # will execure cleanup in any case of exiting
 #
 function cleanup {
 # cleanup before exiting this script
 }
 trap cleanup EXIT   

 .... lot of useful script lines.

 #
 #FIN
+shell loop
 #!/bin/sh
 LOOP=12
 while test $LOOP -lt 20
 do
     LOOP=`expr $LOOP + 1`
     echo $LOOP
 done
+command line

  • bash:
    • Ctrl-r to search in history
    • 'ctrl-x ctrl-e' opens an editor to work with long or complex command lines
    • Input from the commandline as if it were a file by replacing 'command < file.in' with 'command <<< "some input text"'
    • 'shopt -s cdspell' automatically fixes your 'cd folder' spelling mistakes
  • bash or csh: cd - change to the previous directory you were working on
+bash pseudo aliases
  • function lt() { ls -ltrsa "$@" | tail; }
  • function psgrep() { ps axuf | grep -v grep | grep "$@" -i --color=auto; }
  • function fname() { find . -iname "*$@*"; }
+unsorted tricks

  • TOOLS
  • 'htop' instead of 'top'
  • Use 'apt-file' to see which package provides that file you're missing
  • 'dict' is a commandline dictionary
  • 'echo start_backup.sh | at midnight' starts a command at the specified time
  • Pipe any command over 'column -t' to nicely align the columns
    • Google 'magic sysrq' and learn how to bring you machine back from the dead
  • 'diff --side-by-side fileA.txt fileB.txt | pager' to see a nice diff
  • if you liked the 'psgrep' alias, check 'pgrep' as it is far more powerful
  • never run 'chmod o+x * -R', capitalize the X to avoid executable files. If you want _only_ executable folders: 'find . -type d -exec chmod g+x {} \;'
  • 'xargs' gets its input from a pipe and runs some command for each argument
  • run jobs in parallel easily: 'ls *.png | parallel -j4 convert {} {.}.jpg'

  • NETWORKING
  • 'socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22' forwards your port 1234 to another machine's port 22. Very useful for quick NAT redirection.
  • Some tools to monitor network connections and bandwith:
    • 'lsof -i' monitors network connections in real time
    • 'iftop' shows bandwith usage per *connection*
    • 'nethogs' shows the bandwith usage per *process*
  • ssmtp can use a Gmail account as SMTP and send emails from the command line. 'echo "Hello, User!" | mail user@domain.com' ## Thanks to Adam Ziaja.

  Configure your /etc/ssmtp/ssmtp.conf:
      root=***E-MAIL***
      mailhub=smtp.gmail.com:587
      rewriteDomain=
      hostname=smtp.gmail.com:587
      UseSTARTTLS?=YES
      UseTLS?=YES
      AuthUser?=***E-MAIL***
      AuthPass?=***PASSWORD***
      AuthMethod?=LOGIN
      FromLineOverride?=YES

Retrieved from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt