System & Shell commands

  • Run a process detached to the current terminal:
    nohup my_command &
    
  • Get the exit code of the latest runned command:
    echo $?
    
  • Run the last command as root (source):
    sudo !!
    
  • Show the user under which I’m currently logged in:
    whoami
    
  • If you have the following error:
    -bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory
    

    Then the fix consist of removing the bad characters:

    sed -i 's/\r//' ./myscript.sh
    
  • Free up some memory by clearing RAM caches (source):
    sync ; echo 3 > /proc/sys/vm/drop_caches
    
  • Display which distro is running the system (source):
    lsb_release -a
    

    or

    cat /etc/lsb-release
    
  • List of most used commands:
    history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
    
  • Disable a service on Debian/Ubuntu, then re-enable it:
    update-rc.d my-service-name remove
    update-rc.d my-service-name defaults
    
  • Same thing as above but on a RedHat-like system:
    chkconfig sshd --del
    chkconfig sshd --add