Link Search Menu Expand Document

Basic Bash Commands

Table of contents

Echo

  • Create txt
cd ~/myFolder
echo "anything" 
    # outputs: `anything`
echo "hello there" > new-text.txt 
    # this creates a new .txt file with content of "hello there"
cat new-text.txt 
    # if the file already exists, new content will replace the content. 
  • Replace content in the .txt file
echo "new content" 
    # new-text.txt
cat new-text.txt 
    # gets "new content 
  • Append content, using >>
echo "append a new line" >> new-text.txt
cat new-text.txt  
    # gets 2 lines 
  • Append output to the .txt file
ls -l .
ls -l . >> new-text.txt
    # all output will be appended to txt file. 
  • Concatenate files
cat text1.txt text2.txt > new-text.txt 
    # contents on both text1 and text2 be printed in new-text.txt file. 

Exit status

  • Run echo $?

This will output the number of the exit status code, check against the number in the command manual.

Pipe

  • Syntax

      command 1 | command 2 
    
  • Example use of Pipe |

      ls a-folder/ | less 
          # less to list out the files in the folder
      ls a-folder/ > test.txt
          # add folder files to the txt file.  
      less test.txt 
          # similar to `cat`, output the content
      tail -4 txt.txt 
          # outputs the last 4 lines of txt file 
      ls | tail -4 | less
      ls | tail -4 | sort > my-new-file.txt
    

Find

find . 
    # this is to find everything in the `pwd` 
find [folder-name] 
    # list out all files inside the folder 
find [folder-name] -name [file-name]
    # look for specific file 
find [folder-name] -type file
find [folder-name] -type d 

Grep

grep [text-string] [file-name] 
    # case sensitive
grep -i [text-string] [file-name] 
    # non-case sensitive
ls | grep [text-string]
    # list out everything contains the text string
ls | grep -v [text-string]
    # everything doesn't contains text string 

Awk

  • Output csv-ish file
awk '{print }' text1.txt
awk '{print $1 $2}' text1.txt
    # $1 = column 1, $2 = column 2, without comma, columns will be concatenated
    # $0 = first column 
    # $NF = last column 
    # $(NF-1) = 2nd to last column
awk '{print $1,$2}' text1.txt
    # using comma, to separate the columns
awk 'NR==2 {print $0}' text1.txt
    # NR==2, Number Row 2, outputs 2nd row 
awk 'NR==2, NR==5 {print $0}' text1.txt
    # outputs row 2, 3, 4, 5, using comma
awk 'NR==2; NR==5 {print $0}' text1.txt
    # output only row 2 and 5, using semi colo 
awk 'NR==2, NR==5 {print $1, $4}' text1.txt
    # 4 rows in 2 columns 
  • Sort out the block of text, separated by ; or , or space
awk -F ";" '{print $1, $2}' text.txt 
    # -F = fields 
  • Looking for words
awk '/usa/ {print }' text1.txt
    # look for and outputs rows with letters between `/ ... /`
awk '/usa|ita/ {print }' text1.txt
    # using `/ A|B /` is to look A `or` B 
awk '$2~/i/ {print}' text1.txt
    # look in 2nd column contains `i` 
awk '$2~/i/ {print $2, $4}' text1.txt
awk '/i/ {print $2}' text1.txt
    # output 2nd column contains letter `i` 
awk '$4==$5 {print }' text1.txt
    # if column 4 = column 5, print the line
  • Length
awk '{print $1, length($1)}' text1.txt 
    # count the length of the word, get 1st column and its length
    # using `length($column)`

Sudo

ls -al 
    # list out the permission on all folders / files 
sudo bash
    # change user to root to use bash 
whoami 
    # output root

Change owner

Read more on User & Permission

chown [new-user-name] [file-or-folder]
    # change user to root for the file 
chgrp [new-group-name] [file-or-folder]
    # change group

Change permission

Read more on User & Permission

chmod u=wrx [file-name]
    # u, user, giving read, write and execute

Variables

echo "anything"
myVar=573
    # create variables 
echo "the value is: $myVar"
    # output: the value is: 573
echo ${myVar}
    # works the same 
myCommand=ls 
    # creating the `ls` command as a variable 
$myCommand 
    # using this will equal to `ls` in the terminal 

Command substitution

d=$(ls)
    # creating d variable as `ls` 
echo $d 
    # output exactly same as `ls` 
echo "$(date)", the variable can also be used ` `: echo "`date`"
    # outputs the same

UNIX login file

  • Variables will not be saved unless its saved in: .profile, .bash_rc, .bash_profile, .login, .zshrc, source file.

Read

Case 1

read myVariable
    # whatever is enter next will be the variable 
echo $myVariable
    # outputs the same content 

Case 2

read -p "type your age " age
    # outputs: "type your age", then enter 87 
    # note, this `read -p` works only in bash shell, not zsh
echo "you are $age" 
    # outputs: you are 87 

Case 3

read -s password
    # enter anything, but it's invisible
echo $password
    # visible 

Case 4

read -sp "type password " pass
    # invisible variable 
echo $pass
    # outputs the invisible variable