Dec 15 2019

Useful Bash Commands

Every programmer should be well versed with Bash, from finding files to schedule tasks.  The following are some of the most common Bash commands that I use on a weekly basis.

  • grep -i aBc <FILE>
    • Grep ignore case
  • grep –color abc <FILE>
    • Color highlight matching pattern
  • grep -v ABC <FILE>
    • Ignore any lines that include ABC
  • grep abc $(find . -mtime 0 -type f)
    • Grep recently modified files
  • grep “abc xyz” <FILE> | grep “02/03/19 09”
    • Find all lines that contain “abc xyz” and then find the remaining lines that contain the following timestamp
  • ps -ef | grep java
    • Find Java processes
  • history | grep git
    • Find all commands in the history that contain the word git
  • history -cw
    • Clear history
  • wc -l <FILE>
    • Number of lines in file
  • ls -1 | wc -l
    • Count the number of files in the current directory
  • ls -lh
    • Show the size unit of the file, such as K for kilobyte, M for megabyte, etc
  • ln -s <SOURCE_FILE> <LINK_FILE>
    • Create a symbolic file link reference to <SOURCE_FILE>
  • df -P <FILE>
    • Find free disk space and if it’s mounted
  • du -hs
    • Find disk usage for each file in current directory
  • cat /dev/null > <FILE>
    • Clear/wipe file
  • zip -r <FILE>
    • Zip all contents in current directory into the specified zip file
  • zip -er <FILE> <DIRECTORY>
    • Zip and encrypt all contents in given directory into the specified zip file
  • zip -d <FILE> “file.txt”
    • Remove file file.txt from the specified zip file
  • unzip -l <FILE>
    • List contents of given zip file
  • diff -rq <DIRECTORY_ONE> <DIRECTORY_TWO>
    • Recursively diff two directory structures
  • crontab -l
    • List cron rules for current user
  • crontab -e
    • Open crontab for editing
  • at -f <COMMAND> -t 201906160701
    • Schedule a command or batch file to run at the specified time
  • atq
    • List current scheduled tasks
  • atrm
    • Remove a scheudle task
  • uuidgen
    • Create a 128-bit UUID
  • date | md5
    • Use md5 on a date to generate a UUID based on a hash
  • $RANDOM
    • Create a random number between 0 and 32k
  • tcpdump -ni eth0 -vvv -s0 -w capture.pcap
    • Listen to eth0 interface in verbose mode and write out to file
  • tcpdump -ni eth0 port 1812 or port 1813 -vvv -s0 -w capture.pcap
    • Listen to eth0 interface for only ports 1812 and 1813