Skip to content

Latest commit

 

History

History
390 lines (290 loc) · 13.8 KB

bash.md

File metadata and controls

390 lines (290 loc) · 13.8 KB

GNU/Linux

COMMANDS

unzip aaa.zip -d dest     # unzip aaa.zip to folder 'dest'
<some command> | sort -u  # print only unique lines (note uniq is not the tool for this)
ls * | grep -E '\..*$' -o | sort -u     # print list of unique file extensions

command line editing

  • Delete next word: Alt-D
  • Delete prev word: Ctrl-W | Ctrl-X + Backspace
  • Remove character: Ctrl+D or Delete – remove or deletes the character under the cursor.
  • Ctrl+K – removes all text from the cursor to the end of the line.

Bash Bang Commands

  • !! – execute last command.
  • !top – execute the most recent command that starts with ‘top’ (e.g. !).
  • !top:p – displays the command that !top would run (also adds it as the latest command in the command history).
  • !$ – execute the last word of the previous command (same as Alt +., e.g. if last command is ‘cat tecmint.txt’, then !$ would try to run ‘tecmint.txt’).
  • !$:p – displays the word that !$ would execute.
  • !* – displays the last word of the previous command.
  • !:p – displays the last word that ! would substitute.

Find

  • find file: find . -type f -name "test1.txt"
  • find files: find . -type f -name "test*"
  • find directory: find . -type d -name "some-dir"
  • case insensitive: find . -type f -iname "test*"
  • files modified in last 10 mins: find . -type f -mmin -10
  • files modified more than 1 min ago, less than 5 mins ago: find . -type f -mmin +1 -mmin -5
  • find empty file: find . -empty
  • find files over 5 MB: find . -size +5M
  • find files over 5 KB: find . -size +5k
  • find files over 5 GB: find . -size +5G
  • list all file paths recursively: find . -type f

-OPTIONS: - size # takes M k or G - mtime # days - mmin - cmin, ctime # change minutes or days - amin, atime # access minutes or days

History

history              # shows all history (including current)
cat ~./bash_history  # shows all history from past sessions (not current)
history n            # shows n most recent lines of history with numbers
history -w /dev/stdout                # prints history without numbers
history -w /dev/stdout | tail -n n    # prints n most recent lines of history without numbers

Dpkg/apt

  • see installed packages:dpkg -l
  • uninstall package: sudo apt-get remove packagename
  • uninstall package and config files: sudo apt-get purge packagename

Grep

Less

  • show line numbers: less -N

Make

  • Substitutions: $(patsubst %,raw/%.csv,file smile tile) --> raw/file.csv raw/smile.csv raw/tile.csv
  • Implicit recipe (where $* is whatever the % is):
output/%.csv : src/script_to_do raw/%.zip
    python $^ $@ $*

Rename

  • 'rename' syntax: `$ rename 's///'
  • print out changes: rename -v
  • dry run: rename -n
  • reference groups in replacement: $ rename -v -n 's/file_(\d+)/$1_file/' file_* --> rename(file_2020.txt, 2020_file.txt)

Sed

  • print between lines: sed -n '10,40p' filename.txt # prints lines 10 to 40
  • find and replace: sed 's/old-text/new-text/g' input.txt
  • regex find and replace: sed -E 's/^\s+/ - /g' file.txt
  • delete all empty/blank lines: sed '/^$/d' /tmp/data.txt

Ubuntu

check Ubuntu version: lsb_release -a

Safety nets for avoiding overwriting/inadvertent copying

  • Set up noclobber to avoid overwriting files: set -o noclobber (add to .bashrc)
  • Prompt when copying or moving (-i = 'interactive'): cp -i, mv -i (add to .bashrc: alias cp='cp -i')

Filename handling

  • Open first file in an ls: vim $(ls -t some-file-pattern*txt | head -n 1)
  • Find filename and pipe to Vim: grep google= ~/.bashrc | sed -E 's/.*(\/mnt.*\.sh).*/\1/' | xargs -o vim

wc (Word count)

  • Count lines in file: wc -l filename.csv
  • Count lines in files: wc -l data/*.csv
  • Count columns in csv file: head -1 file.csv | sed 's/[^,]//g' | wc -c

Print with color

Get the stem of a filename with parameter substitution/pattern matching src

$ x=abc.txt
$ echo ${x%.txt}
abc

Loop over multiple files:

for f in *.opml; do pandoc $f -o ${f%.opml}.md; done;

References:

CONFIGURATION

  • Convert Bash input to vim mode: add set editing-mode vi to the ~/.inputrc file. src
  • or ad hoc: type set -o vi in the terminal
  • Turn of dinging: put line set bell-style none in your /etc/inputrc or ~/.inputrc file ($ echo 'set bell-style none' >> ~/.inputrc) (src)

NAVIGATION

Move Cursor

Char Word Line
RIGHT Ctrl-F Alt-F* Ctrl-E
LEFT Ctrl-B Alt-B* Ctrl-A
UP Ctrl-P
DOWN Ctrl-N

* Alt- can be done instead by doing Esc then

  • Move one char forward: Ctrl-F or Right
  • Move one char backward: Ctrl-B or Left
  • Move one word forward: Alt-F, Ctrl-Right, or Esc + F
  • Move one word backward: Alt-B, Ctrl-Left, or Esc + B
  • Move one line up: Ctrl-P
  • Move one line down: Ctrl-N
  • Move home: Ctrl-A
  • Move end: Ctrl-E

Delete text

  • Delete char: Ctrl-D or Del
  • Delete previous word: Ctrl-W
  • Delete next word: Alt-D
  • Delete to end of line: Ctrl-K
  • Delete to start of line: Ctrl-U or Ctrl-X + Backspace

Transform text

  • Swap char with previous: Ctrl-T
  • Transposes the two words immediately before (or under) the cursor: Esc + T
  • Transforms the text from the cursor to the end of the word to uppercase: Esc + U
  • Transforms the text from the cursor to the end of the word to lowercase: Esc + L
  • Changes the letter under the cursor (or the first letter of the next word) to upper: Esc + C

Processes

  • Keep using shell after opening other window: append & to the command src
  • Ctrl+Z – suspend the current foreground process. This sends the SIGTSTP signal to the process. You can get the process back to the foreground later using the fg process_name (or %bgprocess_number like %1, %2 and so on) command.
  • Ctrl+C – interrupt the current foreground process, by sending the SIGINT signal to it. The default behavior is to terminate a process gracefully, but the process can either honor or ignore it.
  • Ctrl+D – exit the bash shell (same as running the exit command).

Screen

  • Ctrl+L – clears the screen (same effect as the “clear” command).
  • Ctrl+S – pause all command output to the screen. If you have executed a command that produces verbose, long output, use this to pause the output scrolling down the screen.
  • Ctrl+Q – resume output to the screen after pausing it with Ctrl+S. src

Search previous commands

  • Up arrow key – retrieves the previous command. If you press it constantly, it takes you through multiple commands in history, so you can find the one you want. Use the Down arrow to move in the reverse direction through the history.
  • Ctrl+P and Ctrl+N – alternatives for the Up and Down arrow keys, respectively.
  • Ctrl+R – starts a reverse search, through the bash history, simply type characters that should be unique to the command you want to find in the history.
  • Ctrl+S – launches a forward search, through the bash history.
  • Ctrl+G – quits reverse or forward search, through the bash history.

SCRIPTING

Function syntax:

All are valid (first is most common) src:

function_name () {
  commands
}

function_name () { commands; }

function function_name {
  commands
}

function function_name { commands; }

If-else

#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi

Comparing numbers:

  • In bash, you should do your check in arithmetic context: if (( a > b )); then ... fi
  • For POSIX shells that don't support (()), you can use -lt and -gt: if [ "$a" -gt "$b" ]; then ... fi src

She-bang for script

  • Put this at the beginning of the script: #!/bin/bash
  • Example:
#!/bin/bash

# global variables
var1='A'
var2='B'

my_function () {
  local var1='C'
  var2='D'
  echo "Inside function: var1: $var1, var2: $var2"
}

Arguments

  • referenced in order as $1, $2, etc.
  • $0 = function name
  • $# = number of passed arguments
  • $* and $@ both hold all arguments but behave different when double quoted:
  • When double-quoted, "$*" expands to a single string separated by space --> "$1 $2 $n".
  • When double-quoted, "$@" expands to separate strings --> "$1" "$2" "$n"
  • Slicing arguments:
echo $@;              #"p1" "p2" "p3" "p4" "p5"
echo ${@: 0};  #"bash" "p1" "p2" "p3" "p4" "p5"
echo ${@: 1};         #"p1" "p2" "p3" "p4" "p5"
echo ${@: 2};              #"p2" "p3" "p4" "p5"
echo ${@: 2:1};            #"p2"
echo ${@: 2:2};            #"p2" "p3"
echo ${@: -2};                       #"p4" "p5"
echo ${@: -2:1};                     #"p4"

Case Statements

Example:

#!/bin/bash

echo -n "Enter the name of a country: "
read COUNTRY

echo -n "The official language of $COUNTRY is "

case $COUNTRY in

  Lithuania)
    echo -n "Lithuanian"
    ;;

  Romania | Moldova)
    echo -n "Romanian"
    ;;

  Italy | "San Marino" | Switzerland | "Vatican City")
    echo -n "Italian"
    ;;

  *)
    echo -n "unknown"
    ;;
esac

Loops

Example:

#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

or: for i in 1 2 3 4 5; do echo $i ; done

WINDOWS-LINUX COMPATIBILITY

WSL

  • Mounting Drives
sudo mkdir /mnt/x
sudo mount -t drvfs '\\network\path' /mnt/x
  • Un-mount drive: sudo umount /mnt/x src

  • Mount a Drive Until Logoff:

    • Create a new folder for that drive letter under /mnt if it does not already exist. (ex: mkdir /mnt/m)
    • Mount the drive with sudo mount -t drvfs M: /mnt/m
  • Mount Drives in a Persistent Manner (src)

    • Ensure the folder exists for the mount target (e.g. /mnt/m)
    • Open /etc/fstab and add a line such as the following:
    • M: /mnt/m drvfs defaults 0 0
    • Reload the fstab file with sudo mount -a
  • Using start in WSL: alias start='cmd.exe /C start'

  • Check WSL version (in powershell): wsl -l -v (src)

  • Change WSL version: wsl --set-version Ubuntu 2 (src)

Git Bash for Windows

  • Find etc directory for Windows Git Bash: c/Users/<username>/AppData/Local/Programs/Git/etc/

Commands to interface between Ubuntu and Windows

  • Pipe output to clipboard from WSL: <command> | clip.exe

AutoHotKey - Map CapsLock to Esc:

  • install AutoHotKey
  • create an .ahk script
  • add CapsLock::Esc to the script
  • run the script to enable it
  • to run it at startup, add it to C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
  • you can open that folder by doing win + r and running shell:startup source

CapsLock to BOTH Esc and Ctrl: