Konsolenschnipsel

Aus TippvomTibb
Zur Navigation springen Zur Suche springen

Hier sammele ich ohne weiteren Zusammenhang Schnipsel, die mir die Consolenarbeit erleichtern.

rsync

Benutze ich am liebsten um sicher zu stellen, dass bei einem Uebertragen eines Verzeichnisses nichts schiefgeht und wenn doch, bekommt man es wenigstens mit.

rsync -avPh --stats Quelle Ziel

Korrektur: Da -a folgende Optionen zusammenfasst:

       -r kopiert Unterverzeichnisse
       -l kopiert symbolische Links
       -p behält Rechte der Quelldatei bei
       -t behält Zeiten der Quelldatei bei,
       -g behält Gruppenrechte der Quelldatei bei
       -o behält Besitzrechte der Quelldatei bei (nur root)
       -D behält Gerätedateien der Quelldatei bei (nur root) 

ist 'p' doppelt gemoppelt. Hier war von mir das grosze P gewollt.

-P aktiviert folgende Optionen:
   --progress Fortschrittsanzeige beim Transfer anzeigen
   --partial Fortsetzung des Transfers bei Abbruch 
-h, --human-readable erzeugt Groeszenangaben, die fuer den Nutzer besser lesbar sind.

Zur Sicherheit: Ist die Quelle ein Verzeichnis und steht am Ende kein / (slash), dann wird ein Verzeichnis mit dem selben Namen in der Quelle angelegt.


Um z.B. ein Full-Backup auf ein USB-Laufwerk (z.B.) zu machen dient folgender Befehl.

sudo rsync -avPhHAX / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/USB

Hier sind folgende Optionen dazugekommen.

-H 	                     Hardlinks werden berücksichtigt 
-A, --acls                  behaelt ACLs bei (aktiviert -p mit)
-X, --xattrs                behaelt extended attributes bei

diff

Um nach einem Übertragen von Daten auf Nummer Sicher zu gehen.

diff -r Ordner1/ Ordner2/

adb

Neben einem MTP-Backup immer noch meine Lieblingsmethode um meine Tablets/Smartphones zu sichern.

adb devices
adb backup --all

oder besser

adb backup "-apk -shared -all -f C:\Users\NAME\backup.ab"

Entpacken des "backup.ab"-Files.

( printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" ; tail -c +25 backup.ab ) |  tar xfvz - 

Näheres hierzu siehe. https://www.gnu.org/software/tar/manual/html_node/Standard.html

simple-mtpfs

Super um über MTP zu mounten.

https://github.com/phatina/simple-mtpfs/

 1 #! /usr/bin/env bash
 2 #Script for simple-mtpfs
 3 PS3=$'\nSelect an option.: '
 4 #mount directory
 5 dir="/media/myphone"
 6 while :; do
 7     clear
 8     options=('Quit' 'List Devices' 'Mount' 'Umount')
 9 	select opt in "${options[@]}"; do
10         case "$opt" in
11             Quit) clear; exit
12             ;;
13 
14             'List Devices') simple-mtpfs -l
15             ;;
16             
17             Mount) simple-mtpfs "$dir"; echo "Mounted to "$dir"" 
18             ;;
19 
20             Umount) fusermount -u "$dir"; echo "Unmounted" 
21             ;;
22         esac
23     done
24 done

Quelle: [1]

alias raspi

alias l='ls -alF'
alias la='ls -la'
alias ll='ls -l'
alias ls='_ls'
alias ls-l='ls -l'
alias md='mkdir -p'
alias o='less'
alias rd='rmdir'

bash .alias

Useful Examples

Unter Suse war im ~root kein bashrc vorhanden. Beim Anlegen eines neuen Users wird eine default-Version normalerweise aus /etc/skel ins (neue) Heimatverzeichnis umkopiert. In dieser Datei (etc/skel/.bashrc) findet sich folgender Hinweistext.

# There are 3 different types of shells in bash: the login shell, normal shell 
# and interactive shell. 
# Login shells read ~/.profile and 
# interactive shells read ~/.bashrc; 
# in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.

Login/Interactive Shell 1 Login/Interactive Shell 2

For example, if you login to bash using an xterm or terminal emulator like putty, then the session is both a login shell and an interactive one. If you then type bash then you enter an interactive shell, but it is not a login shell.
When a login bash shell is invoked, then /etc/profile is sourced (executed in the current environment). After that, three files are checked for existence. The checks for these files are done in this order:
if /etc/profile exists, source (run) it
if ~/.bash_profile exists, source (run) it
if ~/.bash_login exists, source (run) it
if ~/.profile exists, source (run) it

/etc/profile sollte man aber nicht veraendern!

# PLEASE DO NOT CHANGE /etc/profile. There are chances that your changes
# will be lost during system upgrades. Instead use /etc/profile.local for
# your local settings, favourite global aliases, VISUAL and EDITOR
# variables, etc ...

Also dann alles folgende in die etc/profile.local, damit auch alle Nutzer was davon haben;-)

alias histg="history | grep"
alias myip="curl http://ipecho.net/plain; echo"
alias psgrep='ps ax|grep -v grep|grep'

TODO: Special Variables $$, $1, $@, ...

um z.B. sowas hinzubekommen:

 alias fing='function _fing(){ find "$1" -type f -exec grep -l "$2" {} \; ; }; _fing'

Das Ding hat mich Nerven gekostet. Das Problem war die zwie Semicolon am Ende in der Funktion. Das erste \; schlieszt das exec ab, das zweite ; den find Befehl.

Eventuell kannman das auch als alias gebrauchen.

rmv() { mv "$2/${1##*/}" "${1%/*}" ; }

Funktion im alias

Probier mal

alias echotest='f(){ /usr/bin/echo "$1" "$2" "$@"; };f'

oder

alias echotest='function testecho(){ /usr/bin/echo "$1" "$2" "$@"; };testecho'

Kapitel 3.2.5

Fuer Eilige

In OpenSuSE 15.3 gab es noch keine profile.local. Also

vi /etc/profile.local
 
alias psgrep='ps ax|grep -v grep|grep'
alias fing='function _fing(){ find "$1" -type f -exec grep -l "$2" {} \; ; }; _fing'
alias fingl='function _fingl(){ find "$1" -type f -exec grep -l "$2" {} \; ; }; _fingl'
alias histg="history | grep"
alias myip="curl http://ipecho.net/plain; echo"
alias lsd="ls -ld" 
alias confcat="egrep -v '^#|^$'"

und zum Abschluss

source /etc/profile.local

und freuen.

History Statistik

history | awk '{CMD[$4]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n10
    1  71  7.06468%    systemctl
    2  66  6.56716%    cd
    3  63  6.26866%    vi
    4  63  6.26866%    ll
    5  40  3.9801%     find
    6  38  3.78109%    zypper
    7  34  3.38308%    locate
    8  32  3.18408%    ps
    9  32  3.18408%    aplay
   10  31  3.08458%    history

Bash regex

Es ist zum .... Gerade eben einen regulaeren Ausdruck zusammengebaut und mit getestet. Dann in ein Bash-Script eingebaut und POW! Lange Suche kurzer Sinn. Bash kennt keine (nicht alle?) Kurzschreibweisen. Normalerweise sind z.B. diese Schreibweisen gleichbedeutend:

[[:digit:]] = [0-9] = \d

Aber Bash (grep, sed?) kennt das \d nicht!!!

The \d exists in less instances than [[:digit:]] (available in grep -P but not in POSIX).

Ausloeser war eigentlich das Problem, dass meine Soundkarte/Soundchip (MCP73) in einen Standby schaltet, sobald 'alsa' keine (Audio-)Daten schickt. Das machte sich in einem Dauerrauschen bemerkbar, wenn keine Audiodatei abgespielt wurde. Mit

 aplay -Dplug:dmix /dev/zero

war Stille. Dumm nur, dass der Task nach unbstimmter Zeit (Programmfehler?) abbricht, also musste ein Script her, welches von cron aufgerufen werden kann.

 1 #! /bin/bash
 2 output=$(ps ax|grep -v 'grep'|grep -v $0 |grep 'aplay')
 3 [[ $output =~ ^([[:digit:]]+).+(aplay -Dplug:dmix /dev/zero) ]]
 4 #output="19178 pts/0 SL 0:46 aplay -Dplug:dmix /dev/zero"
 5 #[[ $output =~ ^([0-9]+)(.*)$ ]] # zum Testen 
 6 #echo "${output}"
 7 #echo "${BASH_REMATCH[0]}"
 8 #echo "1: ${BASH_REMATCH[0]}"
 9 #echo "2: ${BASH_REMATCH[1]}"
10 if [ -z "${BASH_REMATCH[1]}" ];
11 then
12  exec aplay -Dplug:dmix /dev/zero &
13 fi