#bash #linux #redline #tricks

Bash tricks – Some of the useful bash shortcuts

The bash tricks list:

  1. Quick cd to previous directory

    cd -

  2. Set the default text editor

    echo ‘export EDITOR=“vim”’ >> ~/.bashrc

  3. Change the default history size (number of commands that are saved)

    echo “HISTSIZE=2000” >> ~/.bash_profile
    echo “HISTFILESIZE=2000” >> ~/.bash_profile

  4. Find and run a command from history

    history | grep search_keyword
    !n #where n is the command number

    or you can use CTRL-r

    • Run the last command


    • Delete the actual line CTRL-u and paste it back CTRL-y

    • Exit bash without saving history

      kill -9 $$

    • List all in directory sorted by modify date

      ls -latr

    • Preview a command in an interval

      watch <command />

    • Create new nested directories at once

      mkdir -p /tpm/this/and/that/will/be/created

    • Find files and execute a command on them

      find . -name ‘*.php’ | xargs wc -l

    • Loop through a command numerous times

      for i in {1..10} ; do touch filenumber${i} ; done;
      #will create 10 files named file_number_1 .. file_number_10

      Shorter way of doing above :

      touch file_number_{1..10}
    • Synchronize a local direcotry with a remote one

      $ rsync -r -a -z -v -e “ssh -l remoteuser” –delete /local/path host.name:/remote/path

    • Delete all files in directory (shortcut for directories with large amount of files)

      mkdir empty
      rsync -a –delete empty/ target_directory/

    • Print file by columns using awk

      awk -F”delimiter” ‘{ print $1 }’ /path/to/file

      It will print first column, determining columns by “delimiter”

      Real life exapmle, print all usernames in system:

      cat /etc/passwd | awk -F":" '{ print $1 }'
    • Replace Xth column in a row using awk

      awk -F”delimiter” ‘$X=“replace”’  /path/to/file
      #X - is the number of column

      It will print first column, determining columns by “delimiter”

      Real life exapmle, print all usernames in system and replace the password column with a dash:

      cat /etc/passwd | awk -F":" '{  $2=" - "; print $1 $2 }'
    • Simple way to change date:

      date +%Y%m%d -s “20081128”

    • Simple way to change time:

      date +%T -s “10:13:13”

    • Grep recursively :

      grep -R  “searchword” /path/to/search

    • Examine local network:

      nmap -n -sP

    • Find out which directories consume most of the disk space:

      find / -mount -size +1024k -type f -exec ls -la {} \;|sort -rnb -k 5 | less

    • Quick & dirty way to reboot

      kill -9 0

If You want more, check this cheatsheet.

BASH / Redline Shortcuts

  1. ALT + . – Use the last word of previous command
  2. CTRL + t – Auto correct misspelled word
  3. CTRL-x CTRL-e – Edit a command / script in default editor
  4. CTRL + u – Cuts text up until the cursor.
  5. CTRL + k – Cuts text from the cursor until the end of the line
  6. CTRL + y – Pastes text
  7. CTRL + e – Move cursor to end of line
  8. CTRL + a – Move cursor to the beginning of the line
  9. ALT + f – Jump forward to next space
  10. ALT + b – Skip back to previous space
  11. ALT + Backspace – Delete previous word
  12. CTRL + W – Cut word behind cursor
  13. Shift + Insert – Pastes text into terminal