annika-w
5/31/2017 - 11:17 AM

Linux/Bash

Linux / Bash commands + options that I need to look up here and there.

Basics

Information about Linux/kernel

# Kernel version
uname -r

Make user session last longer

sudo visudo
# Change line
Defaults        env_reset
# to
Defaults        env_reset,timestamp_timeout={XX}  # XX: time in minutes

Redirecting output

some_cmd > output.log &
echo "Hello" > file.txt

Grep

grep  -R      # search dirs recursively from current dir
      -F      # normal string search (no regex)
      -i      # ignore case
      -v      # invert matching, only show non-matching lines
      -w      # string must be a word, i.e. preceded and followed by non-word 
              # constituent char (everything except letters, digits and _)
      'my search term'
      **/*.py # only search .py files

Tree: Print directory structure

tree /my/dir
  -a  # print all files (hidden)
  -d  # only dirs
  -C  # use system colors for output

Find

Find will only filter for file/dir names, not for file content.

find my/dir/path/ -name "*.txt"   # Find all files in path with .txt extension.

SSH

SSHFS

sshfs {username}@{remote IP}:{remote dire to mount} {local dir to mount to}
# Example:
sshfs wierichs@137.226.133.156:/home/wierichs /home/anni/swarm1

Generate key

ssh-keygen -d

Connecting

Leave public key ~/.ssh/id_rsa.pub at remote server. Key consists of everything in that file.

Log in to machine

# If connecting for the first time, add remote to known hosts:
ssh-keygen -f "~/.ssh/known_hosts" -R IP
ssh IP_ODER_HOSTNAME -l LOGIN_NAME -i ~/.ssh/id_rsa

Close connection

Ctrl D

Kernel Modules

Modules are stored in /usr/lib/modules/<kernel_release>. \ To print kernel version: uname -r

Show and Load Modules

lsmod                       # Show loaded modules
modprobe <module_name>      # Load module
modinfo <module_name>

PCI Devices

lspci -nn       # List all PCI devices, also print vendor + device IDs
update-pciids   # Run if some devices shown as "Unknown Device"

Processes and Jobs

top          # dynamic list
ps           # static list, only with TTY
ps xf        # static list, also without TTY
pstree       # print process tree of the user
kill 5237    # kill process with ID 5237

Network

netstat -tulpen     # show all connections

Kill Background Job

sleep 100 &     # or: sleep 100 -> Ctrl Z -> bg (for background) 
jobs
# > [1]+  Running      sleep 100 &
kill %1

Input Navigation Shortcuts

Text Input

  • Ctrl-W: Delete word
  • Ctrl-A: Beginning of line
  • Ctrl-E: End of line
  • Ctrl-U: Clear line before cursor
  • Ctrl-R: Search command history for string
  • Alt-D: Delete word after cursor

Lib Management

Display library's symbols

nm -g yourLib.so          # Display symbols if .so is not in elf format
objdump -T -C yourLib.so  # Otherwise this...
readelf -Ws yourLib.so    # ...or this.

Display dependencies of a lib or executable

ldd /usr/lib/yourLib.so   # for a shared lib
ldd /usr/bin/firefox      # for executable

bashrc

Add alias

vim ~/.bashrc
alias mynewgitcommand='git status'

Reload bashrc

source ~/.bashrc