tutorials / shell-oneliners

Shell One-Liner Sammlung

Einleitung

Diese Sammlung enthält nützliche Shell One-Liner, die dir die Arbeit im Terminal erleichtern. Von einfachen Tricks bis zu komplexen Befehlen - hier findest du viele praktische Kommandos für deinen Alltag als Entwickler oder Systemadministrator.

Befehlsausführung

Run the last command as root

sudo !!

Runs previous command but replacing

^foo^bar

Rapidly invoke an editor to write long, complex, or tricky command

ctrl-x e

Place the argument of the most recent command on the shell

'ALT+.' or '<ESC> .'

Type partial command, kill this command, check something you forgot, yank the command, resume typing

<ctrl + u> [...] <ctrl + y>

Reuse all parameter of the previous command line

!*

Create a script of the last executed command

echo "!!" > foo.sh

Easy and fast access to often executed commands that are very long and complex

some_very_long_complex_command # label

Escape command aliases

\[command]

Netzwerk & Server

Serve current directory tree at http://$HOSTNAME:8000/

python -m SimpleHTTPServer

Get your external IP address

curl ifconfig.me

Download an entire website

wget --random-wait -r -p -e robots=off -U mozilla http://example.com

Output your microphone to a remote computer’s speaker

dd if=/dev/dsp | ssh -c arcfour -C username@host dd of/dev/dsp

Watch Network Service Activity in Real-time

lsof -i

Show apps that use internet connection at the moment

lsof -P -i -n

SSH & Remote

Mount folder / filesystem through ssh

sshfs name@server:/path/to/folder /path/to/mountFolder/point

Compare a remote file with a local file

ssh user@host cat /path/to/remoteFile | diff /path/to/localFile -

SSH connection through host in the middle

ssh -t reachable_host ssh unreachable_host

System Information & Monitoring

Currently mounted filesystems in nice layout

mount | column -t

Quick access to the ascii table

man ascii

32 bits or 64 bits?

getconf LONG_BIT

List of commands you use most often

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Display the top ten running processes - sorted by memory usage

ps aux | sort -nk +4 | tail

Dateisystem & Dateien

Mount a temporary RAM partition

mount -t tmpfs tmpfs /mnt -o size=1024m

Delete all files in a folder that dont match a certain file extension

rm !(*.foo|*.bar|*.baz)

Push your present working directory to a stack that you can pop later

pushd /tmp

Quickly rename file

mv filename.{old,new}

Diff two unsorted files without creating temporary files

diff <(sort file1) <(sort file2)

Terminal & Display

Clear the terminal screen

ctrl-l

Salvage a borked terminal

reset

Simulate typing

echo "You can simulate on-screen typing" | pv -qL 10

Put a console clock in the top right corner

while sleep 1;do tput sc; tput sc; tput cup 0 $(($( tput cols)-29));date; tput rc;done &

Utilities & Tricks

Execute a command at a given time

echo "ls -l" | at midnight

Query Wikipedia via console or DNS

dig +short txt <keyword> .wp .dg .cx

Update twitter via curl

curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml

A very simple and useful stopwatch

time read (ctrl-d to stop)

Make ‘less’ behave like ‘tail -f’

less +f someLogFile

Close shell keeping all subprocesses running

disown -a && exit

Set audible alarm

ping -i 60 -a IP_address

Reboot machine when everything is hanging

<alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>

Best Practices

Backticks are evil

Statt Backticks solltest du die moderne Syntax verwenden:

echo "The date is: $( date +%D)"

Tipp: Viele dieser Befehle können in Shell-Aliase oder Funktionen umgewandelt werden, um sie noch einfacher zu nutzen. Füge sie einfach zu deiner ~/.bashrc oder ~/.zshrc hinzu!

Weitere Ressourcen