bash cheat sheet
bash cheat sheet

bash cheat sheet

Debian specific

  • install Let’s Encrypt certificates

    apt-get update
    apt-get install software-properties-common
    add-apt-repository ppa:certbot/certbot
    apt-get update
    apt-get install python-certbot-nginx
    certbot --nginx #  for nginx
  • find packages that contain a certain file (e. g. a header file that is needed by a build)

    apt-file search file_name
  • install VNC

    apt-get update
    apt-get install -y x11vnc
    apt-get install gnome-core
    x11vnc -storepasswd
    apt install xinit
    startx &
    x11vnc -xkb -noxrecord -forever -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -usepw &
  • install mount support for SMB protocol

    apt install cifs-utils
  • temporary mount

    mount //server/share /mnt/share -o user=user,pass=pass
  • persistent mount
    edit /etc/fstab to persistently mount an SMB network share

      mkdir /media/yourshare
      id # note uid and gid, e. g. 1000 and 1004
      vi /etc/fstab
    Password inside fstab Secure password outside fstab
    servername/yourshare      /media/yourshare      cifs    username=user,password=pass,uid=1000,gid=1004   0       0
    mount -a # reload fstab
     //server/share /pathto/mountpoint cifs credentials=/home/username/.smbcredentials,uid=shareuser,gid=sharegroup 0 0

    Create the .smbcredentials file in your home directory:


    Make sure you secure your ~/.smbcredentials file:

    chmod 0600 ~/.smbcredentials
  • unmount persistent network share

    vi /etc/fstab # remove entry from /etc/fstab
    mount -a # reload fstab
    umount /media/yourshare
    rm /media/yourshare
  • update Debian

    apt-get update # fetches the list of updates
    apt-get upgrade # strictly upgrades the current packages
    apt-get dist-upgrade # installs updates (new ones)
  • start programs automatically

    crontab -e
    @reboot command_or_script
  • send a mail

    Subject: your subject
    Text of mail

    (then send mail with controlZ)

  • share folders over NFS

    1. install mount support for NFS protocol

      apt-get install nfs-common
    2. edit fstab to persistently mount an NFS network share

      mkdir /media/yourshare
      vi /etc/fstab
      servername:/yourshare    /media/yourshare   nfs     rsize=8192,wsize=8192,timeo=14,intr     0       0
      mount -a # reload fstab

Mac specific

  • install gcc and make CMake use it instead of /usr/bin/g++ (i. e. XCode Clang)

    brew install gcc
    export CC=/usr/local/Cellar/gcc/9.1.0/bin/gcc-9
    export CXX=/usr/local/Cellar/gcc/9.1.0/bin/g++-9

    Adapt above path so it matches the one printed by brew info gcc.

  • print version of shared library

    otool -L libName.dylib
  • share a file

    sharing -a file_name
  • list files including extended attributes

    ls -l@
  • show the names and values of all extended attributes of a file

    xattr -l file
  • compress directory into zip archive

    zip -r /path/to/folder
  • compress directory into zip archive with password

    zip -er /path/to/folder
  • copy file content into clipboard

    pbcopy < /path/to/source_file
  • copy clipboard content into file

    pbpaste > /path/to/target_file
  • create directory hardlink

    brew install hardlink-osx
    hln /path/to/source_directory /path/to/target_directory
  • open file with associated app

    open file_name
  • see the list of outdated software

    softwareupdate --list
  • update all outdated software

    softwareupdate --install --all
  • update specfic software

    softwareupdate --install product_name
  • suspend system

    /System/Library/CoreServices/Menu\ Extras/ -suspend
  • stop dock icons from bouncing

    defaults write no-bouncing -bool TRUE
    killall Dock

Linux and Mac

  • create patch file

    diff -u modified_file original_file >my_patch.patch
  • apply patch file

    patch -u file_to_be_modified my_patch.patch
  • change text encoding of file (here from utf-16 to utf-8)

    iconv -f utf-16 -t utf-8 </path/to/source_file >/path/to/target_file
  • print hex dump of file

    xxd filename
  • print all environment variables

  • compare folder contents

    diff -rq <folder1> <folder2>
  • print folder size

    du -sh /path/to/folder
  • copy file to server

    scp -P <port> local_file user_name@server_name:/target/path
  • ssh-login to server

    ssh -l user_name -p <port> server_name
  • create symlink (example in /usr/local/bin)

    ln -s /path/to/my_executable /usr/local/bin/my_executable
  • try to make PDF file smaller

    gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/PSL2Printer -dCompatibilityLevel=1.4 -sOutputFile=out.pdf in.pdf
  • list possible pdfsettings

    gs -dNODISPLAY  -c ".distillersettings {exch ==only ( ) print ==} forall quit"
  • check which nfs shares are available on an nfs server

    showmount -e server_name
  • list all files in a directory

    find . -type f -ls
  • list all symlinks

    find . -type l -ls
  • delete all files in a directory

    find . -type f -delete
  • list currently running processes

  • find a process by its name

    pgrep process_name
  • kill a process by its name

    pkill process_name
  • create SSH key

  • switch on SSH logging

    vi /etc/ssh/sshd_config # comment in LogLevel INFO
    service ssh restart
    tail -f /var/log/auth.log
  • change SSH port

    vi /etc/ssh/sshd_config # change the line containing the port
    service ssh restart
  • show processes that lock a file:

    lsof /path/to/locked/file
  • change owner of a file

    chown username filename
  • list files containing given text

    • list file names

      grep -rl --include "*.ext" "text_to_search_for" /path/where/to/search
    • list file names and part of text

      grep -ro --include "filename" -e "pattern" .

      (-rho to suppress the file name)

  • find file based on its name (case insensitive)

    find / -iname "file_name"
  • list files with a certain extension

    find /path/where/to/search *.c