Skip to content

Instantly share code, notes, and snippets.

@codfish
Last active June 25, 2022 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codfish/6ea26e4069a1092ce9e9 to your computer and use it in GitHub Desktop.
Save codfish/6ea26e4069a1092ce9e9 to your computer and use it in GitHub Desktop.
Helpful bash commands & reference. Some commands specific to Debian-based distros.

Bash Notebook

Popular

  • mkdir -p /path/to/dir/to/create - Recursively create directories if necessary
  • scp -r <host>:</source/path> <host>:</destination/path> - Recursively copy files & directories over ssh
  • printenv - List all environment variables
  • lsof -i :<port> - Find out which process is listening upon a port

Conditional Logic

  • a=${VAR:-20} - will assign to a the value of VAR if VAR is set, otherwise it will assign it the default value 20 -- this can also be a result of an expression. This approach is technically called "Parameter Expansion".
  • a=${1:-20} - Same as above, but checking "positional arguments".
  • [[ $b = 5 ]] && a="$c" || a="$d" - If $b = 5, then a will get set to $c, otherwise it will get set to $d.

Reference: http://stackoverflow.com/questions/3953645/ternary-operator-in-bash

Package Management

  • dpkg --list - list out all packages
  • dpkg --list | less - list out packages, in parts. let's you scroll through them
  • dpkg --list | grep -i 'http' - list out packages with http in the name
  • sudo apt-get remove {packagename} - basic uninstall, doesn't remove configuration files, doesn't remove full package “tree”, aka the other packages that are dependencies of that package
  • sudo apt-get --purge remove {packagename} - basic uninstall that includes all config files
  • sudo apt-get --purge autoremove {packagename} - full uninstall that removes config files as well as all dependency packages

awk

  • cat ~/.npmrc | awk -F'=' '{if($1=="//registry.custom.npme.io/:_authToken"){print $2}}' - grab npm token for custom enterprise registry. -F allows you to specify custom delimeter, then using an if statement in the "program" we can check one match and print another.

File Management / Permissions

  • find . -type d -exec sudo chmod 775 {} \; - recursively update permissions of all directories starting from where you are. (Change d to f for files)
  • sudo find / -name newrelic - searches the entire server for files & directories that have the name newrelic
  • ls * | grep -v .gitignore | xargs rm -rf - this will delete all files in your current directory other than the .gitignore file. Works well for cache directories.
  • find <path> -type f -name "Icon*[^\.]" -exec rm -f {} \; - Find files recursively and delete them. If you use a period for the <path> it will begin searching in the current directory
  • find <path> -type f | wc -l - Count number of files in (recursive)
  • find <path> -type d | wc -l - Count number of directories/subdirectories in (recursive)
  • find <path> -type l | wc -l - Count number of symlinks in (recursive)
  • find <path> -delete - Delete everything in . Use a . for the path to delete everything in your current working directory.

xargs

https://www.lifewire.com/uses-of-xargs-command-2201091

  • find ./ -type f -name "*.txt" -print | xargs -l200 -i cp -f {} ./backup
  • find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

User Management

  • cat /etc/passwd - list out all users
  • cat /etc/group - list out all groups and show what users are in those groups
  • sudo usermod -a -G webteam chris - add existing user to an existing group
  • sudo usermod -a -G sudo chris - add existing user to sudo group

Web Server

  • cat /var/log/apache2/example.com-access.log | awk '{print $9}' | sort | uniq -c | sort -nr Get apache response code counts for your site from access log

System Administration

  • /etc/init.d/<servicename> status - check if a service is currently running
  • sudo update-rc.d service defaults - set a service to start on default
  • top - The top program provides a dynamic real-time view of a running system i.e. actual process activity. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.
  • service --status-all - shows the status of all installed services.
  • cat /etc/*-release - find out what version of Linux (distro) you are running
  • cat /proc/version - see kernel version and gcc version used to build the same

Networking

  • ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' - find public IP address for machine

Curl

  • curl -I, curl --head - Just retrieve headers
  • curl http://somurl.com/article/[1-25].jpg -O -s -f &> /dev/null & - Download a range of urls

Logs - Linux Log files and usage

  • /var/log/messages - General log messages
  • /var/log/boot - System boot log
  • /var/log/debug - Debugging log messages
  • /var/log/auth.log - User login and authentication logs
  • /var/log/daemon.log - Running services such as squid, ntpd and others log message to this file
  • /var/log/dmesg - Linux kernel ring buffer log
  • /var/log/dpkg.log - All binary package log includes package installation and other information
  • /var/log/faillog - User failed login log file
  • /var/log/kern.log - Kernel log file
  • /var/log/lpr.log - Printer log file
  • /var/log/mail.* - All mail server message log files
  • /var/log/mysql.* - MySQL server log file
  • /var/log/user.log - All userlevel logs
  • /var/log/xorg.0.log - X.org log file
  • /var/log/apache2/* - Apache web server log files directory
  • /var/log/lighttpd/* - Lighttpd web server log files directory
  • /var/log/fsck/* - fsck command log
  • /var/log/apport.log - Application crash report / log file

To view log files use tail, more, less and/or grep commands.

  • tail -f /var/log/apache2/error.log - Stream the latest errors being logged by apache.
  • tail -f /var/log/apport.log
  • more /var/log/xorg.0.log
  • cat /var/log/mysql.err
  • less /var/log/messages
  • grep -i fail /var/log/boot

General Bash Scripting

  • <command> >/dev/null 2>&1 - Redirect stdout and errors to /dev/null
  • <command> &>/dev/null - Shortcut for above
  • for i in {1..20}; do <command using ${i}>; done - quick way to iterate through a sequence and iterate a command for each one

Bash Keyboard Shortcuts

Source Cheat Sheet (PDF)

My Faves

  • 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 + z – suspend/stop the command
  • Ctrl + u – delete from cursor to the start of the command line
  • 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)
  • Ctrl + r – search the history backwards

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 + 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 + 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 + 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 + c – terminate the command

Bash Bang (!) Shortcuts

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment