Skip to content

Instantly share code, notes, and snippets.

@j0n3
Last active November 13, 2021 12:57
Show Gist options
  • Save j0n3/ddeb8582e3363f9f2747097c79f4f72e to your computer and use it in GitHub Desktop.
Save j0n3/ddeb8582e3363f9f2747097c79f4f72e to your computer and use it in GitHub Desktop.
Living in a Linux console

living in a linux console

This is a compilation of tips and tricks, tools and shortcuts for those who enjoy living inside a terminal.

I use debian + gnome3 + terminator + zsh + oh-my-zsh and some commands could not work on all terminals.

Bash shortcuts

(from http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/)

Command Editing Shortcuts

Ctrl + a – go to the start of the command line
Ctrl + e – go to the end of the command line
Ctrl + k – delete from cursor to the end of the command line
Ctrl + u – delete from cursor to the start of the command line
Ctrl + w – delete from cursor to start of word (i.e. delete backwards one word)
Ctrl + y – paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor
Ctrl + xx – move between start of command line and current cursor position (and back again)
Alt + b – move backward one word (or go to start of word the cursor is currently on)
Alt + f – move forward one word (or go to end of word the cursor is currently on)
Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + c – capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + u – make uppercase from cursor to end of word
Alt + l – make lowercase from cursor to end of word
Alt + t – swap current word with previous
Ctrl + f – move forward one character
Ctrl + b – move backward one character
Ctrl + d – delete character under the cursor
Ctrl + h – delete character before the cursor
Ctrl + t – swap character under cursor with the previous one

Command Recall Shortcuts

Ctrl + r – search the history backwards
Ctrl + g – escape from history searching mode
Ctrl + p – previous command in history (i.e. walk back through the command history)
Ctrl + n – next command in history (i.e. walk forward through the command history)
Alt + . – use the last word of the previous command

Command Control Shortcuts

Ctrl + l – clear the screen
Ctrl + s – stops the output to the screen (for long running verbose command)
Ctrl + q – allow output to the screen (if previously stopped using command above)
Ctrl + c – terminate the command
Ctrl + z – suspend/stop the command

Bash Bang (!) Commands

Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.

!! – run last command
!blah – run the most recent command that starts with ‘blah’ (e.g. !ls)
!blah:p – print out the command that !blah would run (also adds it as the latest command in the command history)
!$ – the last word of the previous command (same as Alt + .)
!$:p – print out the word that !$ would substitute
!* – the previous command except for the last word (e.g. if you type ‘find some_file.txt /‘, then !* would give you ‘find some_file.txt‘)
!*:p – print out what !* would substitute

Bonus winner commands

ctrl + 7 <- undo! (works for multiple records on history!!). Also ctrl + _

(motion) ctrl + x ctrl + e: opens default editor for power command editing (executes after quit on bash or leave command on prompt on zsh)

(more on shortcuts: https://ss64.com/bash/syntax-keyboard.html)

Completion

use bash-completion package in order to help yourself completing available commands. Zsh and oh-my-zsh is a great combination as it let you navigate through options.

apt install bash-completion

Viewing files

  • Less is more than more
  • cat
  • tail
  • head
  • ccze -A: colorize (great combo with tail)

history

  • ctrl + r: backward search
  • ctrl + s: forward (only if setty -ixon)
  • sudo!!

command inception

``command $(command)```

tools

escaping

backslash is your friend!

single dash

Some apps cand handle - as input instead of a filename. Example:

ls | vim - will open ls ouput as content for vi

double dash --

use two dashes to stop parsing parameters.

Example:

touch -- -hello rm -- -hello

(remember those ugly filenames?)

tree

tree -d -L 2

df -h

du --summarize -h | sort -h

uniq

ssh command

ssh user@host -C -t 'sudo apt update && sudo apt dist upgrade -y'

alias

which, whereis, whatis

finding packages

apt search stuff dpkg-query --list or dpkg -l

environment variables

- env
- set

More tools

  • sed
  • awk
  • grep + grep -v + grep -e
  • htop
  • ps aux
  • killall
  • locate/updatedb

vim

execute current line in bash

:.w !bash

notify

require sudo apt install notification-daemon libnotify-bin (or only libnotify-bin)

example:

notify-send something dosomething; notify-send dosomething && notify-send good || notify-send bad tail -f /tmp/kk.log | while read line; do notify-send "$line"; done ssh -t millocal 'tail -f /tmp/was.log' | while read line; do notify-send "$line"; done

help info man

Processes and jobs

  • my processes: ps ux
  • all processes ps aux
  • htop
  • go background: ctrl + z
  • list jobs: jobs
  • get job on foreground fg or % or %[jobnumber] or (zsh) %[command-name]

Different shells

  • sh vs bash vs zsh (+oh-my-zsh)
  • debug script (set -x)
  • do not beep, plz (setterm -blength 0)
  • Any executable #!/path/to/tool (such as #!/bin/bash #!/bin/php

ToDo

  • Powerful find commands
  • for f in $(command); do command; done
  • redirect outputs (STDOUT STDERR...)

text to clipboard

cat file.txt | xsel -i -b or xclip -sel clip < wiki_snt_testing.txt

Other great stuff

who is on this network?

arp-scan -l

info about my network card

ethtool eth0 useful info such as link speed

my public ip

wget -qO- ipecho.net/plain or easier with curl curl ipecho.net/plain

weather in my city

curl wttr.in/alcala_de_henares

expr is calculator and much more

expr 5 * 111 will return 555

expr $(expr 5 \\* 111) : "5\+" will count how many 'fives' the previous expr has

ping with datetime

ping google.com -i 10| while read pong; do echo "$(date): $pong"; done will ping each 10 seconds and decorate ping output with date

never exit

use ctrl + d instead as it will not keep the command on the history. Almost any console apps will quit (mysql, ssh sessions...)

go home ~

cd /home/use = cd $HOME = cd ~

toggle paths

try cd /tmp then cd -. Will go to previous path. Do again to toggle between paths. Zsh can handle more records cd -3. Use completion to see what

diff files

diff file1 file2 vimdiff file1 file2

I have a browser, of course

also you by installing lynx

youtube-dl

you can download any youtube video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment